【问题标题】:Android/RoboGuice/Maven: ClassNotFoundException in Eclipse, but not from Maven command lineAndroid/RoboGuice/Maven:Eclipse 中的 ClassNotFoundException,但不是来自 Maven 命令行
【发布时间】:2011-09-05 04:12:55
【问题描述】:

我有一个处理 Guice 绑定的类。它是第一个需要运行的东西,称为 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;
    }

}

无论出于何种原因,在从 Eclipse 构建/部署该类时,Android 模拟器都找不到该类

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

但是,如果我从命令行运行它:

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

一切正常。 Eclipse 中发生了什么使 Android 无法找到该类?

编辑:这是 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>

还有,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>

【问题讨论】:

  • 你能展示你的 pom 来分析你的问题吗?

标签: android eclipse maven guice classnotfoundexception


【解决方案1】:

问题在于 Android Development Toolkit Eclipse 插件不支持 Maven 依赖项。如果你想使用它,你需要使用 M2E 的 m2e-android Android Configurator 扩展:

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

【讨论】:

  • 此依赖项已安装,并且之前可以部署到模拟器。我目前使用的是 0.2.5 版。但是,您似乎有一个新的测试版。我想我应该尝试安装它并回复您。
  • 更新到 0.3.0 最终让我获得了 Indigo(来自 Helios),清除了我的本地 git 存储库,并再次设置了我所有的插件/构建路径/依赖项。这些东西的结合解决了我的问题,所以感谢你把我推向那个方向!
  • 在我的项目中添加了 Scala 支持,并且一切正常……错误又回来了。这绝对不是你的依赖。
  • 抱歉,m2e-android 0.3.0 版本中存在一个刚刚修复的错误。您可以使用主快照更新站点升级到工作预发布版本:rgladwell.github.com/m2e-android/updates/master
【解决方案2】:

只是一个猜测,但是当类已重命名/移动时,您的 AndroidManifest 是否有可能正在寻找应用程序类 com.redsoft.android.autoworkout.ApplicationAutoworkout?

我发现清单中有时会出现错误的值,即使我不进行移动/重命名也是如此。

【讨论】:

  • 情况似乎并非如此。我将粘贴我的 AndroidManifest.xml,以便人们仔细检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-29
  • 1970-01-01
  • 2016-07-15
  • 1970-01-01
  • 2013-08-14
  • 2016-12-12
相关资源
最近更新 更多