I use this code to launch an APK file after it is downloaded by OS:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setData(uri);
intent.addFlags(FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} else {
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
It works on Android 8 and lower. But on Android 9 and 10 it doesn't work. I couldn't find any log telling the problem. The code also executes till the end and no warning/error log appears! But also no install dialog is launched!
The project targetSdkVersion
is 28. And this is my project level Gradle file:
buildscript {
repositories {
google()
jcenter()
maven {
url 'https://maven.fabric.io/public'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.3'
classpath 'com.google.gms:google-services:4.3.2'
classpath 'io.fabric.tools:gradle:1.31.0' // Crashlytics plugin
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.2"
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url "https://jitpack.io"
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
So after a lot of search, I finally figured out the problem. For android 9 onward, we need an extra permission in AndroidManifest.xml
.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
This isn't really needed for earlier versions of Android. Anyways after adding the permission, the problem got solved for API level >=28.