我想执行这个命令:
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
使用 Maven。我必须在 Android SDK/tools 目录中执行这个命令。谁能帮助我如何做到这一点?我认为这可以使用批处理文件来完成,但我不确定如何创建批处理文件。当我输入“mvn clean install”时,我需要执行这个命令。谢谢
我想执行这个命令:
zipalign [-f] [-v] <alignment> infile.apk outfile.apk
使用 Maven。我必须在 Android SDK/tools 目录中执行这个命令。谁能帮助我如何做到这一点?我认为这可以使用批处理文件来完成,但我不确定如何创建批处理文件。当我输入“mvn clean install”时,我需要执行这个命令。谢谢
您可以使用绑定到阶段的Maven Exec 插件。install
在下面的代码片段中,每次执行 a 时都会执行ping
带有参数的命令:8.8.8.8
mvn install
<project>
...
<build>
<plugins>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<version>1.2.1</version>
<executions>
<execution>
<id>My Command Runner</id>
<phase>install</phase>
<goals>
<goal>exec</goal>
</goals>
<configuration>
<executable>ping</executable>
<arguments>
<argument>8.8.8.8</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
</project>
就是这样。
我现在看到您真正遇到的问题是执行zipalign,而不是任意命令。为此,有两种方法。
从 Android Maven 插件的 2.5.0 版开始,zipalign
目标是插件的一部分。要激活,只需将目标添加zipalign
为执行(例如,添加到package
阶段)并将skip
参数设置plugin
configuration
为false
:
<zipalign><skip>false</skip></zipalign>
完整<plugin>
标签示例:
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<sign>
<debug>false</debug>
</sign>
<zipalign>
<verbose>true</verbose>
<inputApk>${project.build.directory}/${project.artifactId}.apk</inputApk>
<outputApk>${project.build.directory}/${project.artifactId}-signed-aligned.apk
</outputApk>
</zipalign>
</configuration>
<executions>
<execution>
<id>alignApk</id>
<phase>package</phase>
<goals>
<goal>zipalign</goal>
</goals>
</execution>
</executions>
</plugin>
资料来源:
android-maven-plugin
)的示例;pom.xml
zipalign
.下面的代码将 zip 对齐的功能绑定到package
阶段,无需询问即可覆盖以前的 zip 对齐文件。
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.1.1</version>
<executions>
<execution>
<id>zipalign</id>
<goals>
<goal>exec</goal>
</goals>
<phase>package</phase>
<configuration>
<executable>${ANDROID_HOME}/tools/zipalign</executable>
<arguments>
<argument>-f</argument>
<argument>4</argument>
<argument>target/${project.build.finalName}.apk</argument>
<argument>target/${project.build.finalName}-zipped.apk</argument>
</arguments>
</configuration>
</execution>
</executions>
</plugin>
请注意,必须将此配置文件添加到实际 APK 的 pom 中。您不能将其添加到父 pom。这样做的原因是它使用了定义工件名称 ( project.build.finalName
) 的 Maven 属性。
如果您使用 Maven 构建 Android 项目,您应该使用maven-android-plugn,它直接支持 zipalign 工具。