Android - use ant to create build configurations that change configuration values

前端 未结 4 1439
春和景丽
春和景丽 2020-12-05 01:19

What I want is a way to have settings that are dependent on build configuration. To give a specific example, my android application connects to a web service. In developme

相关标签:
4条回答
  • 2020-12-05 01:51

    As answers mentioned above says, you have to place the URLs in a property file like dev.properties, test.properties, prod.properties etc..

    Now only thing that you need to do is making your build intelligent enough to choose a property file depending upon environment.

    That can be done by passing a parameter to ANT, something like:

    $ ant -file MyBuild.xml -DcurrentEnv=dev (For Development environment)
    $ ant -file MyBuild.xml -DcurrentEnv=test (For Test)
    $ ant -file MyBuild.xml -DcurrentEnv=prod (For Production)

    Inside your build script, this is how you can include your property file:

    <target name="jarMe">
        <jar destfile="sample.jar" basedir="src" includes="${currentEnv}.properties"/>
    </target>
    

    With this in place, whatever name you supply at the time of build, property file with that name will be picked up.

    0 讨论(0)
  • 2020-12-05 01:52

    I would start by placing the urls into a properties file that you can then place onto the classpath. Make a test and a production properties file. Then depending on the build place the correct file onto the classpath and pull the properties at runtime.

    0 讨论(0)
  • 2020-12-05 01:59

    Found a tutorial which goes through all the details of using ant to automate a build system, to create and use build configurations, as well as to build the release project with one command. Here it is: http://www.androidengineer.com/2010/06/using-ant-to-automate-building-android.html

    Seems a little long, but it goes through all the steps and details involved.

    0 讨论(0)
  • 2020-12-05 02:04

    You could try to have a following property file in your build.properties file:

    service.url=*
    

    And you could have http://localhost:1234 or https://test.mydomain.com in local.properties for your development and integration testing, and it could be set to https://mydomain.com in default.properties.

    By do ing this, you have will get different value for service.url in different build environment. You could use that value to generate a config file, and parse it into your code, or set it to env variable, or just put it into a resource file, and Android will read it for you:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string name="service-url">@@toben_to_be_replaced_during_build_time@@</string>
    </resources>
    
    0 讨论(0)
提交回复
热议问题