Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio adding rxjava library

Consider following project structure:

MainProject
-.idea
-.grandle
-src
-SubProject
--libs //I created this folder manually
---rxjava-core-0.16.0-sources.jar
--src
---main //+ all the sources
--build.grandle
--SubProject.iml
-build.grandle
-//other files

I downloaded the .jar from http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.netflix.rxjava%22%20AND%20a%3A%22rxjava-core%22 (sources.jar) - but I also tried others

Then I created lib folder in the SubProject and then put the .jar to it.

In Android Studio I rightclicked on the library and selected "Add as Library..." with level: "Project Library" and module: "SubProject".

The rxjava uses package name "rx".I have some code implemented that imports this package:

import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.Subscriptions;

When building the project following error occure:

Gradle: package rx does not exist
Gradle: package rx.util.functions does not exist
Gradle: cannot find symbol class Action1
...

I found that it is required to put a line to SubProject/build.grandle:

dependencies {
    compile 'libs/rxjava-core-0.16.0-sources.jar' //added line
    compile 'com.android.support:support-v4:19.0.0'
    compile 'com.android.support:appcompat-v7:19.0.0'
}

but then it throws:

Gradle: A problem occurred evaluating project ':SubProject'.
> The description libs/rxjava-core-0.16.0-sources.jar is invalid

I tried to moving the .jar around the project structure but so far no luck.

How do I properly add a 3rd party library to the project? Is it ok that I created the "libs" folder myself?

like image 350
Vojtech B Avatar asked Jan 11 '14 09:01

Vojtech B


People also ask

Is RxJava still used in Android?

RxJava is a very common tool used for asynchronous calling in Android development and provides a great alternative to typical asynchronous development.

What is RxJava in Android Studio?

RxJava is a JVM library that uses observable sequences to perform asynchronous and event-based programming. Its primary building blocks are triple O's, which stand for Operator, Observer, and Observables. And we use them to complete asynchronous tasks in our project. It greatly simplifies multithreading in our project.

How does RxJava work on Android?

RxJava is a Java VM implementation of ReactiveX a library for composing asynchronous and event-based programs by using observable sequences. The building blocks of RxJava are Observables and Subscribers. Observable is used for emitting items and Subscriber is used for consuming those items.

How do you use RxJava with retrofit?

To use RxJava in retrofit environment we need to do just two major changes: Add the RxJava in Retrofit Builder. Use Observable type in the interface instead of Call.


2 Answers

you could just try adding:

compile 'com.netflix.rxjava:rxjava-android:0.16.1'

and dont forget to click "Sync Project With Gradle Files" button.

like image 97
Reza Avatar answered Sep 22 '22 13:09

Reza


The latest dependency for gradle should be:

compile 'io.reactivex:rxjava:1.0.10'

Ref this link.

like image 26
peacepassion Avatar answered Sep 21 '22 13:09

peacepassion