Installing apk on android device via ADB with Java program on Linux

↘锁芯ラ 提交于 2020-01-03 05:17:31

问题


I am trying to pass a path from Java's inbuilt file manager to ADB with java program on Linux to install apk on android device. When the code is executed the apk selected using file manager never gets installed.

Here is the code:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
        "APK Files", "apk");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if(returnVal == JFileChooser.APPROVE_OPTION) {
    System.out.println("You choose to open this file: " + chooser.getSelectedFile().getName());
    File file = new File("");
    System.out.println(file.getAbsolutePath().toString());

    try {
        Process p1 = Runtime.getRuntime().exec("adb kill-server"); //for killing old adb instance
        Process p2 = Runtime.getRuntime().exec("adb start-server");
        Process p3 = Runtime.getRuntime().exec("adb install \"" + file.getAbsolutePath() + "\"");
        p3.waitFor();
        Process p4 = Runtime.getRuntime().exec("adb kill-server");
    } catch (Exception e1) {

        System.err.println(e1);
    }

The following code should install the apk:

Process p3 = Runtime.getRuntime().exec("adb install \"" + file.getAbsolutePath() + "\"");

回答1:


I figured it out myself, and here is the code:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("APK Files", "apk");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(getParent());
if (returnVal == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    String filename = chooser.getSelectedFile().getName();
    try {
        String[] commands = new String[3];
        commands[0] = "adb";
        commands[1] = "install";
        commands[2] = file.getAbsolutePath();
        Process p1 = Runtime.getRuntime().exec(commands, null);
        p1.waitFor();
                            } catch (Exception e1) {
        System.err.println(e1);
    }
}



回答2:


Change the line

File file = new File("");

to

File file = chooser.getSelectedFile();

Also, don't forget to check

if(file.exists()) {

to validate the file.




回答3:


I've spent a week working towards the same task for window system I have found out a simple solution to do this task, Here are some of the following steps that I have applied in my project

  1. The First step is to download the ADB tool (Known as platform-tools) from this URL, and extract the downloaded file into your workspace directory.

  2. Open the directory you downloaded the platform tools into

  3. Create the Batch file if you don't aware how to create batch file follow the following steps

    i. Open your text editor notepad or notepad++

    ii. Save it as xyz.bat then it will be treated by the window system as a batch file

  4. Open your batch file in your text editor and paste the following command

    adb install "b2c.apk" && adb shell am start -n com.xyz.app/com.xyz.b2c.Activity.SplashScreen**
    

    (Here there are two ADB command which separated by the ampersand sign. The first command is for installing the APK in your Android devices and the second one is to open the application )

    i. b2c.apk is my android APK Which I want to install on my phone

    ii. com.xyz.app is an android application package name and com.xyz.b2c.Activity.SplashScreen is an activity package name that I want to open

  5. Save the file and close it, and then copy that file and paste it in the directory with the platform tools. And don't forget to place your APK file in this directory too

cd into the platform directory, and run this Java program:

import java.io.DataInputStream;
import java.io.File;
import java.io.InputStreamReader;
import java.util.Scanner;

public class TestClass {
    static int progress = 0;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        try {   
            String ls_str;   
            Process p =  Runtime.getRuntime().exec("cmd /c xyz.bat", null, new File("E:\\Arun_Java_Workspace\\TestApplication\\platform-tools"));
            DataInputStream ls_in = new DataInputStream( 
            p.getInputStream()); 
            while ((ls_str = ls_in.readLine()) != null) { 
                System.out.println(ls_str);
            } 

        } catch (Exception e) {
            System.out.println("Exception e: " + e);
        }

    }

}


来源:https://stackoverflow.com/questions/35824445/installing-apk-on-android-device-via-adb-with-java-program-on-linux

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