自定义库中包含的库在示例项目Android中不可用/不可见

时间:2018-11-09 15:18:42

标签: android android-gradle rx-java2 rx-android

我正在创建一个简单的库来调用天气api,在我的lib项目中,我使用RxJava和RxAndroid并进行了HTTP调用的改进。 我的WeatherService进行呼叫并以json形式接收结果,并且需要进行一些操作并将其作为Single<CurrentWeather>返回给客户端应用。 在我的lib项目中,我添加了rx和改造所需的所有依赖项。

我的图书馆成绩单:

ext{
    rxJava = "2.1.17"
    rxAndroid = "2.0.2"
    support = "27.1.1"
    retrofitVersion = "2.4.0"
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation "com.android.support:appcompat-v7:$support"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"
    implementation "com.squareup.retrofit2:adapter-rxjava2:$retrofitVersion"

    implementation "io.reactivex.rxjava2:rxandroid:$rxAndroid"
    implementation "io.reactivex.rxjava2:rxjava:$rxJava"
}

现在在MainActivity的示例应用程序中,我想测试一个函数以查看它是否按预期工作,但是当我尝试调用返回Single的函数时,它显示易出错错误:

error: cannot access Single
class file for io.reactivex.Single not found

lib函数声明:

public Single<Current> getCurrentWeather(double lat, double lng) {
    return mClient.getWeather(lat + "," + lng).flatMap(new Function<JSONObject, SingleSource<Current>>() {
      @Override
      public SingleSource<Current> apply(JSONObject jsonObject) throws Exception {
        //parse jsonObject and return as Single<Current>

        return Single.create(new SingleOnSubscribe<Current>() {

          @Override
          public void subscribe(SingleEmitter<Current> emitter) throws Exception {
            Current current = new Current();
            emitter.onSuccess(current);
          }
        });
      }
    });
  }

客户端应用程序MainActivity:这是编译错误。

WeatherService.getIsnstance().getCurrentWeather(32.5554, 35.545)

客户端应用gradle:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation project(":weatherlib")
}

客户端应用设置。gradle:

include ':app', ':weatherlib'

我在这里想念什么吗?

这是否意味着每个希望使用此lib的人都必须在gradle中添加rx依赖项?

1 个答案:

答案 0 :(得分:0)

对于库的依赖项,您需要使用api而不是implementation

See here了解它们之间的区别。