无法启动APK文件以便用户可以安装它,Android 9以后?

无法启动APK文件以便用户可以安装它,Android 9以后?,android,gradle,android-install-apk,Android,Gradle,Android Install Apk,我使用此代码在操作系统下载APK文件后启动该文件: 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); in

我使用此代码在操作系统下载APK文件后启动该文件:

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);
 }
它在安卓8及更低版本上工作。但在安卓9和10上,它不起作用。我找不到任何说明问题的日志。代码也会一直执行到结束,并且不会出现警告/错误日志!但也没有安装对话框启动

项目
targetSdkVersion
为28。这是我的项目级Gradle文件:

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
}

所以经过大量的搜索,我终于找到了问题所在。对于android 9以后的版本,我们需要在
AndroidManifest.xml
中有一个额外的权限

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>


对于早期版本的Android来说,这并不是真正需要的。无论如何,在添加权限后,API级别>=28的问题得到了解决。

请共享您的项目级Gradle文件。还可以共享targetsdkversion您是否查看了这些链接:或者@saj更新您的编译器dkversion和target SDK然后检查是的,我已经实现了这些方法。关于Google play链接,我无法使用它,因为该应用程序不在Google play商店中@Davidkroukamppup已过期,但由于XML布局不兼容,应用程序崩溃@KaushalPanchalThat在我顺便分享的第一个链接中给出了答案:谢谢,但还不够清楚。它声明Intent.ACTION\u INSTALL\u包需要该权限,而我使用的操作是Intent.ACTION\u VIEW。但我仍然需要许可@戴维德克鲁坎普