【问题标题】:Cannot launch APK file so user can install it, Android 9 onward?无法启动 APK 文件以便用户可以安装它,Android 9 及更高版本?
【发布时间】:2020-11-10 15:08:21
【问题描述】:

我使用此代码在操作系统下载 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);
 }

它适用于 Android 8 及更低版本。但在 Android 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
}

【问题讨论】:

  • 请分享您的项目级 Gradle 文件。也分享 targetSdkVersion
  • @saj 更新您的 compileSdkVersion 和目标 SDK 然后检查
  • 是的,我已经实现了这些方法。关于 Google Play 链接,我无法使用它,因为该应用不在 Google Play 商店中。 @DavidKroukamp
  • 已更新,但由于 XML 布局不兼容,应用程序崩溃。 @KaushalPanchal

标签: android gradle android-install-apk


【解决方案1】:

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

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

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

【讨论】:

  • 这是我顺便分享的第一个链接中的答案:stackoverflow.com/a/54424223/1133011
  • 谢谢,但还不够清楚。它指出 Intent.ACTION_INSTALL_PACKAGE 需要权限,而我使用的操作是 Intent.ACTION_VIEW。但我仍然需要许可。 @DavidKroukamp
猜你喜欢
  • 2013-03-18
  • 2022-11-21
  • 2014-02-17
  • 2015-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
相关资源
最近更新 更多