Why Would Dependencies Be Included Only In Release Builds?

Starting with a brand-new Android Studio 2.2.3 project, accepting all defaults except opting out of AppCompat, I added two dependencies to 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'
}

There are no problems at compile time, even if I reference classes from Gson or OkHttp. However, I run the app from Android Studio and get the dreaded 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

If I use Android Studio's APK Analyzer to analyze the app-debug.apk , I see that nothing from my two dependencies is included in the APK, except a reference to GsonBuilder :

调试版的APK分析器

I get the same results from a command-line build ( gradle clean assembleDebug ).

However, a release build shows the APK with the dependencies included:

发布版APK分析器

If I switch to older versions of these artifacts ( com.google.code.gson:gson:2.3 and com.squareup.okhttp3:okhttp:3.2.0 ), they show up properly in debug builds. The problem is per-artifact, so if I include the older Gson and the newer OkHttp, my debug builds contain Gson classes by not OkHttp ones.

So, to recap: for these specific versions of these specific artifacts, for debug builds, the contents of the artifacts are not added to my APK.

The question is: why?


The problem was some sort of corruption in the new build cache. Wiping out the build cache cleared up the problem.

I filed an issue about this, though since I do not have a reproducible scenario for corrupting the build cache, the issue may be of limited utility.

链接地址: http://www.djcxy.com/p/94268.html

上一篇: 计算console.log对象

下一篇: 为什么相关性只包含在发布版本中?