【问题标题】:Can an Android app copy itself to the system directory on a rooted device?Android 应用程序可以将自身复制到有根设备上的系统目录吗?
【发布时间】:2016-03-25 05:52:59
【问题描述】:

我有一些应用程序需要安装在system directory 上。我见过几种使用adb commands 的方法。我认为这必须使用 pc 上的 adb shell 和 USB 电缆来完成。但是有没有什么方法可以让应用在不需要电脑的情况下将自己从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 中找出问题所在。为什么代码不起作用?是否可以让应用程序将自身复制到根设备上的另一个文件夹,或者我们必须使用usb 电缆?

【问题讨论】:

  • 除非您有 root 权限,否则您无法访问系统目录。
  • root 访问是什么意思?我的设备已root,我可以运行su,还有其他要求吗?谢谢。
  • @Shahzeb 我认为他明确表示他可以使用 adb 手动完成,但需要以编程方式完成。即应用程序自行完成。
  • @Shahzeb,谢谢,但正如 Usama 所说,我需要以编程方式进行。据我所知,有一个应用程序可以让您将任何应用程序转换为系统应用程序,我认为它称为 ES File Explorer,所以也许有一些编程方法。有什么办法吗?
  • @UsamaZafar 是的,我正在寻找什么。有没有办法从应用程序本身做到这一点?谢谢。

标签: android linux adb root su


【解决方案1】:

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

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

而不是

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

【讨论】:

  • 谢谢,我已经删除了所有的 adb 关键字,但仍然无法正常工作。该应用程序没有崩溃,但我得到了问题中发布的 logcat 中说明的错误。你能告诉我这些错误是否相关,我该如何解决?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-28
  • 1970-01-01
  • 2012-07-12
  • 1970-01-01
  • 2015-08-23
  • 2017-11-17
  • 1970-01-01
相关资源
最近更新 更多