-1

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
}
sajjad
  • 834
  • 6
  • 13
  • please share your project-level Gradle file. also share targetSdkVersion – Kaushal Panchal Jul 21 '20 at 09:07
  • Have you had a look at these links: https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android or https://stackoverflow.com/questions/4967669/android-install-apk-programmatically?noredirect=1&lq=1 – David Kroukamp Jul 21 '20 at 09:10
  • @saj update your compileSdkVersion and target SDK then check – Kaushal Panchal Jul 21 '20 at 09:14
  • Yes, I have already implemented the methods. About Google play link, I can't use it because the app is not on Google Play Store. @DavidKroukamp – sajjad Jul 21 '20 at 09:15
  • Updated, but the app crashes due to an XML layout incompatibility. @KaushalPanchal – sajjad Jul 21 '20 at 09:25
  • Does this answer your question? [Install Application programmatically on Android](https://stackoverflow.com/questions/4604239/install-application-programmatically-on-android) – David Kroukamp Jul 21 '20 at 22:23

1 Answers1

0

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.

sajjad
  • 834
  • 6
  • 13
  • That was in an answer in the firstlink I shared by the way: https://stackoverflow.com/a/54424223/1133011 – David Kroukamp Jul 21 '20 at 22:22
  • Thanks but it's not clear enough. It states that the permission is required for Intent.ACTION_INSTALL_PACKAGE while the action that I have used is Intent.ACTION_VIEW. But I still need the permission. @DavidKroukamp – sajjad Jul 22 '20 at 05:29