Android库项目抛出java.lang.NoClassDefFoundError:解析失败:Lrx / Observer

时间:2017-03-06 07:22:59

标签: android rx-java rx-android

我有一个使用Rx的库,我将这个库打包为aar,可以在其他项目中使用。我能够生成aar但是当我尝试在其他项目中访问它时,它会抛出 java.lang.NoClassDefFoundError:解析失败:Lrx / Observer;

引起:java.lang.ClassNotFoundException:在路径上找不到类“rx.Observer”:DexPathList

也许我正在编写build.gradle错误或不排除某些内容。非常感谢任何帮助。

我的库的build.gradle看起来像这样

compile 'io.reactivex:rxandroid:1.0.1'
compile 'io.reactivex:rxjava:1.0.14

我们可以在库项目中使用Rx并将其导出为aar吗?非常感谢任何帮助。

编辑:添加完整的gradle文件。

图书馆Gradle文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
    }
}

apply plugin: 'com.android.library'

apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.jakewharton.butterknife'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            consumerProguardFiles 'proguard-rules.pro'
        }
    }

    packagingOptions {
        exclude '.readme'
        exclude 'LICENSE.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/README.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'META-INF/MANIFEST.MF'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
//    compile project(path: ':backend', configuration: 'android-endpoints')

    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:gridlayout-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'

    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.jakewharton.picasso:picasso2-okhttp3-downloader:1.1.0'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
    provided 'org.glassfish:javax.annotation:10.0-b28'

    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'io.reactivex:rxjava:1.0.14'
    compile 'com.artemzin.rxjava:proguard-rules:1.2.7.0'

    compile 'com.google.api-client:google-api-client:1.22.0'
    compile 'com.google.api-client:google-api-client-android:1.22.0'


    compile 'com.google.android.gms:play-services-base:10.0.1'
    compile 'com.google.android.gms:play-services-location:10.0.1'
    compile 'com.google.android.gms:play-services-places:10.0.1'
    compile 'com.google.firebase:firebase-messaging:10.0.1'

    compile 'com.android.support:appcompat-v7:25.1.1'
    testCompile 'junit:junit:4.12'

}

Proguard文件:

# Dagger
-dontwarn dagger.internal.codegen.**
-keepclassmembers,allowobfuscation class * {
    @javax.inject.* *;
    @dagger.* *;
    <init>();
}
-keep class dagger.* { *; }
-keep class javax.inject.* { *; }
-keep class * extends dagger.internal.Binding
-keep class * extends dagger.internal.ModuleAdapter
-keep class * extends dagger.internal.StaticInjection

*编辑*

更新了库项目的gradle文件

apt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.sothree.slidinguppanel:library:3.3.1'

compile 'io.reactivex:rxandroid:1.2.1'
compile 'io.reactivex:rxjava:1.2.7'
compile 'com.artemzin.rxjava:proguard-rules:1.2.7.0'

更新proguard-rules.pro

# Dagger
-dontwarn dagger.internal.codegen.**
-keepclassmembers,allowobfuscation class * {
    @javax.inject.* *;
    @dagger.* *;
    <init>();
}
-keep class dagger.* { *; }
-keep class javax.inject.* { *; }
-keep class * extends dagger.internal.Binding
-keep class * extends dagger.internal.ModuleAdapter
-keep class * extends dagger.internal.StaticInjection

-dontwarn sun.misc.**

-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
   long producerIndex;
   long consumerIndex;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode producerNode;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode consumerNode;
}

-dontnote rx.internal.util.PlatformDependent

我将生成的aar添加为模块,这是另一个应用程序的依赖项。

我仍然收到此错误

java.lang.NoClassDefFoundError: Failed resolution of: Lrx/Observer;

1 个答案:

答案 0 :(得分:0)

您需要添加

-dontwarn sun.misc.**

-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
   long producerIndex;
   long consumerIndex;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode producerNode;
}

-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode consumerNode;
}

-dontnote rx.internal.util.PlatformDependent

有关详细信息,请参阅https://github.com/artem-zinnatullin/RxJavaProGuardRules

到您的proguard文件。

PS:您使用的是旧版本的RxJava。您可能希望升级到1.2.7版甚至RxJava2。

编辑:您可能还需要添加类似

的内容
compile ('com.somepackage:LIBRARY_NAME:1.0.0@aar'){
    transitive=true
}

而不仅仅是

compile ('com.somepackage:LIBRARY_NAME:1.0.0@aar')

在使用aar的项目的build.gradle文件中。这告诉gradle下载你所需的任何依赖项。