Build android project from command line with different urls / Environment

I want to build an android project from the command line. Usually I create a project for two environments (merchant and production), and I want to do this for the Merchant and Production URL automatically from the command line, without having to specify the project manually each time. For example, "Say", create a project for the production environment or say, create a project for the trading environment by specifying the environment in the command itself. It can be done? Please help.

+5
source share
2 answers

Using Ant, you can customize the project by copying the template resource file or the template java file, which will be generated before launching the release target of the standard android Ant file.

Generated files should be ignored before version control.

Here is an example of Ant target that needs to be called before creating an application with an Ant release. It generates one java file and one resource file:

<target name="updateMyConfiguration"">
    <copy file="./MyTemplateConfiguration.java"
        tofile="./src/com/mycompany/android/myapp/MyCodeConfiguration.java"
        overwrite="true">
    </copy>
    <replace file="./src/com/mycompany/android/myapp/MyCodeConfiguration.java">
        <replacefilter token="token_my_boolean"
            value="${code.configuration.my_boolean}" />
        <replacefilter token="token_my_integer"
            value="${code.configuration.my_integer}" />
        <replacefilter token="token_my_string"
            value="${code.configuration.my_string}" />
    </replace>
    <copy file="./MyTemplateRes.xml"
        tofile="./res/values/MyResConfiguration.xml"
        overwrite="true">
    </copy>
    <replace file="./res/values/MyResConfiguration.xml">
        <replacefilter token="token_my_string"
            value="${res.configuration.my_string}" />
        <replacefilter token="token_my_integer"
            value="${res_configuration.my_integer}" />
    </replace>
</target>

package com.mycompany.android.myapp;

public final class MyCodeConfiguration
{
    static final boolean my_boolean = token_my_boolean;
    static final String my_string = "token_my_string";
    static final int my_integer = token_my_integer;
}

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <add-resource type="string" name="my_string"/>
    <string name="my_string">token_my_string</string>
    <add-resource type="integer" name="my_integer"/>
    <integer name="my_integer">token_my_integer</integer>
</resources>

Then you just need to run Ant as follows:

ant updateMyConfiguration -Dres.configuration.string=MyCustomBuildString -Dcode.configuration.my_integer=1234
+1
source

You can create your project using Maven, Ant and Gradle. All of them will do what you want.
I use Maven, so I will focus on the configuration of Maven. This can be challenging if you do not know how Maven works.

Customize Maven

, :
https://code.google.com/p/maven-android-plugin/wiki/GettingStarted

android maven:
https://code.google.com/p/maven-android-plugin/

Eclipse:
https://code.google.com/p/maven-android-plugin/wiki/QuickStartForEclipseProject

:

mvn archetype:generate \
  -DarchetypeArtifactId=android-quickstart \
  -DarchetypeGroupId=de.akquinet.android.archetypes \
  -DarchetypeVersion=1.0.8 \
  -DgroupId=com.myproject \
  -DartifactId=my-android-application

- .

: pom.xml, mvn archetype:generate .

, res/values ​​/strings.xml

:

<string name="hello">Whatever text</string>

 <string name="hello">productionURL</string>

( pom.xml </project>):

<profiles>
    <profile>
        <id>production</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>com.google.code.maven-replacer-plugin</groupId>
                    <artifactId>replacer</artifactId>
                    <version>1.5.2</version>
                    <executions>
                        <execution>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>replace</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <file>${project.basedir}/res/values/strings.xml</file>
                        <regex>true</regex>
                        <replacements>
                            <replacement>
                                <token><![CDATA[(<string name="hello">)(.+)(</string>)]]></token>
                                <value><![CDATA[$1productionURL$3]]></value>
                            </replacement>         
                        </replacements>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                    <artifactId>android-maven-plugin</artifactId>
                    <inherited>true</inherited>
                    <configuration>
                        <sign>
                            <debug>true</debug>
                        </sign>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

https://code.google.com/p/maven-replacer-plugin/ .

Merchant just copy-paste <profile>, , <id>production</id> <id>merchant</id> <value> URL- .

mvn install -Pproduction

mvn install -Pmerchant
+4

All Articles