how to get access of android root directory (/syst

2019-04-02 20:08发布

问题:

I have an apk file in asset folder.
Now I need to create a program which will install that apk file as system application.
Manually it is possible by copying that apk file into sdcard with following steps,

$ adb push MyApk.apk /sdcard/ 
$ adb shell
$ su
$ mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
$ cat /sdcard/MyApk.apk > /system/app/MyApk.apk
$ pm install /system/app/MyApk.apk

But is it possible to do through code?

回答1:

Refer the below code to move user app apk into system app apk in rooting device with the help of RootTools method .

     PackageInfo paramPackageInfo = null;
        try {
            paramPackageInfo = this.getPackageManager().getPackageInfo(
                    this.getPackageName(), 0);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }

        ApplicationInfo localApplicationInfo = paramPackageInfo.applicationInfo;

String str1 = "/system/app/" + localApplicationInfo.packageName
                + ".apk";
        String str2 = "busybox mv " + localApplicationInfo.sourceDir + " "
                + str1;

        RootTools.remount("/system", "rw");
        RootTools.remount("/mnt", "rw");

        CommandCapture command = new CommandCapture(0, str2,
                "busybox chmod 644 " + str1);

        try {
            RootTools.getShell(true).add(command).waitForFinish();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } catch (RootDeniedException e) {
            e.printStackTrace();
        }

        RootTools.remount("/system", "ro");
        RootTools.remount("/mnt", "ro");

Necessary of Busybox and superuser app while use the above code in your application.