库项目Dx UNEXPECTED TOP-LEVEL EXCEPTION或ActivityNotFoundException

时间:2012-10-16 17:18:19

标签: android eclipse android-sdk-tools dx

我有库项目和使用它的常规android项目。

图书馆计划:

src文件夹 com.myproject

MainActivity
ChildActivity

的AndroidManifest.xml

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.project.library">
        <uses-sdk android:minSdkVersion="8" />
        <application>
            <activity android:name="com.myproject.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="com.myproject.ChildActivity">
            </activity>
        </application>
 </manifest>

独立项目(是一个Android项目并使用库项目):

src文件夹 com.myproject

ChildActivity

的AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.project">
        <uses-sdk android:minSdkVersion="8" />
        <application>
            <activity android:name="com.myproject.MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity android:name="com.myproject.ChildActivity">
            </activity>
        </application>
</manifest>

我想要实现的目标:

Android项目中的ChildActivity应该覆盖库项目中的ChildActivity。

编译失败:

[2012-10-16 20:02:02 - Standalone] Dx 
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/myproject/ChildActivity;
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main.processClass(Main.java:486)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main.processFileBytes(Main.java:455)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main.access$400(Main.java:67)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:394)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:135)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processDirectory(ClassPathOpener.java:191)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:123)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main.processOne(Main.java:418)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main.processAllFiles(Main.java:329)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.dx.command.dexer.Main.run(Main.java:206)
[2012-10-16 20:02:02 - Standalone] Dx   at sun.reflect.GeneratedMethodAccessor64.invoke(Unknown Source)
[2012-10-16 20:02:02 - Standalone] Dx   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
[2012-10-16 20:02:02 - Standalone] Dx   at java.lang.reflect.Method.invoke(Method.java:597)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.ide.eclipse.adt.internal.build.DexWrapper.run(DexWrapper.java:180)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.ide.eclipse.adt.internal.build.BuildHelper.executeDx(BuildHelper.java:703)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.ide.eclipse.adt.internal.build.builders.PostCompilerBuilder.build(PostCompilerBuilder.java:577)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.events.BuildManager$2.run(BuildManager.java:728)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:199)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.events.BuildManager.basicBuild(BuildManager.java:321)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.events.BuildManager.build(BuildManager.java:396)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.resources.Project$1.run(Project.java:618)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.resources.Project.internalBuild(Project.java:597)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.resources.Project.build(Project.java:124)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.ide.eclipse.adt.internal.project.ProjectHelper.doFullIncrementalDebugBuild(ProjectHelper.java:1000)
[2012-10-16 20:02:02 - Standalone] Dx   at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate.launch(LaunchConfigDelegate.java:147)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:855)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.debug.internal.core.LaunchConfiguration.launch(LaunchConfiguration.java:704)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.debug.internal.ui.DebugUIPlugin.buildAndLaunch(DebugUIPlugin.java:1047)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.debug.internal.ui.DebugUIPlugin$8.run(DebugUIPlugin.java:1251)
[2012-10-16 20:02:02 - Standalone] Dx   at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
[2012-10-16 20:02:02 - Standalone] Dx 1 error; aborting
[2012-10-16 20:02:02 - Standalone] Conversion to Dalvik format failed with error 1

如果我将Standalone项目中的src文件夹更改为com.myproject.standalone并将清单条目更改为

<activity android:name="com.myproject.standalone.ChildActivity">

我有例外:

android.content.ActivityNotFoundException: Unable to find explicit activity class

1 个答案:

答案 0 :(得分:0)

您无需在两个清单中声明活动。选择一个并使用它。我更喜欢将app清单用于所有活动声明。

更新:此外,库项目永远不应包含对引用它的任何项目的引用。这称为循环依赖。

更新:因此您的代码有点令人困惑,但看起来您在库和独立项目中声明具有相同完全限定名称的类。这不行。他们需要有不同的合格名称。限定名称是完整的包和类名。您的AndroidManifest for the库将包声明为“com.myproject.library”,但随后使用包“com.myproject”声明2个活动。如果它们在库项目中,请将限定名称更改为“com.myproject.library.ActivityName”或使用“.ActivityName”速记。如果它们在同一个包装中,则需要使用不同的包装。