如何使用 android:apk 的简单性 android-maven-plugin

Posted

技术标签:

【中文标题】如何使用 android:apk 的简单性 android-maven-plugin【英文标题】:how use android:apk of simpligility android-maven-plugin 【发布时间】:2016-09-27 04:16:52 【问题描述】:

我知道 com.jayway.maven.plugins.android.generation2 groupId 现在也已过时。插件的 groupId 现在是 com.simligility.maven .plugins.i 已经尝试过 jayway'plugin,没关系,当我尝试简单时 插件我有点麻烦。当我使用android:apk的目标时,它会出现这个错误:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building MavenTest 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- android-maven-plugin:4.4.1:apk (default-cli) @ MavenTest ---
[INFO] Generating debug apk.
[INFO] D:\Tools\EJuno\MavenTest2\target\AndroidManifest.xml: error: Unable to open file for read: No such file or directory
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.887 s
[INFO] Finished at: 2016-05-29T14:23:28+08:00
[INFO] Final Memory: 22M/362M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.simpligility.maven.plugins:android-maven-plugin:4.4.1:apk (default-cli) on project MavenTest: MojoExecutionException: ANDROID-040-001: Could not execute: Command = cmd.exe /X /C "E:\JavaEnvironment\Android-SDK\build-tools\23.0.3\aapt.exe package -f -M D:\Tools\EJuno\MavenTest2\target\AndroidManifest.xml --auto-add-overlay -I E:\JavaEnvironment\Android-SDK\platforms\android-22\android.jar -F D:\Tools\EJuno\MavenTest2\target\MavenTest-0.0.1-SNAPSHOT.ap_ --debug-mode", Result = 1 -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>MavenTest</groupId>
    <artifactId>MavenTest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
     <build>
        <plugins>
          <plugin>
            <groupId>com.simpligility.maven.plugins</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>4.4.1</version> 
            <extensions>true</extensions>
          </plugin>
        </plugins>
      </build>
</project>

我将AndroidManifest.xml复制到目标文件夹尝试一下,但问题存在。越来越多的人使用android studio,可能很少使用这些方式。我只是想用maven打包一个apk。

【问题讨论】:

【参考方案1】:

这里有我的工作 pom.xml 它适用于持续部署、发布、保护。调整用户、密码和 svn 设置和 proguard 设置(切换为 true 或 false):

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mydomain</groupId>
    <artifactId>myapp</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>MyApp</name>

    <scm>
        <connection>scm:svn:https://localhost/svn/myproject</connection>
        <developerConnection>scm:svn:https://localhost/svn/myproject/myapp</developerConnection>
        <url>scm:svn:https://localhost/svn/myrepo</url>
    </scm>

    <distributionManagement>
        <repository>
            <id>svnrepo</id>
            <url>svn:https://localhost/svn/myproject/myapp/releases</url>
        </repository>
        <snapshotRepository>
            <id>svnrepo</id>
            <url>svn:https://localhost/svn/myproject/myapp/snapshots</url>
        </snapshotRepository>
    </distributionManagement>

    <!-- see also properties in settings.xml! -->
    <properties>
        <android.sdk.path>/opt/android-sdk-linux</android.sdk.path>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.javadoc.skip>true</maven.javadoc.skip>
    </properties>

    <dependencies>
        <!-- android 6 -->
        <!-- The dependency was installed by mvn install:install-file -->
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- Dependencies for AdMob -->
        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>26.0.0</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>com.google.android.gms</groupId>
            <artifactId>google-play-services</artifactId>
            <version>26.0.0</version>
            <type>jar</type>
        </dependency>

        <!-- Compatibilty packages: FragmentActivity, NavigationDrawer,... -->

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v4</artifactId>
            <version>23.0.0</version>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <version>23.0.0</version>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <version>23.0.0</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>com.android.support</groupId>
            <artifactId>design</artifactId>
            <version>23.0.0</version>
            <type>aar</type>
            <exclusions>
                <exclusion>
                    <groupId>com.android.support</groupId>
                    <artifactId>appcompat-v7</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.android.support</groupId>
                    <artifactId>support-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>com.android.support</groupId>
                    <artifactId>support-v4</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <outputDirectory>target/classes</outputDirectory>
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-scm-plugin</artifactId>
                <version>1.9.2</version>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-release-plugin</artifactId>
                <version>2.5.1</version>
            </plugin>
            <plugin>
                <artifactId>maven-jarsigner-plugin</artifactId>
                <version>1.2</version>
                <configuration>
                    <finalName>$project.artifactId-$project.version</finalName>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <encoding>$project.build.sourceEncoding</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>4.0.0-rc.1</version>
                <extensions>true</extensions>
                <configuration>
                    <androidManifestFile>$project.basedir/AndroidManifest.xml</androidManifestFile>
                    <assetsDirectory>$project.basedir/assets</assetsDirectory>
                    <resourceDirectory>$project.basedir/res</resourceDirectory>
                    <nativeLibrariesDirectory>$project.basedir/src/main/native</nativeLibrariesDirectory>
                    <genDirectory>$project.basedir/gen</genDirectory>
                    <sdk>
                        <platform>23</platform>
                        <path>$android.sdk.path</path>
                    </sdk>
                    <undeployBeforeDeploy>true</undeployBeforeDeploy>
                    <proguard>
                        <skip>true</skip>
                        <config>proguard.cfg</config>
                    </proguard>
                    <sign>
                        <debug>$debug</debug>
                    </sign>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself. -->
                <plugin>
                    <groupId>org.eclipse.m2e</groupId>
                    <artifactId>lifecycle-mapping</artifactId>
                    <version>1.0.0</version>
                    <configuration>
                        <lifecycleMappingMetadata>
                            <pluginExecutions>
                                <pluginExecution>
                                    <pluginExecutionFilter>
                                        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                                        <artifactId>android-maven-plugin</artifactId>
                                        <versionRange>4.0.0-rc.1</versionRange>
                                        <goals>
                                            <goal>generate-sources</goal>
                                            <goal>proguard</goal>
                                        </goals>
                                    </pluginExecutionFilter>
                                    <action>
                                        <ignore />
                                    </action>
                                </pluginExecution>
                            </pluginExecutions>
                        </lifecycleMappingMetadata>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>

    <profiles>
        <profile>
            <id>devel</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <debug>true</debug>
            </properties>
        </profile>

        <profile>
            <id>release</id>
            <properties>
                <debug>false</debug>
            </properties>
            <activation>
                <property>
                    <name>performRelease</name>
                    <value>true</value>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-release-plugin</artifactId>
                        <version>2.5.1</version>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-deploy-plugin</artifactId>
                        <version>2.8.2</version>
                        <dependencies>
                            <dependency>
                                <groupId>org.jvnet.wagon-svn</groupId>
                                <artifactId>wagon-svn</artifactId>
                                <version>1.12</version>
                            </dependency>
                        </dependencies>
                    </plugin>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-jarsigner-plugin</artifactId>
                        <version>1.3.2</version>
                        <executions>
                            <execution>
                                <id>signing</id>
                                <goals>
                                    <goal>sign</goal>
                                </goals>
                                <phase>package</phase>
                                <inherited>true</inherited>
                                <configuration>
                                    <archiveDirectory />
                                    <includes>
                                        <include>target/$project.artifactId-$project.version.apk</include>
                                    </includes>
                                    <keystore>$android.keystore</keystore>
                                    <storepass>$key.password</storepass>
                                    <keypass>$key.password</keypass>
                                    <alias>$key.alias</alias>
                                    <arguments>
                                        <argument>-verbose</argument>
                                        <argument>-sigalg</argument>
                                        <argument>MD5withRSA</argument>
                                        <argument>-digestalg</argument>
                                        <argument>SHA1</argument>
                                    </arguments>
                                </configuration>
                            </execution>
                        </executions>
                    </plugin>
                    <plugin>
                        <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                        <artifactId>android-maven-plugin</artifactId>
                        <inherited>true</inherited>
                        <configuration>
                            <!-- Since non-release builds are now debuggable by default you NEED TO ensure that the release parameter is set to true in your release build. -->
                            <release>true</release>
                            <zipalign>
                                <verbose>true</verbose>
                                <skip>false</skip>
                                <inputApk>$project.build.directory/$project.artifactId-$project.version.apk</inputApk>
                                <outputApk>$project.build.directory/$project.artifactId-$project.version-RELEASE.apk</outputApk>
                            </zipalign>

                        </configuration>
                        <executions>
                            <execution>
                                <id>zipalign</id>
                                <phase>install</phase>
                                <goals>
                                    <goal>zipalign</goal>
                                </goals>
                            </execution>
                        </executions>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>
</project>

settings.xml

<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">

  <pluginGroups>
  </pluginGroups>

  <proxies>
  </proxies>

  <servers>
    <server>
      <id>svnrepo</id>
      <username>myuser</username>
      <password>mypassword</password>
    </server>
  </servers>

  <mirrors>
  </mirrors>

  <profiles>
    <profile>
        <id>outsorced-project-properties</id>
        <properties>
            <android.keystore>/home/myuser/.android/release.keystore</android.keystore>
            <key.password>mykeystorepassword</key.password>
            <key.alias>androidreleasekey</key.alias>
        </properties>
    </profile>
  </profiles>

  <activeProfiles>
    <activeProfile>outsorced-project-properties</activeProfile>
  </activeProfiles>

</settings>

设备上的持续构建和部署由以下人员执行:

clean install android:deploy android:run 

发布管理适用于:

mvn -X release:prepare -Darguments=-Dusername=youruser -Dpassword=yourpassword
mvn -X release:perform -Darguments=-Dusername=youruser -Dpassword=yourpassword

对于 Google 支持库,您还必须安装 https://github.com/simpligility/maven-android-sdk-deployer

【讨论】:

以上是关于如何使用 android:apk 的简单性 android-maven-plugin的主要内容,如果未能解决你的问题,请参考以下文章

Android Apk的反编译和加密

Android APK反编译就这么简单 详解(附图)

如何使用Gradle实验插件对64位Android APK进行代码签名?

Android APK瘦身全面总结——如何从32.6M到13.6M

Android APK反编译就这么简单 详解(图文详解)

Android APK反编译就这么简单 详解(附图)