Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 development, I want the service url to be pulled in from a configurable value. In Test, I want a different value pulled in. In production, yet another value.

So, in code I have something like this:

public class HttpRequestHelper 
{
    private static String GetServiceUrl(ServiceAction action)
    {
        return serviceUrl + action.toString();
    }
}

By default (when debugging/running through eclipse), I want that url to be http://localhost:1234

In Test I want https://test.mydomain.com

In Production I want https://mydomain.com

I am new to eclipse and ant and it has been a long time since I used java. How do I go about setting this up? What should the build.xml look like? I understand that when I want to build the test/prod versions I will need to use the command line. That's okay. But I don't know how to get this serviceUrl auto-set dependent on the build. I'm not even sure the best place to put this information (a resource, a properties file?). I really want to avoid setting it, building, setting it, building, etc.

like image 530
Josh Avatar asked Feb 17 '11 17:02

Josh


People also ask

How do I build an Ant build xml?

Create Ant build fileIn the Project tool window, select the directory, where the build file should be created. Right-click the directory and from the context menu, select New | File ( Alt+Insert ). In the New File dialog, specify the name of the new file with the xml extension, for example, build. xml.

What is Basedir in build xml?

The 'basedir' is the base directory from which any relative directories used within the Ant build file are referenced from. If this is omitted the parent directory of the build file will be used.

What is build xml used for?

The build. xml file is an Ant script that is created by the PDE to take your plug-in components and combine them into a deployable format. This file compiles and archives your plug-in source code into a single JAR file.


2 Answers

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.

like image 142
Vicky Avatar answered Oct 07 '22 04:10

Vicky


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>
like image 37
dongshengcn Avatar answered Oct 07 '22 04:10

dongshengcn