0

I am trying to sign apk by using apksigner and zipalign tools, which are located in jdk/bin folder. I have already set path environment variable to it. Now what I want to do is, execute some shell commands which signs my apk through java code in ubuntu. Commands are,

command-1 :

keytool -genkey -v -keystore /Desktop/my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias app

This command required some inputs at run time, like first name last name etc.

command-2 :

./zipalign -v 4 inputAPK.apk outputAPK.apk

command-3 :

apksigner sign --min-sdk-version 15 --ks my-release-key.jks outputAPK.apk

command-4 :

apksigner verify --min-sdk-version 15 outputAPK.apk

All above 4 commands I want to execute it through java code in ubuntu environment, how can I achieve it?

Tushar Deshpande
  • 448
  • 2
  • 9
  • 28
  • Whats the problem that you're facing? – StackUseR Nov 25 '16 at 11:51
  • Actually I am new to java programming, so don't know the exact solution for it, I mean how can I execute above 4 commands through java program and also at runtime how can I send inputs to the command line in ubuntu. – Tushar Deshpande Nov 25 '16 at 11:56
  • 1
    Possible duplicate of [Run cmd commands through java](http://stackoverflow.com/questions/15464111/run-cmd-commands-through-java) – Alessandro Da Rugna Nov 25 '16 at 13:16
  • I tried with the link you suggested but while running second command getting exception like, java.io.IOException: Cannot run program "zipalign -v 4 myInput.apk myOutput.apk": error=2, No such file or directory – Tushar Deshpande Nov 26 '16 at 05:30

1 Answers1

1

Oh k. You can use java.lang.Runtime.exec to run simple code. Have a look at these sites:

http://docs.oracle.com/javase/7/docs/api/java/lang/Runtime.html#exec(java.lang.String)

http://alvinalexander.com/java/edu/pj/pj010016

https://www.mkyong.com/java/how-to-execute-shell-command-from-java/

Also you can use Process.getOutputStream() method to get input. Whatever data you enter will be passed to your input stream.

StackUseR
  • 884
  • 1
  • 11
  • 40
  • I tried with the link you suggested but while running second command getting exception like, java.io.IOException: Cannot run program "zipalign -v 4 myInput.apk myOutput.apk": error=2, No such file or directory – Tushar Deshpande Nov 26 '16 at 05:31
  • In that case either you are pointing to a wrong location as the error says... or you need to do this - `sudo apt-get update sudo apt-get install gcc-multilib lib32z1 lib32stdc++6` – StackUseR Nov 26 '16 at 05:39
  • zipalign tool is located in Android/sdk/build tools/24.0.3/zipalign. I have already located this path in environment variable, but even though not working. – Tushar Deshpande Nov 26 '16 at 05:52
  • How about your OS? x64 or x32.? Also check your build.xml. – StackUseR Nov 26 '16 at 05:59
  • Hey! [this post](http://stackoverflow.com/questions/18041769/error-cannot-run-aapt) may help you. – StackUseR Nov 26 '16 at 06:12
  • By the way when you find a answer useful do mark it as accepted or vote it so that people with same problem will find it easy to get solutions. – StackUseR Nov 26 '16 at 06:14