Home > Mobile >  Silently Install APK in Android Programmatically
Silently Install APK in Android Programmatically

Time:01-16

@Override
        protected Boolean doInBackground(String... strings) {
            try {
                File path = context.getFilesDir();
                File outputFile = new File(path, fileName   ".apk");
                int repetition = 1;
                while (outputFile.exists()) {
                    outputFile = new File(path, fileName   " ( "   repetition   ").apk");
                    repetition  ;
                }

                if (!path.exists()) {
                    path.mkdirs();
                }

                URL url = new URL(downloadUrl);
                HttpURLConnection c = (HttpURLConnection) url.openConnection();
                c.setRequestMethod("GET");
                c.connect();

                InputStream inputStream = c.getInputStream();
                float totalSize = (float) c.getContentLength();

                Pair<Integer, InputStream> bitmapToStream = Pair.create((int) totalSize, inputStream);
                InputStream in = bitmapToStream.second;
                OutputStream out = new FileOutputStream(outputFile);

                long fileSize = bitmapToStream.first;
                byte[] buf = new byte[(int) fileSize];
                int len;
                float per;
                float downloaded = 0f;

                while ((len = in.read(buf)) > 0){
                    out.write(buf,0,len);
                    downloaded  = len;
                    per = (downloaded * 100 / totalSize);
                    publishProgress((int) per);
                }

                out.close();
                in.close();

                installAPK(path, outputFile.getName());
                updatePatchListener.onDone();

//                openNewVersion(outputFile.getPath());
                return true;
            } catch (Exception exception) {
                this.exception = exception;
            }

            return false;
        }

        private void installAPK(File path, String apkName) {
            try {
                Process p = new ProcessBuilder("pm install -r "   apkName).directory(path).start();
                p.waitFor();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

I want to install the apk that I have downloaded silently, but always get the error permission denied..

I already use command pm install or adb install or adb shell or run-as, but all not working for me. Always permission denied

is there any solution for me..?

2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err: java.io.IOException: Cannot run program "pm install -r Update ( 3).apk" (in directory "/data/user/0/com.c.aka/files"): error=13, Permission denied
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at java.lang.ProcessBuilder.start(ProcessBuilder.java:1050)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at com.c.cpayid.UpdatePatch$DownloadNewVersion.installAPK(UpdatePatch.java:129)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at com.c.cpayid.UpdatePatch$DownloadNewVersion.doInBackground(UpdatePatch.java:115)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at com.c.cpayid.UpdatePatch$DownloadNewVersion.doInBackground(UpdatePatch.java:54)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at android.os.AsyncTask$2.call(AsyncTask.java:333)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at java.util.concurrent.FutureTask.run(FutureTask.java:266)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err:     at java.lang.Thread.run(Thread.java:764)
2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err: Caused by: java.io.IOException: error=13, Permission denied

2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err: at java.lang.UNIXProcess.forkAndExec(Native Method) 2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err: at java.lang.UNIXProcess.(UNIXProcess.java:133) 2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err: at java.lang.ProcessImpl.start(ProcessImpl.java:132) 2023-01-15 14:14:21.787 12507-12558/com.c.aka W/System.err: at java.lang.ProcessBuilder.start(ProcessBuilder.java:1029)

this error show

CodePudding user response:

You cannot install an APK silently. Imagine if any piece of malware could download an APK and install it to hide itself and include additional pieces of malware on a device. That would be bad. That's why no app can install others without the user intervening.

If you're the device owner and trying to install apps across your fleet, look at Android Enterprise. That can install apps. But there's no way to do it programatically.

CodePudding user response:

The error message, "Permission denied" suggests that your app does not have the necessary permissions to execute the "pm install" command. In order to silently install an APK on Android programmatically, your app must have "android.permission.INSTALL_PACKAGES" permission.

You can add this permission in your AndroidManifest.xml file like this:

<uses-permission android:name="android.permission.INSTALL_PACKAGES" />

Also you may need to run this command with root access.

  • Related