android - 使用 maven 构建 apk 中的 ClassNotFoundException

标签 android maven maven-3 apk android-maven-plugin

我想在 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) 小得多。显然那里缺少类(class)。

因此,当启动此 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 signmvn 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>

最佳答案

发现问题:

<sourceDirectory>src</sourceDirectory>

build 部分中丢失。想知道为什么 Android 原型(prototype)(运行 archetype:generate 时)还没有包含它,因为它在 Android 中是非常标准的。

关于android - 使用 maven 构建 apk 中的 ClassNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11264887/

相关文章:

java - 这如何解析为 Java 对象 (JSON)

java - Android - 使用 JSOUP 实现具有 AsyncTask 的 ListView

java - Android:如何不保存实例状态?

maven - Axis2 数据绑定(bind) jaxbri + Maven : JAX-B RI JARs not on classpath

java - 从 Eclipse 为 Nexus 部署典型的 Maven 插件

java - 当 Maven war 插件找不到 webResource 目录时,如何避免构建失败?

maven 3 : Accessing version of "root" corporate POM

macos - 如何在不使用 brew 的情况下使用终端将 maven 安装到 mac

java - 从文件中读取 - 如何?

maven - 如何强制 Maven clean install 只显示构建摘要?