从一个全新的Android Studio 2.2.3项目开始,除了选择退出AppCompat之外接受所有默认设置,我向app/build.gradle
添加了两个依赖项:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.commonsware.myapplication"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.google.code.gson:gson:2.4'
compile 'com.squareup.okhttp3:okhttp:3.5.0'
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testCompile 'junit:junit:4.12'
}
即使我从Gson或OkHttp引用类,编译时也没有问题。但是,我从Android Studio运行应用程序并获得可怕的NoClassDefFoundError
:
12-16 10:48:52.175 15687-15687/com.commonsware.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.commonsware.myapplication, PID: 15687
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/gson/GsonBuilder;
at com.commonsware.myapplication.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.gson.GsonBuilder" on path: DexPathList[[zip file "/data/app/com.commonsware.myapplication-1/base.apk"],nativeLibraryDirectories=[/data/app/com.commonsware.myapplication-1/lib/arm, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
at com.commonsware.myapplication.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6251)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Suppressed: java.lang.ClassNotFoundException: com.google.gson.GsonBuilder
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 14 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
如果我使用Android Studio的APK分析器来分析app-debug.apk
,我发现除了对GsonBuilder
的引用外,APK中不包含任何来自我的两个依赖项:
我从命令行构建(gradle clean assembleDebug
)得到了相同的结果。
但是,release
版本会显示包含依赖项的APK:
如果我切换到这些工件的早期版本(com.google.code.gson:gson:2.3
和com.squareup.okhttp3:okhttp:3.2.0
),它们会在debug
版本中正确显示。问题是每个工件,所以如果我包含较旧的Gson和较新的OkHttp,我的debug
构建包含不是OkHttp的Gson类。
因此,回顾一下:对于这些特定工件的特定版本,对于debug
版本,工件的内容不会添加到我的APK中。
问题是:为什么?
答案 0 :(得分:4)
the new build cache问题是某种腐败。擦除构建缓存可以解决问题。
我提交了an issue关于此问题,但由于我没有可重现的方案来破坏构建缓存,因此该问题的实用性可能有限。