⬆️ ⬇️

Auto-generating APK with various data from the command line using Ant

Recently I ran into a problem - it was necessary to generate an apk file on the remote server for download, and depending on the server address being transmitted, the program had to connect to different servers during installation by default.



So the task is to give the user the opportunity to download from the Internet a dynamically generated Android application that will behave differently depending on the parameters transferred (in this case, different data download servers).





')

Perhaps this task will seem easy to many, but I came across this for the first time and I hope that the information will be useful to someone.



First, set up Java and Ant, Android SDK on your remote server. Make sure that the paths to them are specified or enter the full path when launching applications from these packages. Copy the project source to the server.



Command order




I give this example for Ubuntu. For a Windows server, you only need to replace the command for creating a file on the command line.



Create a batch file that will generate an apk file with variable server addresses. Naturally you can use it for other variables.



At first you create a file of your data and specified the folder / assets in your project as a data path ...



cat<<F>__/_.txt

,

F




Generate the build.xml file in the project root, and the project name <project_name> you can specify any, it will be the name of the apk file. If you need to make different releases, then you can point for example project_name_release_number_version_number, etc.



android update project --name <project_name> --path <path_to_your_project>



Then run the command from the project root.



ant release



as a result, you will get a ready apk in the / bin directory. In this case, you will receive a file with the suffix -unsigned.apk, since you have not signed your application. To sign your application you need to make sure that the path to your certificate store is specified in the local.properties file. How to create a certificate for signing your application is described here . This is beyond the scope of this note.



key.store=/Path/to/my/keystore/MyKeystore.ks

key.alias=myalias




To be sure, we can insert the code generating this file.



cat<<F >_/local.properties

# This file is automatically generated by Android Tools.

# Do not modify this file -- YOUR CHANGES WILL BE ERASED!

#

# This file must *NOT* be checked in Version Control Systems,

# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant

# For customization when using a Version Control System, please read the

# header note.

sdk.dir=/__sdk/Android-sdk

key.store=/Path/to/my/keystore/MyKeystore.ks

key.alias=myalias

F





In your Java code, you get the data you need to work from the generated file in / assets. Use them as you need. In my case, I just read the address of the server from which to download.

User for download give a link to the finished apk.



The complete batch file is shown below. Data paths and file names you can transfer to the script / batch file using parameters.



ATTENTION! In the text of the note, everywhere EOF is written with the help of Russian letters E and O - otherwise it was incorrectly displayed.



cat<<F >__/_.txt

,

F

android update project --name <project_name> --path <path_to_your_project>

cat<<F>_/local.properties

# This file is automatically generated by Android Tools.

# Do not modify this file -- YOUR CHANGES WILL BE ERASED!

#

# This file must *NOT* be checked in Version Control Systems,

# as it contains information specific to your local configuration.

# location of the SDK. This is only used by Ant

# For customization when using a Version Control System, please read the

# header note.

sdk.dir=/__sdk/Android-sdk

key.store=/Path/to/my/keystore/MyKeystore.ks

key.alias=myalias

F

ant release

Source: https://habr.com/ru/post/122528/



All Articles