我想在 Android 开发中为我的构建过程切换到 maven。我按照http://www.sonatype.com/books/mvnref-book/reference/android-dev-sect-archetype.html创建 pom.xml 并稍微调整了版本以便我使用最新版本的android-maven-plugin。
虽然我的 apk 可以从我的 IDE(IntellJ)中手动构建,但使用 maven 构建的 apk 存在问题。显然缺少一些不会将类放入 apk 的东西。
当我检查两个生成的apk(解压缩它)时,我发现maven生成的apk的classes.dex(2.6kB)比常规的(24.9kB)小得多。显然那里缺少课程。
因此,在启动此 apk 时,我收到以下错误:
E/AndroidRuntime(31228): FATAL EXCEPTION: main
E/AndroidRuntime(31228): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mycompany.abc/com.mycompany.abc.ABCActivity}: java.lang.ClassNotFoundException: com.mycompany.abc.ABCActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.mycompany.abc-1/pkg.apk]
E/AndroidRuntime(31228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
E/AndroidRuntime(31228): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime(31228): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(31228): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime(31228): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(31228): at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(31228): at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(31228): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(31228): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(31228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(31228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(31228): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(31228): Caused by: java.lang.ClassNotFoundException: com.mycompany.abc.ABCActivity in loader dalvik.system.PathClassLoader[/mnt/asec/com.mycompany.abc-1/pkg.apk]
E/AndroidRuntime(31228): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
E/AndroidRuntime(31228): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
E/AndroidRuntime(31228): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
E/AndroidRuntime(31228): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
E/AndroidRuntime(31228): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
E/AndroidRuntime(31228): ... 11 more
W/ActivityManager( 4964): Force finishing activity com.mycompany.abc/.ABCActivity
W/ActivityManager( 4964): Activity pause timeout for HistoryRecord{40902960 com.mycompany.abc/.ABCActivity}
这是我使用的 pom.xml:mvn install -P sign
或mvn android:apk
<?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.mycompany</groupId>
<artifactId>com.mycompany.abc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>com.mycompany.abc</name>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- Simply read properties from file -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>android.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
<sdk>
<platform>11</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
<device>usb</device>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>exec-maven-plugin</artifactId>
<groupId>org.codehaus.mojo</groupId>
<configuration>
<executable>${basedir}/scripts/run_app.sh</executable>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>sign</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
<executions>
<execution>
<id>signing</id>
<goals>
<goal>sign</goal>
</goals>
<phase>package</phase>
<inherited>true</inherited>
<configuration>
<archiveDirectory></archiveDirectory>
<includes>
<include>target/*.apk</include>
</includes>
<keystore>/path/to/debug.keystore</keystore>
<storepass>android</storepass>
<keypass>android</keypass>
<alias>androiddebugkey</alias>
<arguments>
<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>
<sign>
<debug>false</debug>
</sign>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>