Automatically generate APKs with various data from the command line using Ant

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

    So the task is to give the user the opportunity to download a dynamically generated Android application from the Internet that will behave differently depending on the parameters passed (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, configure Java and Ant, the Android SDK on your remote server. Make sure that the paths to them are spelled out or enter the full path when starting applications from these packages. Copy the project sources to the server.

    Command Execution Order


    I give this example for Ubuntu. For a Windows server, you will only need to replace the file creation command 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 as well.

    First, create a file of your data indicated as the path to the data the / assets folder in your project ... Generate the build.xml file in the root of the project, and the name of the project

    cat<<ЕОF>путь_к_данным/файл_данных.txt
    здесь любые ваши данные, в моем случае просто адрес сервера
    ЕОF


    you can specify any, this will be the name of the apk file. If you need to make various releases, then you can specify for example project_name_release_number_version_number, etc. Then, from the root of the project, run the command as a result you will get the finished 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 written in the local.properties file . How to create a certificate for signing your application is described here . This is beyond the scope of this article. To be sure, we can insert the generation code for this file.

    android update project --name --path



    ant release



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




    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, 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 server address from which I need to boot.
    Give the user a download link to the finished apk.

    The full batch file is shown below. You can pass data paths and file names to a script / batch file using parameters.

    ATTENTION! In the text of the note, everywhere EOF is written using the Russian letters E and O - otherwise it would not be displayed correctly.

    cat<<ЕОF >путь_к_данным/файл_данных.txt
    здесь любые ваши данные, в моем случае просто адрес сервера
    ЕОF
    android update project --name --path
    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

    Also popular now: