Install a APK and Launch App from Shell script [duplicate]
Asked Answered
L

1

3

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
Leaves answered 6/12, 2017 at 7:13 Comment(2)
Can you change rt.exec("sh /system/bin/myshellscript.sh"); to call with separate arguments rt.exec("sh", "/system/bin/myshellscript.sh");Extravagancy
rt.exec() API don't take two string as parameter. but i guess problem is not in running script because my script is running, as i mentioned in question ,all command in script is working except install.Leaves
F
0

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

Formulism answered 13/12, 2017 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.