5

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

4

2 回答 2

4

您可以使用 Maven、Ant 和 Gradle 构建您的项目。他们都会做你想做的事。
我使用 Maven,因此我将专注于 Maven 配置。如果您不了解 Maven 的工作原理,这可能是一项复杂的任务。

配置 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/替换字符串。

对于商家只需复制粘贴上面列出的内容,<profile>更改<id>production</id>为更新 url。<id>merchant</id><value>

建造

mvn install -Pproduction

或者

mvn install -Pmerchant
于 2013-02-12T19:04:41.483 回答
1

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

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

这是一个示例 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>

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>

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

ant updateMyConfiguration -Dres.configuration.string=MyCustomBuildString -Dcode.configuration.my_integer=1234
于 2013-02-18T07:58:51.213 回答