问题描述
我正在将现有应用从 Eclipse 转换为 Android Studio.但是,当我在运行 4.x 的设备上运行它时(我已经在模拟器上测试了几个版本),它立即崩溃并出现 NoClassDefFoundError
.
I'm in the process of converting an existing app from Eclipse to Android Studio. However, when I run it on a device running 4.x (I've tested several versions on emulators), it immediately crashes with a NoClassDefFoundError
.
我已经尝试注释掉对它找不到的类的引用,但总会有另一个.据我所知,有问题的班级总是
I've tried commenting out references to the classes it can't find, but there's always another. As far as can tell, the offending class is always
- 在我自己的代码中
- 一个内部类(更新:经过更多调查,这并不总是正确的)
- Within my own code
- An inner class (Update: With more investigation, this one isn't always true)
在 5.0.1 模拟器上一切正常(我没有要测试的设备).我的 build.gradle
文件相当长,但看起来像这样:
Everything works fine on a 5.0.1 emulator (I don't have a device to test on). My build.gradle
file is fairly long, but looks like this:
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = "2.7.1"
android {
compileSdkVersion 19
buildToolsVersion "21.1.1"
defaultConfig {
applicationId "com.myapp.android"
minSdkVersion 8
targetSdkVersion 19
multiDexEnabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
packagingOptions {
*snip*
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
}
repositories {
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
resourcePackageName 'com.pact.android'
}
}
dependencies {
*snip compile project(':...') declarations
apt "com.googlecode.androidannotations:androidannotations:$AAVersion"
compile "com.googlecode.androidannotations:androidannotations-api:$AAVersion"
compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.google.android.gms:play-services:3.1.36'
*snip many more compile declarations*
compile fileTree(dir: 'libs', include: ['*.jar'])
}
导致麻烦的类的一些例子:
Some examples of classes that cause trouble:
- 在 facebook 库中实现接口的匿名类(作为库项目)
Parcelable.CREATOR
在我的模型中实现- 扩展 android.os.Library 的内部类
- 我自己代码中的内部类
- 在 maven 库中实现接口的类
出了什么问题,我该如何解决?
What's going wrong, and how do I fix it?
推荐答案
我没有完全实现 MultiDex 支持,所以我的一些类不在正确的 dex 文件中.要修复它,您需要做的不仅仅是在 defaultConfig
块中设置 multiDexEnabled = true
.您还必须:
I was incompletely implementing MultiDex support, so some of my classes weren't in the proper dex file. To fix it, you have to do more than just set multiDexEnabled = true
in your defaultConfig
block. You also have to:
- 在您的依赖项中包含
compile 'com.android.support:multidex:1.0.1'
- 让您的应用程序类扩展
MultiDexApplication
而不仅仅是Application
.或者,您可以在应用程序的attachBaseContext()
中调用MultiDex.install()
.
- Include
compile 'com.android.support:multidex:1.0.1'
in your dependencies - Have your Application class extend
MultiDexApplication
instead of justApplication
. Alternatively, you can callMultiDex.install()
inattachBaseContext()
of your application.
请参阅 https://developer.android.com/tools/building/multidex.html 了解更多细节.
See https://developer.android.com/tools/building/multidex.html for more details.
这篇关于Android 4 上 Android Studio 的 NoClassDefFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!