Maven和Android的 - 略有不同的是建立不同的环境(Maven and android -

2019-07-05 05:26发布

好吧我从蚂蚁切换到Maven在Android项目,并想知道如果它下面会很容易实现:

目前,我有这对于创建发布版本的几个目标定制build.xml脚本。 他们每个人都用于构建以针对不同的服务器URL,其中服务器可以是开发,生产,升级,甚至我们在其他国家部署服务器运行应用程序。

其原因是,我们的应用程序将运行对几个不同的服务器取决于谁得到它,我不希望这样的东西的用户选择。 相反,它应该被硬编码到应用程序,它是如何工作的目前。

这是蚂蚁很容易安装,我只取值从[ENV]属性文件,然后替换了在res /价值/ config.xml中SERVER_URL字符串。 例如:

ant release-prod

会拉读取一个文件名为prod.properties定义什么是SERVER_URL。 我存储在配置/ config.xml中的config.xml文件为这样:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <string name="config_server_url">@CONFIG.SERVER_URL@</string>
</resources>

然后,我的ant脚本做到这一点:

<copy file="config/config.xml" todir="res/values" overwrite="true" encoding="utf-8">
    <filterset>
           <filter token="CONFIG.SERVER_URL" value="${config.server_url}" />
    </filterset>
</copy>

凡config.server_url在prod.properties定义。

我想知道我怎么能做到与Maven类似的东西? 有任何想法吗。 我抬起头,如何读取属性文件与Maven,它看上去像结果是混合是否将工作或没有。

Answer 1:

在Maven的,这就是所谓的资源筛选,Android的Maven的插件支持过滤以下资源类型:

  • AndroidManifest.xml中, 看到这个答案 。
  • 资产/,看到这个答案 。
  • RES /,见下文。

样品RES /价值/ config.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
  <string name="config_server_url">${config.server.url}</string>
</resources>

用于过滤RES /目录下的所有XML文件示例POM配置:

<build>
  <resources>
    <resource>
      <directory>${project.basedir}/res</directory>
      <filtering>true</filtering>
      <targetPath>${project.build.directory}/filtered-res</targetPath>
      <includes>
        <include>**/*.xml</include>
      </includes>
    </resource>
  </resources>
  <plugins>
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <executions>
        <execution>
          <phase>initialize</phase>
          <goals>
            <goal>resources</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>com.jayway.maven.plugins.android.generation2</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        <sdk>
          <platform>10</platform>
        </sdk>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
        <resourceDirectory>${project.build.directory}/filtered-res</resourceDirectory>
      </configuration>
    </plugin>
  </plugins>
</build>

有几种方法来定义取代的价值,您可以在外部属性定义他们的属性,Maven的插件文件。 为简单起见,我更喜欢使用Maven的配置文件和在pom.xml中定义它们,就像这样:

<profiles>
  <profile>
    <id>dev</id>
    <properties>
      <config.server.url>dev.company.com</config.server.url>
    </properties>
  </profile>
  <profile>
    <id>Test</id>
    <properties>
      <config.server.url>test.company.com</config.server.url>
    </properties>
  </profile>
  <profile>
    <id>Prod</id>
    <properties>
      <config.server.url>prod.company.com</config.server.url>
    </properties>
  </profile>
</profiles>

然后使用mvn clean install -Pxxx建立相应的APK。



文章来源: Maven and android - Slightly different builds for different environments