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

╄→尐↘猪︶ㄣ 提交于 2019-12-10 09:45:27

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/47668812/install-a-apk-and-launch-app-from-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!