Android/RoboGuice/Maven: ClassNotFoundException in

2019-05-16 23:54发布

问题:

I have a class that handles Guice bindings. It is the very first thing that needs to run, called ApplicationAutoworkout.java.

package com.redsoft.android.autoworkout;

import java.util.List;

import roboguice.application.RoboApplication;

import com.google.inject.Module;
import com.redsoft.android.autoworkout.service._ModuleQueryFor;
import com.redsoft.android.autoworkout.service._ModuleService;
import com.redsoft.android.autoworkout.ui._ModuleUi;

public class ApplicationAutoworkout extends RoboApplication {
     private Module testModule;

@Override
protected void addApplicationModules(List<Module> modules) {
    modules.add(new _ModuleUi(this.getApplicationContext()));
    modules.add(new _ModuleService(this.getApplicationContext()));
    modules.add(new _ModuleQueryFor());

    if (testModule!=null)modules.add(testModule);

    super.addApplicationModules(modules);
}
    //Allows the testModule to be added during tests
    public void setModule(Module module) {
        this.testModule = module;
    }

}

This class, for whatever reason, cannot be found by the Android emulator when it is built/deployed from eclipse

09-05 03:43:42.745: ERROR/AndroidRuntime(843): FATAL EXCEPTION: main
09-05 03:43:42.745: ERROR/AndroidRuntime(843): java.lang.RuntimeException: Unable to instantiate application com.redsoft.android.autoworkout.ApplicationAutoworkout: java.lang.ClassNotFoundException: com.redsoft.android.autoworkout.ApplicationAutoworkout in loader dalvik.system.PathClassLoader[/data/app/com.redsoft.android.autoworkout-2.apk]
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.ActivityThread.access$3000(ActivityThread.java:125)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.os.Looper.loop(Looper.java:123)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at java.lang.reflect.Method.invokeNative(Native Method)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at java.lang.reflect.Method.invoke(Method.java:521)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at dalvik.system.NativeStart.main(Native Method)
09-05 03:43:42.745: ERROR/AndroidRuntime(843): Caused by: java.lang.ClassNotFoundException: com.redsoft.android.autoworkout.ApplicationAutoworkout in loader dalvik.system.PathClassLoader[/data/app/com.redsoft.android.autoworkout-2.apk]
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.Instrumentation.newApplication(Instrumentation.java:942)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
09-05 03:43:42.745: ERROR/AndroidRuntime(843):     ... 11 more

However, if I run this from the command line:

mvn clean process-classes android:dex android:apk android:deploy

everything works. What's going on in Eclipse that keeps Android from finding the class?

EDIT: Here's the AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.redsoft.android.autoworkout" android:versionCode="0" android:versionName="0.0.1-SNAPSHOT" xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-sdk android:targetSdkVersion="7" android:minSdkVersion="7"></uses-sdk>

  <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:name="ApplicationAutoworkout">
    <activity android:label="@string/app_name" android:name=".ui.ActivityMain">
      <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
      </intent-filter>
    </activity>
    <activity android:name=".ui.profile.ActivityProfile"></activity>
    <activity android:name=".ui.routine.ActivityRoutine"></activity>

    <activity android:name=".ui.train.ActivityTrain"></activity><activity android:name=".ui.exercise.ActivityExercise"></activity>
    <activity android:name=".ui.exercise.ActivityViewExercise"></activity><activity android:name=".ui.exercise.ActivityAddEditExercise"></activity>
    <activity android:name=".ui.profile.ActivityAddProfile"></activity>
    <activity android:name=".ui.profile.ActivityEditProfile"></activity>
    <activity android:name=".ui.routine.ActivityRoutineView"></activity>
    <activity android:name=".ui.routine.ActivityRoutineEdit"></activity>
    <activity android:name=".ui.routine.ActivityRoutineAdd"></activity>
    <activity android:name=".ui.routine.ActivityRoutineAddExercise"></activity>

  </application>

</manifest>

Also, the POM:

<?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>

  <parent>
    <groupId>com.seventheye.android</groupId>
    <artifactId>autoworkout-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>autoworkout-app</artifactId>
  <packaging>apk</packaging>
  <name>autoworkout - Application</name>
<dependencies>
 <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice-no_aop</artifactId>
    </dependency>
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice-assisted-inject</artifactId>
    </dependency>
    <dependency>
      <groupId>org.roboguice</groupId>
      <artifactId>roboguice</artifactId>
  </dependency>
  <dependency>
    <groupId>com.j256.ormlite</groupId>
    <artifactId>ormlite-core</artifactId>
  </dependency>
  <dependency>
    <groupId>com.j256.ormlite</groupId>
    <artifactId>ormlite-android</artifactId>
  </dependency>
     <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.seventheye.android</groupId>
      <artifactId>robolectric-sqlite</artifactId>
      <version>1.0-RC5-SNAPSHOT</version>
      <scope>test</scope>
    </dependency>
</dependencies>

  <profiles>
  <profile>
      <id>proguard</id>
      <build>
              <plugins>
              <plugin>
                <groupId>com.pyx4me</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.4</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>4.6</proguardVersion>
                    <maxMemory>512m</maxMemory>
                    <injar>android-classes</injar>
                    <libs>
                      <lib>${rt.jar.path}</lib>
                      <lib>${jsse.jar.path}</lib>
                    </libs>
                    <obfuscate>false</obfuscate>
                     <addMavenDescriptor>false</addMavenDescriptor>
                     <proguardInclude>${project.basedir}/proguard.conf</proguardInclude>
                  </configuration>
                <dependencies>
              <dependency>
                <groupId>net.sf.proguard</groupId>
                <artifactId>proguard</artifactId>
                <version>4.6</version>
                <scope>runtime</scope>
              </dependency>
            </dependencies>
            </plugin>
            </plugins>
      </build>
      </profile>
    <profile>
      <id>release</id>
      <build>
        <plugins>
              <plugin>
                <groupId>com.pyx4me</groupId>
                <artifactId>proguard-maven-plugin</artifactId>
                <version>2.0.4</version>
                <executions>
                    <execution>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>proguard</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <proguardVersion>4.6</proguardVersion>
                    <maxMemory>512m</maxMemory>
                    <injar>android-classes</injar>
                    <!-- <injar>scala-library</injar>-->
                    <libs>
                      <lib>${rt.jar.path}</lib>
                      <lib>${jsse.jar.path}</lib>
                    </libs>
                    <obfuscate>false</obfuscate>
                     <addMavenDescriptor>false</addMavenDescriptor>
                     <proguardInclude>${project.basedir}/proguard.conf</proguardInclude>
                  </configuration>
                <dependencies>
              <dependency>
                <groupId>net.sf.proguard</groupId>
                <artifactId>proguard</artifactId>
                <version>4.6</version>
                <scope>runtime</scope>
              </dependency>
            </dependencies>
            </plugin>
          <plugin>
            <artifactId>maven-jarsigner-plugin</artifactId>
            <executions>
              <execution>
                <id>sign-application-apk</id>
                <phase>package</phase>
                <goals>
                  <goal>sign</goal>
                  <goal>verify</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
          <plugin>
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>maven-android-plugin</artifactId>
             <executions>
        <execution>
            <id>startemulator</id>
            <phase>initialize</phase>
            <goals>
            <goal>emulator-start</goal>
            </goals>
        </execution>
              <execution>
                <id>zipalign-application-apk</id>
                <phase>package</phase>
                <goals>
                  <goal>zipalign</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <zipalign>
                <verbose>true</verbose>
                <inputApk>${project.build.directory}/${project.artifactId}-${project.version}.apk</inputApk>
                <outputApk>${project.build.directory}/${project.artifactId}-${project.version}-signed-aligned.apk</outputApk>
              </zipalign>
              <sign>
                <debug>false</debug>
              </sign>
              <emulator>
            <avd>Android2.2</avd>
            <wait>10000</wait>
            <options>-no-skin</options>
              </emulator>
            </configuration>
          </plugin>
          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <configuration>
              <artifacts>
                <artifact>
                  <file>${project.build.directory}/proguard_map.txt</file>
                  <type>map</type>
                  <classifier>release</classifier>
                </artifact>
              </artifacts>
            </configuration>
            <executions>
              <execution>
                <id>attach-signed-aligned</id>
                <phase>package</phase>
                <goals>
                  <goal>attach-artifact</goal>
                </goals>
              </execution>
            </executions>
          </plugin>
         <plugin>
                <artifactId>maven-clean-plugin</artifactId>
                <version>2.4.1</version>
                <configuration>
                    <filesets>
                        <fileset>
                            <directory>tmp</directory>
                            <includes>
                                <include>*</include>
                            </includes>
                        </fileset>
                    </filesets>
                </configuration>
            </plugin>
            <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-eclipse-plugin</artifactId>
    <version>2.8</version>
    <!-- see http://maven.apache.org/plugins/maven-eclipse-plugin/eclipse-mojo.html for more information -->
    <configuration>
      <downloadSources>true</downloadSources>
      <downloadJavadocs>true</downloadJavadocs>
      <projectnatures>
        <projectnature>org.scala-ide.sdt.core.scalanature</projectnature>
        <projectnature>org.eclipse.jdt.core.javanature</projectnature>
      </projectnatures>
      <buildcommands>
        <buildcommand>org.scala-ide.sdt.core.scalabuilder</buildcommand>
      </buildcommands>
      <classpathContainers>
        <classpathContainer>org.scala-ide.sdt.launching.SCALA_CONTAINER</classpathContainer>
        <classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
      </classpathContainers>
      <excludes>
        <exclude>org.scala-lang:scala-library</exclude>
        <exclude>org.scala-lang:scala-compiler</exclude>
      </excludes>
      <sourceIncludes>
        <sourceInclude>**/*.scala</sourceInclude>
        <sourceInclude>**/*.java</sourceInclude>
      </sourceIncludes>
    </configuration>
  </plugin>
        </plugins>
      </build>
    </profile>
  </profiles>

</project>

回答1:

The problem is that the Android Development Toolkit Eclipse plugin does not support Maven dependencies. If you want to use it you need to use the m2e-android Android Configurator extension for M2E:

http://rgladwell.github.com/m2e-android



回答2:

Just a guess, but is it possible your AndroidManifest is looking for the application class com.redsoft.android.autoworkout.ApplicationAutoworkout when the class has been renamed/moved?

I find that the manifest gets a wrong value in it now and then even if I don't do a move/rename.