我如何更改我的AndroidManifest,因为它是由maven打包的

时间:2012-05-29 16:27:37

标签: android maven android-manifest android-maven-plugin

我的maven目标update-manifest在我的项目中工作,并根据我打包的配置文件更新我想要更改的属性;不幸的是,它会更改源代码管理中的清单。我希望它能够更改打包的清单,但只保留主版本。我没有看到AndroidManifest的一个版本目标/我可以指向它。我正在使用android-maven插件版本3.2

2 个答案:

答案 0 :(得分:20)

如果您使用manifestVersionCodemanifestVersionName之类的配置来更新清单,这就是它的设计和运行方式,它会将更改写入原始的AndroidManifest.xml。

您真正需要的是过滤器清单而不是更新清单。资源过滤是android-maven-plugin支持并经常使用的另一种使用场景。

示例AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mycompany.myapp"
    android:versionCode="${app.version.code}"
    android:versionName="${app.version.name}">
... ...

示例pom.xml:

<!-- value to be substituted in manifest -->
<properties>
  <app.version.code>1</app.version.code>
  <app.version.name>1.0.0-SNAPSHOT</app.version.name>
</properties>

... ...

<build>
  <resources>
    <!-- filter manifest and put filtered file in target/filtered-manifest/ -->
    <resource>
      <directory>${project.basedir}</directory>
      <filtering>true</filtering>
      <targetPath>${project.build.directory}/filtered-manifest</targetPath>
      <includes>
        <include>AndroidManifest.xml</include>
      </includes>
    </resource>
  </resources>

  ... ...

  <plugins>
    <plugin>
      <groupId>com.jayway.maven.plugins.android.generation2</groupId>
      <artifactId>android-maven-plugin</artifactId>
      <extensions>true</extensions>
      <configuration>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
        <!-- tell build process to use filtered manifest -->
        <androidManifestFile>${project.build.directory}/filtered-manifest/AndroidManifest.xml</androidManifestFile>
      </configuration>
    </plugin>

  ... ...

现在android-maven-plugin将使用过滤的AndroidManifest.xml并保持原来的AndroidManifest.xml不变。

希望这有帮助。

更新

你可能也需要这个:

<plugin>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.5</version>
        <executions>
            <execution>
                <phase>initialize</phase>
                <goals>
                    <goal>resources</goal>
                </goals>
            </execution>
        </executions>
    </plugin>

答案 1 :(得分:10)

我建议使用 maven-android-plugin manifest-update 目标来完成此任务:

<plugins>
    <plugin>
        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
        <artifactId>android-maven-plugin</artifactId>
        <version>3.3.2</version>
        <!-- This is for maven update properties in AndroidManifest file. Working outside eclipse. -->
        <executions>
            <execution>
                <id>update-manifest</id>
                <phase>process-resources</phase>
                <goals>
                    <goal>manifest-update</goal>
                </goals>
                <configuration>
                    <manifest>
                        <versionCode>${project.codeVersion}</versionCode>
                        <versionName>${project.version}</versionName>
                    </manifest>
                </configuration>
            </execution>
        </executions>
    </plugin>
</plugins>

您还需要定义project.codeVersion

<properties>
    <project.codeVersion>1</project.codeVersion>
</properties>

maven会更新你的AndroidManifest.xml:

[INFO] --- android-maven-plugin:3.3.2:manifest-update (update-manifest) @ myapp ---
[INFO] Attempting to update manifest d:\dsv\project\MyApp\AndroidManifest.xml
[INFO] Setting android:versionName to 0.0.4
[INFO] Setting android:versionCode to 1
[INFO] Made changes to manifest file, updating d:\...\MyApp\AndroidManifest.xml

有关 android:manifest-update 目标here的更多信息,您可以在那里找到一些有用的参数。

重要说明:此解决方案在命令行中运行,m2e-android尚不支持插件执行(请​​参阅此issue)。

相关问题