Java应用程序在Android APK安装
我试图使一个简单的Java应用程序在通过USB连接的Android设备上安装APK。使用手动ABD然后一切工作正常,但我想给一个简单的单按钮,单击我的应用程序中的安装选项,但是由于某些原因,code心不是工作:
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);
这个过程用141,但没有发现其他错误,我可以看到退出,但是当我看到平板电脑上的apk文件还没有安装。我已经检查,以确保有其根源,并支持第三方应用等设备上的空间,所以我敢肯定的问题是与我的Java,而不是Android设备(如我说,如果我运行ADB安装-r自己从终端然后一切工作正常)。
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).
我寻觅各地计算器,但只找到线程上的Android应用程序内安装APK,而不是从桌面Java应用程序。
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.
感谢您的帮助;
编辑:新的code看起来像这样现在的ProcessBuilder管理亚行呼吁:
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);
apk的同步,只不过没有马上为1的状态返回到控制台。
The apk sync just fails straight away with a status of 1 being returned to the console.
如果我手动指定的apk位置
If I manually specify the apk location
ProcessBuilder apksync = new ProcessBuilder("adb","install","/home/geeky/Desktop/1.apk");
然后我得到了同样的错误作为原来的code,过程返回状态141并在一段时间后同步过程完成,但表上的.apk文件的心不是可用。我也得到了同样的结果,如果我试图用一个APK远小于700MB我的应用程序(如在一个500KB的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:我也试图改变命令推APK和它的作品没有错误,APK将推动无问题/mnt/sdcard/test/1.apk。
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.
刘德华
我想这是因为不能够解决亚行二进制Java应用程序。尝试在环境变量中添加亚行二进制使其从任何地方访问。
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.
试试这个:
ProcessBuilder pb = new ProcessBuilder("cmd","arg1");
pb = pb.redirectErrorStream(true);
Process proc = pb.start();
InputStream is = proc.getInputStream();