简体   繁体   中英

Java Application to install APK on android

I am trying to make a simple application in Java to install an APK on android devices connected via USB. Using ABD manually then everything works fine, but I wanted to give a simple single button click install option within my application, but for some reason the code isnt working:

    try {
        abdsourcesync = apkpath;
        progress.setString("sync in progress");
        System.out.println("Starting Sync via adb with command " + "adb"
                + " install -r " + apkpath);

        Process process = Runtime.getRuntime().exec(
                "adb" + " install -r " + apkpath);
        InputStreamReader reader = new InputStreamReader(
                process.getInputStream());
        Scanner scanner = new Scanner(reader);
        scanner.close();
        int exitCode = process.waitFor();
        System.out.println("Process returned: " + exitCode);

The process exits with a status of 141 but no other errors that I can see, but when I look on the tablet the .APK has not installed. I have checked to make sure there is space on the device which is rooted and supports third party apps etc, so I am certain the issue is with my java and not the android device (as I said if I run the ADB install -r myself from terminal then all works fine).

I have searched around stackoverflow but have only found threads on installing an APK from within an Android application, not from a desktop Java application.

Thanks for the help;

EDIT: New code looks like this now with ProcessBuilder managing the adb call:

        try {
            abdsourcesync = apkpath;
            progress.setString("sync in progress");
            System.out.println("Starting Sync via adb with command " + "adb"
                    + " install -r " + apkpath);

            ProcessBuilder apksync = new ProcessBuilder("adb",  " install -r ",  apkpath);

            apksync = apksync.redirectErrorStream(true);
/*          Process process = Runtime.getRuntime().exec(
                    "adb" + " install -r " + apkpath);*/
            Process process = apksync.start();
            InputStreamReader reader = new InputStreamReader(
                    process.getInputStream());
            Scanner scanner = new Scanner(reader);
            scanner.close();
            int exitCode = process.waitFor();
            System.out.println("Process returned: " + exitCode);

The apk sync just fails straight away with a status of 1 being returned to the console.

If I manually specify the apk location

ProcessBuilder apksync = new ProcessBuilder("adb","install","/home/geeky/Desktop/1.apk");

then I get the same error as the original code, the process returns status 141 and after a period of time the sync process completes but the .apk isnt available on the table. I also get the same result if I try with an APK much smaller than my 700mb app (as in a 500kb .apk gives the same results).

EDIT3: I also tried changing the command to push the apk and it works without error, the apk will push to /mnt/sdcard/test/1.apk without issue.

Andy

i guess it is because your java application not being able to resolve the adb binary. try adding the adb binary in your environment variable so that it is accessible from anywhere.

try this:

    ProcessBuilder pb = new ProcessBuilder("cmd","arg1");
pb = pb.redirectErrorStream(true);
    Process proc = pb.start();
    InputStream is = proc.getInputStream();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM