android-應用內升級安裝apk


title: android-應用內升級安裝apk
categories: Android
tags: [android, apk, 安裝]
date: 2019-11-22 15:04:56
comments: false

直接更新到最新的包, 用於修改底層代碼時的更新. (一般熱更都只是更新邏輯部分和資源, 如: lua, 配置, 美術資源 等, 但是修改到 java, csharp 時就需要更包了)


  1. AndroidManifest.xml 中加入安裝 apk 權限

    <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
    
  2. 安卓 7.0+ 路徑獲取適配

    1. 添加一個 xml 文件, 路徑: res/xml/provider_paths.xml

      <?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

      <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 的 代碼

    總的流程是: 檢查安裝未知來源 -> 檢查權限 -> 執行安裝

    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+ 就有了.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章