Android Studio 添加 rxjava 库
题
考虑以下项目结构:
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
我从下载了 .jar http://search.maven.org/#search%7Cga%7C1%7Cg%3A%22com.netflix.rxjava%22%20AND%20a%3A%22rxjava-core%22 (sources.jar) - 但我也尝试过其他的
然后我在子项目中创建了 lib 文件夹,然后将 .jar 放入其中。
在Android Studio中,我在库上正确地插入“项目库”和模块:“子项目”。
rxjava 使用包名称“rx”。我实现了一些导入此包的代码:
import rx.Observable;
import rx.Observer;
import rx.Subscription;
import rx.subscriptions.Subscriptions;
构建项目时出现以下错误:
Gradle: package rx does not exist
Gradle: package rx.util.functions does not exist
Gradle: cannot find symbol class Action1
...
我发现需要在 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'
}
但随后它抛出:
Gradle: A problem occurred evaluating project ':SubProject'.
> The description libs/rxjava-core-0.16.0-sources.jar is invalid
我尝试在项目结构中移动 .jar,但到目前为止还没有成功。
如何正确地将第三方库添加到项目中?我自己创建“libs”文件夹可以吗?
解决方案
你可以尝试添加:
compile 'com.netflix.rxjava:rxjava-android:0.16.1'
并不要忘记单击“使用Gradle文件同步项目”按钮。
其他提示
首先你需要添加代码 compile 'libs/rxjava-core-0.16.0-sources.jar'
您可以在libs文件夹中选择该库,找到“同步+库名称”来重建它,然后就可以使用该库了。希望可以帮助你
不隶属于 StackOverflow