在Android Studio上使用外部库时出现NoClassDefFoundError

时间:2016-09-15 06:25:01

标签: android gradle android-gradle

我正在为我的项目使用几个库,我使用build.gradle添加依赖项,例如Volley和Caligraphy。我试图运行应用程序并且项目构建得很好,但是当应用程序试图访问其中一个外部库时,我得到了这个错误:

09-15 12:35:30.037 20659-20659/com.node8.peek:background_crash E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.node8.peek:background_crash, PID: 20659
  java.lang.NoClassDefFoundError: Failed resolution of: Luk/co/chrisjenx/calligraphy/CalligraphyConfig$Builder;
      at com.node8.peek.PeekApplication.onCreate(PeekApplication.java:48)
      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012)
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4593)
      at android.app.ActivityThread.access$1600(ActivityThread.java:154)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
      at android.os.Handler.dispatchMessage(Handler.java:102)
      at android.os.Looper.loop(Looper.java:135)
      at android.app.ActivityThread.main(ActivityThread.java:5294)
      at java.lang.reflect.Method.invoke(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:372)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
   Caused by: java.lang.ClassNotFoundException: Didn't find class "uk.co.chrisjenx.calligraphy.CalligraphyConfig$Builder" on path: DexPathList[[zip file "/data/app/com.node8.peek-6/base.apk"],nativeLibraryDirectories=[/data/app/com.node8.peek-6/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.node8.peek.PeekApplication.onCreate(PeekApplication.java:48) 
      at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1012) 
      at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4593) 
      at android.app.ActivityThread.access$1600(ActivityThread.java:154) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5294) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
    Suppressed: java.lang.ClassNotFoundException: uk.co.chrisjenx.calligraphy.CalligraphyConfig$Builder
      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)
            ... 13 more
   Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我已经尝试清理项目并使Invalidate Cache&在Android Studio上重新启动,但我仍然遇到错误。可能导致此错误的原因是什么?这是我的build.gradle:

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://maven.fabric.io/public' }
    }
    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
        classpath 'com.google.gms:google-services:3.0.0'
    }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'
apply plugin: 'android'
android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
    }
    defaultConfig {
        applicationId "com.node8.peek"
        minSdkVersion 21
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
}
repositories {
    maven { url "http://jzaccone.github.io/SlidingMenu-aar" }
    mavenCentral()
    jcenter();
    maven {
        url "https://jitpack.io"
    }
    maven { url 'https://maven.fabric.io/public' }
}
dependencies {
    compile 'com.devbrackets.android:exomedia:3.0.2'

    compile 'com.github.castorflex.verticalviewpager:library:19.0.1'
    compile 'uk.co.chrisjenx:calligraphy:2.2.0'
    compile 'com.jeremyfeinstein.slidingmenu:library:1.3@aar'
    compile 'net.simonvt.menudrawer:menudrawer:3.0.+@aar'
    compile 'com.sothree.slidinguppanel:library:3.3.0'
    compile 'com.nikoyuwono:toolbar-panel:0.1.1'
    compile 'com.android.support:design:22.2.1'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'com.google.android.gms:play-services:9.2.0'
    compile 'com.splitwise:tokenautocomplete:2.0.7@aar'
    compile 'com.klinkerapps:android-chips:1.0.1@aar'
    compile('org.apache.httpcomponents:httpmime:4.3.5') {
        exclude module: 'org.apache.httpcomponents:httpclient'
    }
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile('com.github.ganfra:material-spinner:1.1.1') {
        exclude group: 'com.android.support', module: 'appcompat-v7'
    }
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'com.facebook.android:facebook-android-sdk:4.8.0'
    compile 'com.android.support:recyclerview-v7:22.2.1'
    compile files('libs/javacpp.jar')
    compile files('libs/twitter4j-core-4.0.4.jar')
    compile files('libs/javacv.jar')
    compile project(':volley')
    compile('com.twitter.sdk.android:twitter:1.13.1@aar') {
        transitive = true;
    }
    compile 'com.google.android.gms:play-services-maps:9.2.0'
    //compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'com.google.maps.android:android-maps-utils:0.4'
    compile 'com.tumblr:jumblr:0.0.11'
    compile 'oauth.signpost:signpost-core:1.2.1.2'
    compile 'oauth.signpost:signpost-commonshttp4:1.2.1.2'
    compile 'com.theartofdev.edmodo:android-image-cropper:2.2.+'
    compile('com.crashlytics.sdk.android:crashlytics:2.6.0@aar') {
        transitive = true;
    }
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.github.jonisaa:MaterialCalendarview:v1.2.2'
    compile 'com.makeramen:roundedimageview:2.2.1'
    compile 'com.github.lzyzsd:circleprogress:1.1.0@aar'
    compile 'com.google.firebase:firebase-core:9.2.0'                        // this line must be included to integrate with Firebase
    compile 'com.google.firebase:firebase-messaging:9.2.0'
    compile 'com.google.android.gms:play-services-location:9.2.0'
    compile 'com.google.android.gms:play-services-places:9.2.0'
    compile 'com.timehop.stickyheadersrecyclerview:library:0.4.2@aar'

}
apply plugin: 'com.google.gms.google-services'

编辑:我已尝试从我的项目中删除书法库,但现在应用程序在我尝试访问凌空库时会出错。

Edit2:启用Instant Run解决了问题,但我不想使用Instant Run,因为它导致应用程序在首次安装时需要很长时间才能加载。为什么即时运行会导致NoClassDefFoundError?有没有办法在不打开Instant Run的情况下防止错误?

2 个答案:

答案 0 :(得分:1)

你可以尝试将构建工具例如.24,gradle plugin,lib,android studio更新到最新版本,并尝试重建项目。

答案 1 :(得分:0)

如果您使用的是Android Studio 2.0,请关闭即时运行,检查multidex 1.0.1是否正在使用并检查您是否正在调用MultiDex.init()在Calligraphy配置之前。希望它能解决这个问题。

参考:https://github.com/chrisjenx/Calligraphy/issues/224