java.lang.NoClassDefFoundError: com.facebook.imagepipeline.producers.HttpUrlConnectionNetworkFetcher

java.lang.NoClassDefFoundError: com.facebook.imagepipeline.producers.HttpUrlConnectionNetworkFetcher

我正在尝试使用 Fresco,但当我尝试对其进行初始化时它立即崩溃了。我使用 Android Studio,并且一直按照位于 here.

的说明进行操作

在我的应用程序 class 中,我像教程中的 onCreate 方法一样初始化 Fresco,如下所示:

Fresco.initialize(this);

这是崩溃的堆栈跟踪:

09-25 18:31:05.896    7789-7789/com.myproject E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.facebook.imagepipeline.producers.HttpUrlConnectionNetworkFetcher
        at com.facebook.imagepipeline.core.ImagePipelineConfig.<init>(ImagePipelineConfig.java:126)
        at com.facebook.imagepipeline.core.ImagePipelineConfig.<init>(ImagePipelineConfig.java:61)
        at com.facebook.imagepipeline.core.ImagePipelineConfig$Builder.build(ImagePipelineConfig.java:378)
        at com.facebook.imagepipeline.core.ImagePipelineFactory.initialize(ImagePipelineFactory.java:77)
        at com.facebook.drawee.backends.pipeline.Fresco.initialize(Fresco.java:32)
        at com.myproject.MyProjectApplication.onCreate(MyProjectApplication.java:196)
        at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
        at android.app.ActivityThread.access00(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

这是我的 build.gradle 文件

buildscript {
  repositories {
    maven { url 'https://maven.fabric.io/public' }
    mavenCentral()
  }

  dependencies {
    classpath 'io.fabric.tools:gradle:1.+'
  }
}
apply plugin: 'com.android.application'
apply plugin: 'io.fabric'

android {
  compileSdkVersion 22
  buildToolsVersion "22.0.1"
  packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
    exclude 'META-INF/ASL2.0'
  }

  defaultConfig {
    applicationId "com.myproject"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 22
    versionName "0.0.22"
    multiDexEnabled true
  }
 buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
   }
 }
 dexOptions {
    javaMaxHeapSize "2g"
    jumboMode = true
 }
}

repositories {
  flatDir {
    dirs 'libs'
  }
  maven { url 'https://maven.fabric.io/public' }
}

dependencies {
  compile ('com.facebook.fresco:fresco:0.7.0+')
  compile 'com.android.support:multidex:1.0.0'
  compile(name:'httpmime-4.3.5', ext:'jar')
  compile(name:'httpcore-4.3.2', ext:'jar')
  ...
  compile 'com.facebook.android:facebook-android-sdk:4.5.0'
}

configurations {
  all*.exclude group: 'com.nineoldandroids', module: 'library'
}

更新

我在模拟器 运行 Android 版本 4.1.2 和实际的 phone 运行 4.4.4 上遇到了这个问题。在 Lollipop 设备上一切正常。 Fresco 支持 Gingerbread 及以上版本。

您可能已经超过了 65K 的方法限制。如果您尚未在 defaultConfig 中添加 'multiDexEnabled true',请添加。此外,为了在棒棒糖之前的设备上支持 MultiDex,您必须在依赖项中添加 MultiDex 兼容库。您的 build.gradle 文件将如下所示:

apply plugin: 'com.android.application'


android {
    compileSdkVersion 22
    buildToolsVersion "23.0.1"

defaultConfig {
    applicationId "in.justride"
    minSdkVersion 15
    targetSdkVersion 17
    versionCode 30
    versionName "3.0.0"
    **multiDexEnabled true**
}
buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}

android {
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
    }


    **splits {
        abi {
            enable true
            reset()
            include 'x86', 'x86_64', 'arm64-v8a', 'armeabi-v7a', 'armeabi'
            universalApk false
        }
    }**

}
}

repositories{
    maven {
        url "http://repository.codehaus.org/org/codehaus"
    }
    maven {
        url "https://jitpack.io"
    }
}


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.easing:library:1.0.1@aar'
    compile 'com.daimajia.androidanimations:library:1.1.3@aar'
    compile 'com.jakewharton:butterknife:7.0.1'
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta1'
    compile 'org.parceler:parceler:0.2.13'
    compile 'com.squareup.retrofit:converter-jackson:2.0.0-beta1'
    compile 'com.fasterxml.jackson.core:jackson-databind:2.4.3'
    compile 'com.facebook.android:facebook-android-sdk:4.6.0'
    compile 'com.google.android.gms:play-services-identity:8.1.0'
    compile 'com.google.android.gms:play-services-plus:8.1.0'
    compile 'com.google.android.gms:play-services-location:8.1.0'
    compile 'com.balysv:material-ripple:1.0.2'
    **compile 'com.facebook.fresco:fresco:0.7.0+'**
    compile 'com.android.support:recyclerview-v7:22.2.+'
    compile 'com.android.support:cardview-v7:22.2.+'
    **compile 'com.android.support:multidex:1.0.0'**
}

此外,在您的项目应用程序中 class 重写 attachBaseContext() 方法,如下所示:

@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

请告诉我这是否有帮助。使用 Split 是因为 Fresco 是一个本地库,因此每个 CPU 架构都有一组不同的二进制文件,并且为每个 CPU 构建了一个单独的 .apk,仅包含该 CPU架构。

更新

Multidex 支持默认包含在 ART (lollipop+) 中。在 lollipop 之前,multidex 支持是通过支持库提供的。因此,我们需要将它包含在依赖项中。 (如果最小 SDK < 21)