Maven and android - Slightly different builds for different environments

后端 未结 1 1724
旧时难觅i
旧时难觅i 2020-12-10 20:12

Ok I am switching from ant to maven on an android project and wondering if it the following would be easy to implement:

Currently I have a custom build.xml script wh

相关标签:
1条回答
  • 2020-12-10 20:30

    In Maven, this is called resource filtering, android-maven-plugin support filtering the following resource types:

    • AndroidManifest.xml, see this answer.
    • assets/, see this answer.
    • res/, see below.

    Sample res/value/config.xml:

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

    Sample pom configuration for filtering all xml file under res/ directory:

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

    There are several ways to define the substituted value, you can define them in an external properties file with properties-maven-plugin. For simplicity, I prefer to use Maven profiles and define them in pom.xml, like so:

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

    Then use mvn clean install -Pxxx to build corresponding apk.

    0 讨论(0)
提交回复
热议问题