ClassNotFoundException in apk build with maven

时光怂恿深爱的人放手 提交于 2019-12-10 15:11:09

问题


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.

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.

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.

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}

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>

回答1:


Found the problem:

<sourceDirectory>src</sourceDirectory>

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.



来源:https://stackoverflow.com/questions/11264887/classnotfoundexception-in-apk-build-with-maven

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!