When I tried to create an application that updates and installs after a long time, there were various changes around the authority, so I wrote a memo
compileSdkVersion 29
minSdkVersion 24
targetSdkVersion 29
Use the following for the permission when skipping to the installer with Intent
AndroidManifest.xml
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
You have to go through FileProvider to install Dowloaded apk Many articles still use SupportLibrary.v4, but this time I will use androidx
build.gradle
implementation 'androidx.core:core:{current version}'
FileProvider is added as below
AndroidManifest.xml
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
After that, as written in various articles, just skip it with the Intent that was suitable for each Android version
MainActivity.java
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri fileUri = FileProvider.getUriForFile(MainActivity.this,
MainActivity.this.getApplicationContext().getPackageName() + ".fileprovider",
file);
Intent i = new Intent(Intent.ACTION_INSTALL_PACKAGE);
i.setData(fileUri);
i.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
MainActivity.this.startActivity(i);
} else {
Uri fileUri = Uri.fromFile(file);
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(fileUri, APK_MIME_TYPE);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MainActivity.this.startActivity(i);
}
I think it may change depending on the environment, but this is enough that's all
Recommended Posts