【问题标题】:how do I change my AndroidManifest as it's being packaged by maven我如何更改我的 AndroidManifest,因为它正在由 maven 打包
【发布时间】:2012-06-03 21:49:17
【问题描述】:

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

【问题讨论】:

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


    【解决方案1】:

    如果您使用 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>
    

    【讨论】:

    • 在它进入需要过滤清单的阶段之前似乎没有启动过滤..仍在调试
    • 需要这个:maven-resources-plugin2.5initialize资源
    • 我无法更新 version.code 它给出了错误。是因为只有它可以设置整数吗?你是怎么解决的?
    • @yorkw 如果我想根据我在构建时选择的配置文件更改属性值,我该怎么做。我将属性标签放在配置文件中,但它不起作用。如果您想针对相同的问题提出其他问题,我可以这样做
    • @yorkw 提出了一个新问题stackoverflow.com/questions/22040759/…
    【解决方案2】:

    我建议使用 ma​​ven-android-pluginma​​nifest-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)。

    【讨论】:

    • 这对我来说确实有效,即使这种方式修改了原始 XML 文件
    • 不一定错。在某些情况下,修改原始 XML 文件正是我们想要的。
    猜你喜欢
    • 2013-11-25
    • 1970-01-01
    • 2015-04-03
    • 2011-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    相关资源
    最近更新 更多