使用 maven 构建 apk 中的 ClassNotFoundException [英] ClassNotFoundException in apk build with maven

查看:19
本文介绍了使用 maven 构建 apk 中的 ClassNotFoundException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Android 开发中切换到 maven 来进行我的构建过程.我跟着 http://www.sonatype.com/books/mvnref-book/reference/android-dev-sect-archetype.html 创建 pom.xml 并稍微调整版本,以便我使用最新版本的 android-maven-plugin.

I want to switch to maven for my build process in Android development. I followed http://www.sonatype.com/books/mvnref-book/reference/android-dev-sect-archetype.html to create the pom.xml and adjusted the version a bit so that I'm using the latest version of the android-maven-plugin.

虽然我的 apk 从我的 IDE (IntellJ) 中手动构建得很好,但使用 maven 构建的 apk 存在问题.很明显,缺少一些没有将类放入 apk 的东西.

While my apk gets build fine manually from within my IDE (IntellJ), there are problems with the apk built with maven. Obviously something's missing that doesn't put the classes into the apk.

当我检查两个生成的apk(解压)时,我发现maven生成的apk的classes.dex(2.6kB)比普通的(24.9kB)小很多.那里缺少课程.

When I check both generated apk (unzipping it), I find that the classes.dex of the maven generated apk is much smaller (2.6kB) than the regular one (24.9kB). Obviously classes are missing in there.

所以,当启动这个 apk 时,我收到以下错误:

So, when starting this apk, I'm getting the following error:

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

This is my pom.xml, on which I use: mvn install -P sign or 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>

推荐答案

发现问题:

<sourceDirectory>src</sourceDirectory>

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

was missing in the build section. Wondering though why the Android archetype (when running archetype:generate) doesn't already include that, as it's pretty standard in Android.

这篇关于使用 maven 构建 apk 中的 ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆