69558

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

Question:

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() + "\"");

Answer1:

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); } }

Answer2:

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.

Answer3:

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

<ol><li>

The First step is to download the ADB tool (Known as platform-tools) from <a href="https://developer.android.com/studio/releases/platform-tools" rel="nofollow">this URL</a>, and extract the downloaded file into your workspace directory.

</li> <li>

Open the directory you downloaded the platform tools into

</li> <li>

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

</li> <li>

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

</li> <li>

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

</li> </ol>

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); } } }

Recommend

  • How does one pickle arbitrary pytorch models that use lambda functions?
  • Why can I process a large file only when I don't fix HDF5 deprecation warning?
  • Multiplying Matrices in One-Dimensional Arrays
  • Typescript negative type check
  • Powershell - Create a folder from a file name, then place that file in the folder
  • In C++ template copy assignment operator not compatible with initializer_list?
  • java.security.InvalidAlgorithmParameterException: parameter object not a ECParameterSpec
  • Is there a way to assign a multi-line string to a object property?
  • ANSI escape sequence save/restore cursor position support
  • Ruby: Prawn PDF out of memory when using the group method
  • How to programatically change the startColor attribute of gradient - Android
  • In JQuery ajax, how do I handle a HTTP 408 error properly?
  • Obscure error says my component name begins with a zero
  • How to get pip to point to newer version of Python
  • Generic Swift function to test for object type
  • Can Facebook JS SDK work with Phonegap / Cordova?
  • When is quoting necessary in prepared statements of pdo in PHP?
  • Making query to find nearest multiple(Lat,Long) from the single(Lat,Long)
  • plot dirac function in matlab
  • How can I run an Illustrator javascript on all files in a directory?
  • Use awk to convert GPS Position to Latitude & Longitude
  • Corda: How to implement hierarchical relationships between state data persisted to H2
  • Wicket - getting body of markup element
  • jQuery Ajax call to WCF service returning “Method not allowed (405)”
  • Symfony - Setting Cookie onKernelRequest
  • How to display converted time zones in a 'generic week' (Sunday thru Saturday)?
  • Keep rows with certain values always at the bottom while sorting in jquery tablesorter plugin
  • Possible to set default CloudKit container not based on application name?
  • How to make 100% div height between header and footer?
  • C++ STL stack pop operation giving segmentation fault
  • Comparing variables with strings bash
  • Drag and drop unicode TText in DelphiXe4
  • Comma decimal separator is ignored by ASP.NET MVC model binder
  • How to get rgb from transparent pixel in js
  • Background transfer download task failed when app was closed
  • Write to .csv file with PHP (Commas in Data Error)
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}