使用不同的url/Environment从命令行构建android项目

Roo*_*kie 5 android build maven

我想从命令行构建android项目.通常,我为两个环境(商家和生产)构建项目,我想从命令行自动执行商家和生产URL,而不是每次都在手动指定项目.例如,Say,为生产环境构建项目,或者说,通过在命令本身中指定环境来为商家环境构建项目.可以吗?请帮忙.

dav*_*vid 1

使用 Ant,您可以通过复制在运行标准 android Ant 文件的发布目标之前生成的模板资源文件或模板 java 文件来自定义您的项目。

版本控制应忽略生成的文件。

下面是一个示例 ANT 目标,在使用 ant 版本构建应用程序之前应调用该目标。它生成一个java文件和一个资源文件:

<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>
Run Code Online (Sandbox Code Playgroud)

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;
}

Run Code Online (Sandbox Code Playgroud)
<?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>
Run Code Online (Sandbox Code Playgroud)

然后你只需要这样运行 Ant:

ant updateMyConfiguration -Dres.configuration.string=MyCustomBuildString -Dcode.configuration.my_integer=1234
Run Code Online (Sandbox Code Playgroud)