android-应用内升级安装apk

直接更新到最新的包, 用于修改底层代码时的更新. (一般热更都只是更新逻辑部分和资源, 如: lua, 配置, 美术资源 等, 但是修改到 java, csharp 时就需要更包了)


  1. AndroidManifest.xml 中加入安装 apk 权限

    1
    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
  2. 安卓 7.0+ 路径获取适配

    1. 添加一个 xml 文件, 路径: res/xml/provider_paths.xml

      1
      2
      3
      4
      <?xml version="1.0" encoding="utf-8"?>
      <paths xmlns:android="http://schemas.android.com/apk/res/android">
      <external-files-path name="external_files" path="."/>
      </paths>
    2. AndroidManifest.xml 中配置这个 xml

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      <application
      ...
      <provider
      android:name="androidx.core.content.FileProvider"
      android:authorities="YOUR_PACKAGE_NAME.fileprovider"
      android:exported="false"
      android:grantUriPermissions="true">
      <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/provider_paths" />
      </provider>
      </application>
      • YOUR_PACKAGE_NAME: 替换成正确的应用包名
  3. 直接贴 安装 apk 的 代码

    总的流程是: 检查安装未知来源 -> 检查权限 -> 执行安装

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    public static void installApk(final Context context, final String filePath) {

    // 安装
    final Runnable instalFn = new Runnable() {
    @Override
    public void run() {
    File dstFile = new File(context.getExternalFilesDir(filePath).getAbsolutePath());
    Log.d(TAG, "installApk: dstFile exist:" + dstFile.exists());

    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Uri apkUri;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // 7.0+ 需要用 fileprovider 来获得 uri
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    apkUri = FileProvider.getUriForFile(context, context.getPackageName() + ".fileprovider", dstFile);
    } else {
    apkUri = Uri.fromFile(dstFile);
    }
    intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
    context.startActivity(intent);
    android.os.Process.killProcess(android.os.Process.myPid()); // 杀掉进程
    }
    };

    // 权限检查
    final Runnable checkPermissionFn = new Runnable() {
    @Override
    public void run() {
    if (!MainActivity.checkPermission(Manifest.permission.REQUEST_INSTALL_PACKAGES)) {
    // 这个 动态权限请求 调整是自己封装的, 就不贴出来
    ActivityMgr.PerRunnable perRunnable = new ActivityMgr.PerRunnable() {
    @Override
    public void run(ActivityMgr.CPerReq perReq) {
    instalFn.run();
    if (perReq.grantResults[0] != PackageManager.PERMISSION_GRANTED) {
    Log.e(TAG, "installApk: reject permission, kill pid, " + perReq);
    }
    }
    };
    String[] permissions = new String[]{Manifest.permission.REQUEST_INSTALL_PACKAGES};
    ActivityMgr.getIns().reqPermissions(permissions, ActivityMgr.EPerCode.InstallApk, perRunnable);
    } else {
    instalFn.run();
    }
    }
    };

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 8.0 需要开启安装未知来源
    boolean canInstall = context.getPackageManager().canRequestPackageInstalls();
    if (canInstall) {
    instalFn.run();
    } else {
    // 这个 activity 跳转 调整是自己封装的, 就不贴出来
    ActivityMgr.ActRunnable actRunnable = new ActivityMgr.ActRunnable() {
    @Override
    public void run(ActivityMgr.CActReq actReq) {
    Log.d(TAG, "actRunnable, " + actReq);
    boolean canInstall2 = context.getPackageManager().canRequestPackageInstalls();
    if (canInstall2) {
    checkPermissionFn.run();
    } else {
    installApk(context, filePath); // 不肯开启就来个死循环, ^_^
    }
    }
    };
    Uri packageURI = Uri.parse("package:" + context.getPackageName()); // 直接跳转到前应用
    Intent intent = new Intent(Settings.ACTION_MANAGE_UNKNOWN_APP_SOURCES, packageURI);
    ActivityMgr.getIns().startActForResult(intent, ActivityMgr.EActCode.UnknownAppSources, actRunnable);
    }
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 6.0+ 需要运行时检查权限
    checkPermissionFn.run();
    } else {
    instalFn.run();
    }
    }
    • context: 主 Activity
    • filePath: 应用持久化路径, 也就包路径的相对路径, 比如传入: apks/rmg.apk, 就可以安装一下文件
  4. 测试. 执行: Tools.installApk(MainActivity.this, "apks/rmg.apk");

    ps: 模拟是 5.1.1 的, 所以没有 检查未知来源检查权限, 换成 8.0+ 就有了.