Android Studio - 意外的顶级例外:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21102598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio - UNEXPECTED TOP-LEVEL EXCEPTION:
提问by BENBUN Coder
I have built a new project in Android Studio using the new project templates provided as part of the tool. All of the code has been generated by Studio I have not made any amendments yet.
我使用作为工具一部分提供的新项目模板在 Android Studio 中构建了一个新项目。所有的代码都是由 Studio 生成的,我还没有做任何修改。
I am attempting to run the code but the app fails with the following errors, not sure what the problem is so any help appreciated.
我正在尝试运行代码,但应用程序失败并出现以下错误,不确定问题是什么,所以感谢任何帮助。
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\Users\RichardKavanagh\AppData\Local\Android\android-sdk\build-tools.0.1\dx.bat --dex --output D:\Android\Projects\MyHealthRecord\app\build\libs\app-debug.dex D:\Android\Projects\MyHealthRecord\app\build\classes\debug D:\Android\Projects\MyHealthRecord\app\build\dependency-cache\debug D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\android-support-v7-appcompat-5a78dab7e2789bbe64f4bc80d106ca75c04dcf6f.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\classes-f9b947272e9f33ba50355b52d82755584f9c0c58.jar D:\Android\Projects\MyHealthRecord\app\build\pre-dexed\debug\support-v4-19.0.0-31a2c13df80d37d62ca50fec3bde6da0ca706223.jar
Error Code:
2
Output:
UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/v7/app/ActionBar$Callback;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 12.948 secs
回答by iTapAndroid
Like everyone else said here, the support library (com.android.support
) is being included more than once in your project. Try adding this in your build.gradle
at the root level and it should exclude the support library from being exported via other project dependencies.
就像这里的其他人所说的那样,支持库 ( com.android.support
) 不止一次地包含在您的项目中。尝试build.gradle
在根级别添加它,它应该排除支持库通过其他项目依赖项导出。
configurations {
all*.exclude group: 'com.android.support', module: 'support-v4'
}
If you have more then one support libs included in the dependencies like this, you may want to remove one of them:
如果像这样的依赖项中包含多个支持库,则可能需要删除其中之一:
回答by varunarl
dependencies {
compile 'com.android.support:support-v4:19.1.+'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
This gets conflicted if you have the support jar in your libs folder.
If you have the support jar in your project libs folder and you have the module dependency added to compile 'com.android.support:support-v4:13.0.+' the UNEXPECTED_TOPLEVEL_DEPENDANCY exception will be thrown.
如果您的 libs 文件夹中有支持 jar,这会发生冲突。如果您的项目 libs 文件夹中有支持 jar,并且您添加了模块依赖项来编译 'com.android.support:support-v4:13.0.+',则会抛出 UNEXPECTED_TOPLEVEL_DEPENDANCY 异常。
回答by buptcoder
Because you may include two same libs in your project. check your build.gradle file.
因为您的项目中可能包含两个相同的库。检查您的 build.gradle 文件。
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile files('libs/android-support-v4.jar')
}
if your file includes compile 'com.android.support:appcompat-v7:+'
and compile files('libs/android-support-v4.jar')
, it will have this problems.
delete this sentence: compile files('libs/android-support-v4.jar')
如果您的文件包含 compile'com.android.support:appcompat-v7:+'
和compile files('libs/android-support-v4.jar')
,则会出现此问题。删除这句话:compile files('libs/android-support-v4.jar')
That's how I fix this problem.
这就是我解决这个问题的方法。
回答by Gene
The error occurs when you have the same library/directory included more than once in your build.gradle's dependencies. Ok, let's say you have an App structure that looks like this:
当您在 build.gradle 的依赖项中多次包含相同的库/目录时会发生错误。好的,假设您有一个如下所示的 App 结构:
So you have the main “app” and then you have a bunch of sub-apps/modules/libraries. The libraries are: 1) gene_test_library, 2) genes_nine_old_androids_library, & 3) swipe_list_view_library.
所以你有主“应用程序”,然后你有一堆子应用程序/模块/库。这些库是:1)gene_test_library,2)genes_nine_old_androids_library,& 3)swipe_list_view_library。
My name is Gene, so that's why there are all these “gene” libraries.
我的名字是 Gene,这就是为什么有所有这些“基因”库的原因。
Inside the build.gradle for “app”, I have:
在“app”的 build.gradle 中,我有:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
//compile project(':libraries:genes_nine_old_androids_library')
compile project(':libraries:swipe_list_view_library')
}
Inside the build.gradle for gene_test_library, I have nothing:
在gene_test_library的build.gradle里面,我什么都没有:
dependencies {
}
Inside build.gradle for gene_nine_old_androids_library, I have:
在gene_nine_old_androids_library的build.gradle里面,我有:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
Inside build.gradle for swipe_list_view_library, I have:
在 swipe_list_view_library 的 build.gradle 中,我有:
dependencies {
compile 'com.nineoldandroids:library:2.4.0+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.0'
}
This line of code “compile fileTree(dir: 'libs', include: ['*.jar'])” just means “hey, look inside the ‘libs' folder inside this module for any jar files. I do not have anything in the libs folder of any of the modules so you can ignore that line of code.
这行代码“compile fileTree(dir: 'libs', include: ['*.jar'])”只是意味着“嘿,在这个模块内的'libs'文件夹中查找任何jar文件。我在任何模块的 libs 文件夹中都没有任何内容,因此您可以忽略该行代码。
So let's say I uncomment out //compile project(':libraries:genes_nine_old_androids_library') In the build.gradle for the “app” module. Then I would get the “UNEXPECTED TOP-LEVEL EXCEPTION:” error. Why is that?
所以假设我取消了 //compile project(':libraries:genes_nine_old_androids_library') 在“app”模块的 build.gradle 中的注释。然后我会得到“意外的顶级异常:”错误。这是为什么?
Well, writing //compile project(':libraries:genes_nine_old_androids_library') inside the build.gradle for “app”, is the same as taking the build dependencies of “genes_nine_old_androids_library” module and putting it there. So uncommenting the //compile project(':libraries:genes_nine_old_androids_library') statement, the build.gradle for “app” module becomes:
好吧,在 build.gradle 中为“app”编写 //compile project(':libraries:genes_nine_old_androids_library') ,与获取“genes_nine_old_androids_library”模块的构建依赖项并将其放在那里是相同的。因此取消注释 //compile project(':libraries:genes_nine_old_androids_library') 语句,“app”模块的 build.gradle 变为:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':libraries:gene_test_library')
***compile fileTree(dir: 'libs', include: ['*.jar'])***
***compile 'com.android.support:appcompat-v7:21.0.0'***
compile project(':libraries:swipe_list_view_library')
}
Notice how now “compile 'com.android.support:appcompat-v7:21.0.0'” shows up 2x. That's where the error is coming from.
注意现在“compile 'com.android.support:appcompat-v7:21.0.0'”出现了 2 倍。这就是错误的来源。
回答by Hamed Ghadirian
I found 2 reason for this issue:
我发现了这个问题的两个原因:
Sometimes its because of multiple included libraries. For example you add
compile 'com.nineoldandroids:library:2.4.0'
有时是因为包含多个库。例如你添加
编译'com.nineoldandroids:library:2.4.0'
in your gradle and add another library that it also use "nineoldandroids" in it's gradle!
在你的 gradle 中添加另一个库,它也在它的 gradle 中使用“nineoldandroids”!
- As Android Developer Officialwebsite said:
- 正如Android Developer 官网所说:
If you have built an Android app and received this error, then congratulations, you have a lot of code!
如果你已经构建了一个 Android 应用程序并收到这个错误,那么恭喜你,你有很多代码!
So, why?
所以为什么?
The Dalvik Executable specification limits the total number of methods that can be referenced within a single DEX file to 65,536, including Android framework methods, library methods, and methods in your own code. Getting past this limit requires that you configure your app build process to generate more than one DEX file, known as a multidex configuration.
Dalvik Executable 规范将单个 DEX 文件中可以引用的方法总数限制为 65,536,包括 Android 框架方法、库方法和您自己代码中的方法。要突破这一限制,您需要将应用程序构建过程配置为生成多个 DEX 文件,称为 multidex 配置。
Then what should you do?
那你应该怎么做?
Avoiding the 65K Limit- How?
- Review your app's direct and transitive dependencies -Ensure any large library dependency you include in your app is used in a manner that outweighs the amount of code being added to the application. A common anti-pattern is to include a very large library because a few utility methods were useful. Reducing your app code dependencies can often help you avoid the dex reference limit.
- Remove unused code with ProGuard- Configure the ProGuard settings for your app to run ProGuard and ensure you have shrinking enabled for release builds. Enabling shrinking ensures you are not shipping unused code with your APKs.
- Configuring Your App for Multidex with Gradle- How? 1.Change your Gradle build configuration to enable multidex.
避免 65K 限制- 如何?
- 检查您的应用程序的直接和传递依赖项 -确保您在应用程序中包含的任何大型库依赖项的使用方式都超过了添加到应用程序中的代码量。一个常见的反模式是包含一个非常大的库,因为一些实用方法是有用的。减少您的应用程序代码依赖项通常可以帮助您避免 dex 引用限制。
- 使用 ProGuard 删除未使用的代码- 为您的应用配置 ProGuard 设置以运行 ProGuard,并确保为发布版本启用收缩。启用收缩可确保您不会随 APK 一起发送未使用的代码。
- 使用 Gradle 为 Multidex 配置您的应用程序- 如何?1.更改您的 Gradle 构建配置以启用 multidex。
put
放
multiDexEnabled true
in the defaultConfig, buildType, or productFlavor sections of your Gradle build file.
在 Gradle 构建文件的 defaultConfig、buildType 或 productFlavor 部分中。
2.In your manifest add the MultiDexApplication class from the multidex support library to the application element.
2.在您的清单中,将 MultiDexApplication 类从 multidex 支持库添加到应用程序元素。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.multidex.myapplication">
<application
...
android:name="android.support.multidex.MultiDexApplication">
...
</application>
</manifest>
Note:If your app uses extends the Application class, you can override the attachBaseContext() method and call MultiDex.install(this) to enable multidex. For more information, see the MultiDexApplication reference documentation.
注意:如果您的应用使用扩展 Application 类,您可以覆盖 attachBaseContext() 方法并调用 MultiDex.install(this) 以启用 multidex。有关更多信息,请参阅 MultiDexApplication 参考文档。
Also this code may help you:
此代码也可以帮助您:
dexOptions {
javaMaxHeapSize "4g"
}
Put in your gradle(android{ ... } ).
放入你的 gradle(android{ ... } )。
回答by Doug Ray
Hi all I had the same issue that was being caused by a duplicate support version 4 file that I had included while trying to get parse integrated. Deleted the extra inclusion from the libs directory and it works fine now!
大家好,我遇到了同样的问题,该问题是由我在尝试集成解析时包含的重复支持版本 4 文件引起的。从 libs 目录中删除了额外的包含,现在可以正常工作了!
回答by sahil shekhawat
This happens when a library is getting compiled twice (i.e it is added two time). It can be support library or any other, it doesn't matter.
The common case is that you have added a compile statement of a library which is already in your libs/
directory. All the *.jar
files are compiled automatically. Thus, adding a compile statement is causing the error. Removing that statement might fix this issue. If this is not applicable then we already have some awesome answers.
当一个库被编译两次(即它被添加两次)时,就会发生这种情况。它可以是支持库或任何其他库,没关系。
常见的情况是您添加了一个库的编译语句,该库已经在您的libs/
目录中。所有*.jar
文件都是自动编译的。因此,添加编译语句会导致错误。删除该语句可能会解决此问题。如果这不适用,那么我们已经有了一些很棒的答案。
回答by amalBit
This might be the dumbest answer, but this worked for me :
这可能是最愚蠢的答案,但这对我有用:
- I removed and added all the libraries on my project manually.(One after the other) And voila, it worked.
- Build -> Rebuild project
- 我手动删除并添加了项目中的所有库。(一个接一个)瞧,它起作用了。
- 构建 -> 重建项目
Note: No library of mine was compiled twice.
注意:我的库没有被编译两次。
回答by pyus13
Make sure you have downloaded Support Repository to use support library dependency in build.gradle.
确保您已下载 Support Repository 以在 build.gradle 中使用支持库依赖项。
If these all are there already installed sync your project with gradle once using the button available.
如果这些都已安装,请使用可用按钮将您的项目与 gradle 同步一次。
回答by Eneas Gesing
In my case TOP LEVEL EXCEPTION was throw because of a special char in project path. Just closed the project, changed "á" to "a" and reopened the project. Works!
在我的情况下,由于项目路径中的特殊字符,TOP LEVEL EXCEPTION 被抛出。刚刚关闭项目,将“á”更改为“a”并重新打开项目。作品!