android code control installation apk

android code control installation apk

Usually when we use the mall, the installation screen will automatically pop up after downloading the apk, and some mobile phones will also help you check whether the source of the apk is safe.
Since there is a requirement in the project to remotely upgrade the apk, you need to control the installation of the apk in the program you make.
After some online searches, someone suggested the following method. Use PackageInstaller to help install the apk. This is actually a system pre-installed apk, which is the interface we usually use the mall installer to install.
After some inquiries, the following solutions were found online:

        File apk = new File(path);
        Uri uri = FileProvider.getUriForFile(mContext, "com.yanfeng.poc.ota.fileProvider", apk);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, "application/vnd.android.package-archive");
        intent.setClassName("com.android.packageinstaller", "com.android.packageinstaller.PackageInstallerActivity");
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);

Then I found that the following error will be reported:

java.lang.SecurityException: Permission Denial: starting Intent {
    
     act=android.intent.action.VIEW dat=content://com.yanfeng.poc.ota.fileProvider/apk/app-debug.apk typ=application/vnd.android.package-archive flg=0x10000001 cmp=com.android.packageinstaller/.PackageInstallerActivity } from ProcessRecord{
    
    a4b2c99 12176:com.yanfeng.poc.ota/u0a173} (pid=12176, uid=10173) not exported from uid 10046

At first, I thought it was because the application permission level was not enough, but it was not because the system application could not pull up the installed interface. Later, I used am start -n com.android.packageinstaller/.PackageInstallerActivity in adb shell to try to pull up the interface, but it still failed and an error was reported. It is also similar to the words not exported from uid 10046, so after some inquiries, I found that uid is actually an identifier for whether android resources can be shared. If the exported of the activity is set to false in the form file, then processes that do not have the same uid cannot Suspending this activity means that the activity PackageInstallerActivity has exported set to false. It cannot use other applications to pull it up. It can only pull up this activity when its own activity jumps. So I typed pm list package -f | grep install in the adb shell to check the package name and apk path. I found the apk path on the Android 9.0 system board as follows package:/system/priv-app/PackageInstaller
/ PackageInstaller.apk=com.android.packageinstaller
We drag this apk into android studio to view its form file. It has many activities, most of which are exported to false. Among them, I found the
real one we should lift. This activity

is also the default startup activity of this apk.
That is to say, if we can pass the parameters of the apk that needs to be installed to this activity and raise the interface of this activity, we can install the apk.

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

We add the following 3 permissions to the manifest where we need to use program control to install the apk. The code to suspend the activity is as follows

            File apk = new File(path);
            Uri uri = FileProvider.getUriForFile(mContext, "com.yanfeng.poc.ota.fileProvider", apk);
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            intent.setClassName("com.android.packageinstaller", "com.android.packageinstaller.InstallStart");
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            mContext.startActivity(intent);

Then the program was successfully installed

Guess you like

Origin blog.csdn.net/weixin_44280688/article/details/108051006
Recommended