0

I have some app that needs to be installed on the system directory. I've seen several ways which use adb commands to do so. I think this must be done using the adb shell on the pc and a usb cable. But is there any way for the app to copy itself from Data to system directory without needing a pc?

As I know we can run commands within android app using Runtime. So, I tried this code within onCreate, but it doesn't copy the .apk file. This is the code:

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();

    }

The app doesn't crash, but I get these lines in the 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;

Also these lines:

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

I couldn't figure out the problem from the logcat. Why the code doesn't work? Is it possible to let the app copy itself to another folder on a rooted device, or must we use usb cable?

Dania
  • 1,648
  • 4
  • 31
  • 57
  • You cannot access system diectory unless you have root access. – Shahzeb Mar 25 '16 at 05:55
  • What do you mean by root access? My device is rooted and I can run su, is there any other requirement? thanks. – Dania Mar 25 '16 at 05:56
  • 1
    @Shahzeb I think he explicitly said that he is able to do it manually using adb but needs to do it programatically. i.e. the app does it by itself. – usamazf Mar 25 '16 at 06:06
  • @Shahzeb, thanks but as Usama said, I need that to happen programmatically. As I know there is an app that let you convert any app to a system app, I think it's called ES File Explorer, so maybe there is some method programmatically. Is there anyway for that? – Dania Mar 25 '16 at 06:18
  • @UsamaZafar yes that what's I'm looking for. Is there any way to do it from the app itself? Thanks. – Dania Mar 25 '16 at 06:20
  • @Dania look at this thread. Hope it helps. http://stackoverflow.com/questions/10691485/android-programmatically-copying-apk-to-system-app – usamazf Mar 25 '16 at 06:26
  • @UsamaZafar thanks a lot I will check it. – Dania Mar 25 '16 at 06:39
  • When you use `exec(commands)` you run those commands directly in the `shell`, so you cannot use `adb` commands (which run on the PC). Remove all your `adb` commands and use "regular" linux command only to copy the `APK`. – TDG Mar 25 '16 at 07:51
  • @TDG thanks a lot, I'll try that and let you know – Dania Mar 25 '16 at 10:34

1 Answers1

1

The 'adb xxx' command can only be used in a PC shell, not in Android/Linux shell.
That is, you should use

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

instead of

  • Runtime.getRuntime().exec("adb reboot").
Swing
  • 858
  • 1
  • 8
  • 21
  • Thanks, I've removed all adb keywords, but still it's not working. The app doesn't crash, but I get the errors illustrated in the logcat posted in the question. Can you please tell me if these errors are related, and how I can solve them? – Dania Mar 26 '16 at 10:34