Install a APK and Launch App from Shell script

Rishikesh pathak picture Rishikesh pathak · Dec 6, 2017 · Viewed 7k times · Source

My Requirement is : App A will Run shell script(myshellscript.sh) that is already in "system/bin" location and shell script will install App B that is stored in "sdcard/Download" location and will launch App B.

So Before continuing further i want to tell you that

  1. my device is already rooted as i have custom ROM flashed in it
  2. App A is a system app having system privilege.
  3. My script is working fine as per requirement when i am running my script by command : adb shell sh system/bin/myshellscript.sh
  4. I dont have that much knowledge of shell script.

below is my shell script:

#!/bin/bash 
echo "Shell script works on Android"
pm install -r "/sdcard/Download/SampleApplication.apk";
echo "Going to sleep for 15 sec"
sleep 15;
echo "woked up after 15 sec"
am start -n "com.aaa.sampleapplication/.MainActivity";
sleep 5;

So issue is when i am running this script by command mentioned above its working fine but when running same script programmatically on button click of App A all commands written in script is working except pm install -r "/sdcard/Download/SampleApplication.apk"; Code i am trying to run my script is :

            try
            {
                Runtime rt = Runtime.getRuntime();

                Process proc = rt.exec("sh /system/bin/myshellscript.sh");
                InputStream is = proc.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;

                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (Throwable t)
            {
                t.printStackTrace();
            }

UPDATE: I captured adb log and getting below exception:

AndroidRuntime: Calling main entry com.android.commands.pm.Pm
11-19 00:37:50.867  7887  7887 E Pm      : Error
11-19 00:37:50.867  7887  7887 E Pm      : java.lang.NullPointerException
11-19 00:37:50.867  7887  7887 E Pm      :  at android.os.Parcel.readException(Parcel.java:1690)
11-19 00:37:50.867  7887  7887 E Pm      :  at android.os.Parcel.readException(Parcel.java:1637)
11-19 00:37:50.867  7887  7887 E Pm      :  at android.content.pm.IPackageInstaller$Stub$Proxy.createSession(IPackageInstaller.java:249)
11-19 00:37:50.867  7887  7887 E Pm      :  at com.android.commands.pm.Pm.doCreateSession(Pm.java:552)
11-19 00:37:50.867  7887  7887 E Pm      :  at com.android.commands.pm.Pm.runInstall(Pm.java:392)
11-19 00:37:50.867  7887  7887 E Pm      :  at com.android.commands.pm.Pm.run(Pm.java:142)
11-19 00:37:50.867  7887  7887 E Pm      :  at com.android.commands.pm.Pm.main(Pm.java:99)
11-19 00:37:50.867  7887  7887 E Pm      :  at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
11-19 00:37:50.867  7887  7887 E Pm      :  at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:277)
11-19 00:37:50.869  7887  7887 I art     : System.exit called, status: 1

Answer

Os Tiger picture Os Tiger · Dec 13, 2017

When execute a script by adb shell, it runs under shell permission. Shell has higher permission than you app which is sandboxed.