Problems using rxjava-android

2019-05-05 19:10发布

问题:

I'm trying tu use Rxjava in a project that I have with gradle. I dont have any compile error, but when I run the project my app crash.

12-01 14:40:41.553    1015-1015/? I/dalvikvm﹕ Could not find method rx.Subscription.unsubscribe, referenced from method com.flipper83.protohipster.view.feed.ui.HipsterListFragment.onPause
12-01 14:40:41.553    1015-1015/? W/dalvikvm﹕ VFY: unable to resolve interface method 11579: Lrx/Subscription;.unsubscribe ()V
12-01 14:40:41.553    1015-1015/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0005
12-01 14:40:41.553    1015-1015/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lrx/Observable;)
12-01 14:40:41.565    1015-1015/? I/dalvikvm﹕ Could not find method rx.concurrency.Schedulers.newThread, referenced from method com.flipper83.protohipster.view.feed.ui.HipsterListFragment.onResume
12-01 14:40:41.565    1015-1015/? W/dalvikvm﹕ VFY: unable to resolve static method 11581: Lrx/concurrency/Schedulers;.newThread ()Lrx/Scheduler;
12-01 14:40:41.569    1015-1015/? D/dalvikvm﹕ VFY: replacing opcode 0x71 at 0x0014
12-01 14:40:41.569    1015-1015/? I/dalvikvm﹕ Failed resolving Lcom/flipper83/protohipster/view/feed/ui/HipsterListFragment$1; interface 2027 'Lrx/Observer;'
12-01 14:40:41.569    1015-1015/? W/dalvikvm﹕ Link of class 'Lcom/flipper83/protohipster/view/feed/ui/HipsterListFragment$1;' failed
12-01 14:40:41.569    1015-1015/? D/dalvikvm﹕ DexOpt: unable to opt direct call 0x148a at 0x1e in Lcom/flipper83/protohipster/view/feed/ui/HipsterListFragment;.onResume
12-01 14:40:41.577    1015-1015/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lrx/Observer;)
12-01 14:40:41.577    1015-1015/? W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lrx/Observer;)
12-01 14:40:41.577    1015-1015/? I/dalvikvm﹕ Could not find method rx.Observer.onNext, referenced from method com.flipper83.protohipster.view.feed.model.FeedViewModel.notifyFeed
12-01 14:40:41.577    1015-1015/? W/dalvikvm﹕ VFY: unable to resolve interface method 11578: Lrx/Observer;.onNext (Ljava/lang/Object;)V
12-01 14:40:41.577    1015-1015/? D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0028
12-01 14:40:41.577    1015-1015/? I/dalvikvm﹕ Failed resolving Lcom/flipper83/protohipster/view/feed/model/FeedViewModel$1; interface 2025 'Lrx/Observable$OnSubscribeFunc;'
12-01 14:40:41.577    1015-1015/? W/dalvikvm﹕ Link of class 'Lcom/flipper83/protohipster/view/feed/model/FeedViewModel$1;' failed
12-01 14:40:41.577    1015-1015/? E/dalvikvm﹕ Could not find class 'com.flipper83.protohipster.view.feed.model.FeedViewModel$1', referenced from method com.flipper83.protohipster.view.feed.model.FeedViewModel.populateFeed
12-01 14:40:41.581    1015-1015/? W/dalvikvm﹕ VFY: unable to resolve new-instance 794 (Lcom/flipper83/protohipster/view/feed/model/FeedViewModel$1;) in Lcom/flipper83/protohipster/view/feed/model/FeedViewModel;
12-01 14:40:41.581    1015-1015/? D/dalvikvm﹕ VFY: replacing opcode 0x22 at 0x0000
12-01 14:40:41.581    1015-1015/? I/dalvikvm﹕ Failed resolving Lcom/flipper83/protohipster/view/feed/model/FeedViewModel$1; interface 2025 'Lrx/Observable$OnSubscribeFunc;'
12-01 14:40:41.581    1015-1015/? W/dalvikvm﹕ Link of class 'Lcom/flipper83/protohipster/view/feed/model/FeedViewModel$1;' failed
12-01 14:40:41.581    1015-1015/? D/dalvikvm﹕ DexOpt: unable to opt direct call 0x145d at 0x02 in Lcom/flipper83/protohipster/view/feed/model/FeedViewModel;.populateFeed
12-01 14:40:41.585    1015-1015/? D/AndroidRuntime﹕ Shutting down VM
12-01 14:40:41.585    1015-1015/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa62ca288)
12-01 14:40:41.585    1015-1015/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.flipper83.protohipster.view.feed.model.FeedViewModel$1
            at com.flipper83.protohipster.view.feed.model.FeedViewModel.populateFeed(FeedViewModel.java:43)

I believe that the problem comes from gradle. this is my gradle build.xml.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.6.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
    flatDir {
        dirs 'lib'
    }
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 19
    }

    sourceSets {
        main {
            assets.srcDirs = ['assets']
        }
    }

}

dependencies {
    compile 'com.squareup.picasso:picasso:2+'
    compile 'com.squareup.dagger:dagger-compiler:1.1.0'
    compile 'com.squareup.dagger:dagger:1.1.0'
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.squareup.retrofit:retrofit:1.2.+'
    compile 'com.netflix.rxjava:rxjava-android:0.14.+'
    compile files('lib/Parse-1.3.8.jar')
}

Any Idea??

回答1:

Yes, I fixed it with:

compile 'com.netflix.rxjava:rxjava-core:0.+'
compile 'com.netflix.rxjava:rxjava-android:0.+'
compile 'com.squareup.retrofit:retrofit:1.5.0'

in my build.gradle.

Thank you



回答2:

compile 'com.netflix.rxjava:rxjava-core:0.+' is transient dependency by compile 'com.netflix.rxjava:rxjava-android:0.+', and Gradle automatics resolved this dependency and download



回答3:

Based on ReactiveX/RxAndroid : https://github.com/ReactiveX/RxAndroid

Its better to use both android and java dependencies:

compile 'io.reactivex:rxandroid:1.0.1'
// Because RxAndroid releases are few and far between, it is recommended you also
// explicitly depend on RxJava's latest version for bug fixes and new features.
compile 'io.reactivex:rxjava:1.0.14'