android - Android 应用程序可以将自身复制到 Root设备上的系统目录吗?

标签 android linux adb root su

我有一些应用程序需要安装到系统目录。我见过几种使用 adb 命令 的方法。我认为这必须使用 pc 上的 adb shell 和 USB 电缆来完成。但是有什么方法可以让应用程序在不需要 pc 的情况下将自身从 Data 复制到 system 目录?

据我所知,我们可以使用 Runtime 在 android 应用程序中运行命令。所以,我在 onCreate 中尝试了这段代码,但它没有复制 .apk 文件。这是代码:

boolean isSystemApp = (getApplicationInfo().flags
            & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0;
    if (!isSystemApp) {
        Toast.makeText(getApplicationContext (), "not system app", Toast.LENGTH_LONG).show();
        try {

            String [] commandsOldVersions = {"adb shell\n", "su\n", "mount -o rw,remount /system\n",
                    "adb push eyedetection.apk /sdcard/\n", "adb shell\n","su cd /sdcard\n"
            , "mv eyedetection.apk /system/app\n","su chmod 644/system/app/eyedetection.apk\n" };

            String [] commandsNewVersions = {"adb shell\n", "su\n", "mount -o rw,remount /system\n",
                    "adb push eyedetection.apk /sdcard/\n", "adb shell\n","su cd /sdcard\n"
            , "mv eyedetection.apk /system/priv-app\n", "su chmod 644/system/priv-app/eyedetection.apk\n" };

            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                // for 4.3 and higher versions, execute the following line (because they've changed their system files hierarchies):
                Process process = Runtime.getRuntime().exec(commandsNewVersions);
                BufferedReader br= new BufferedReader(new InputStreamReader(process.getInputStream()));
                Log.i("cmd", br.readLine());

            }//end if
            else {
                //for older version, execute this line instead
                Process process= Runtime.getRuntime().exec(commandsOldVersions);
                BufferedReader br= new BufferedReader(new InputStreamReader(process.getInputStream()));
                Log.i("cmd", br.readLine());
            }//end else.

            Runtime.getRuntime().exec("adb reboot");


        }//end try.

        catch (IOException e) {
            Log.i("eyedetection", "error in executing adb commands");
        }//end catch.
    }//end if the app is not system app.

    else {
        Toast.makeText(getApplicationContext (), "It's system app", Toast.LENGTH_LONG).show();

    }

应用程序没有崩溃,但我在 logcat 中得到了这些行:

03-24 22:28:37.661 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve virtual method 143: Landroid/app/Notification$Builder;.setPriority (I)Landroid/app/Notification$Builder;
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x6e at 0x0042
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection I/dalvikvm: Could not find method android.app.Notification$Builder.setLocalOnly, referenced from method com.google.android.gms.common.os.b
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve virtual method 142: Landroid/app/Notification$Builder;.setLocalOnly (Z)Landroid/app/Notification$Builder;
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x6e at 0x00ce
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection E/dalvikvm: Could not find class 'android.app.Notification$BigTextStyle', referenced from method com.google.android.gms.common.os.b
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve new-instance 42 (Landroid/app/Notification$BigTextStyle;) in Lcom/google/android/gms/common/os;
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x22 at 0x00d7
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: couldn't find field Landroid/app/Notification;.extras
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve instance field 10
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x54 at 0x00ed
03-24 22:28:37.661 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: unable to opt direct call 0x0084 at 0x4c in Lcom/google/android/gms/common/os;.b
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: unable to opt direct call 0x0084 at 0xd9 in Lcom/google/android/gms/common/os;.b
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection E/dalvikvm: Could not find class 'android.app.AppOpsManager', referenced from method com.google.android.gms.common.ou.a
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve check-cast 30 (Landroid/app/AppOpsManager;) in Lcom/google/android/gms/common/ou;
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x1f at 0x0010
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection I/dalvikvm: Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.ou.a
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve virtual method 430: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
03-24 22:28:37.669 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x6e at 0x000d
03-24 22:28:37.677 22576-22576/com.project.android.eyedetection E/dalvikvm: Could not find class 'android.os.UserManager', referenced from method com.google.android.gms.common.ou.p
03-24 22:28:37.677 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve check-cast 235 (Landroid/os/UserManager;) in Lcom/google/android/gms/common/ou;
03-24 22:28:37.677 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x1f at 0x000e
03-24 22:28:37.677 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_64_BIT_ABIS
03-24 22:28:37.677 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve static field 119 (SUPPORTED_64_BIT_ABIS) in Landroid/os/Build;
03-24 22:28:37.677 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x62 at 0x000d
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_ABIS
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve static field 120 (SUPPORTED_ABIS) in Landroid/os/Build;
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x62 at 0x0008
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_32_BIT_ABIS
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve static field 118 (SUPPORTED_32_BIT_ABIS) in Landroid/os/Build;
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection D/dalvikvm: VFY: replacing opcode 0x62 at 0x0008
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection D/dalvikvm: DexOpt: couldn't find static field Landroid/os/Build;.SUPPORTED_64_BIT_ABIS
03-24 22:28:37.685 22576-22576/com.project.android.eyedetection W/dalvikvm: VFY: unable to resolve static field 119 (SUPPORTED_64_BIT_ABIS) in Landroid/os/Build;

还有这些行:

03-24 22:29:54.388 23305-23305/com.project.android.eyedetection E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services.  Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
03-24 22:29:54.388 23305-23305/com.project.android.eyedetection E/GMPM: Scheduler not set. Not logging error/warn.
03-24 22:29:54.403 23305-23335/com.project.android.eyedetection E/GMPM: Uploading is not possible. App measurement disabled

我无法从 logcat 中找出问题所在。为什么代码不起作用?是否可以让应用程序将自身复制到 root 设备上的另一个文件夹,或者我们必须使用 usb 电缆?

最佳答案

'adb xxx'命令只能在PC shell中使用,不能在Android/Linux shell中使用。
也就是说,你应该使用

  • Runtime.getRuntime().exec("reboot")

代替

  • Runtime.getRuntime().exec("adb reboot")

关于android - Android 应用程序可以将自身复制到 Root设备上的系统目录吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36214776/

相关文章:

android - 使用 ListView-Adapter 预加载更多 View

android - ActionBarDrawer切换动画

android - 如何将信息传递到自定义 ArrayAdapter 的 getView 中?

linux - Zynq 的交叉编译内核模块

c - Linux semaphore.h 挂起/等待队列策略

php - PHP 中的 Android 屏幕截图

android - 无法从 adb shell 发送 android.intent.action.BOOT_COMPLETED

java - Android 中的关闭音频问题

linux - jinja2.exceptions.TemplateSyntaxError : expected token ',' , 得到 'integer' (支持十六进制、八进制和二进制整数文字)

java - 如何通过Java文件运行adb命令?