最近,我需要自动执行AndroidManifest.xml文件中versionName元素的更新。 运行构建时,需要对此打上时间戳记并使用正确的版本号进行更新。 我们将maven作为构建的一部分,使用了一些插件。 我们要使用POM的版本号,以便可以使用诸如maven-release-plugin或maven版本插件之类的工具。 当这些文件运行时,新版本号应反映在内置的APK中。
以下是添加到pom构建部分中的魔术,可以解决此问题。
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.8</version>
<executions>
<execution>
<id>parse-version</id>
<goals>
<goal>parse-version</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>maven-replacer-plugin</artifactId>
<version>1.4.1</version>
<executions>
<execution>
<id>replace-version</id>
<phase>validate</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>AndroidManifest.xml</file>
<replacements>
<replacement>
<token>0.0.0</token> <value>${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}.${maven.build.timestamp}</value>
</replacement>
</replacements>
<quiet>false</quiet>
</configuration>
</plugin>
</plugins>
您可以将其添加为配置文件的一部分,并且仅当您在Jenkins上进行构建或发布时才启用它。 您的AndroidManifest.xml应将versionName值设置为0.0.0,并且在启用配置文件的情况下运行构建时,它将用pom的版本替换它,并添加时间戳。
翻译自: https://www.javacodegeeks.com/2014/03/update-the-androidmanifest-version-number-automatically.html