简体   繁体   English

如何将项目模块库暴露给 App 模块(Android Studio)

[英]How to expose Project Module Library to App Module (Android Studio)

At first, I am using a single module app for the the Android Application, but now I want to separate the module into app module (core-business logic and UI) and libraries module (HTTP-related and base abstract classes).起初,我为 Android 应用程序使用单个模块app程序,但现在我想将模块分为app模块(核心业务逻辑和 UI)和libraries模块(HTTP 相关和基本抽象类)。

I made new libraries module as the library module and start moving some utility code from app to libraries but problem comes.我将新的libraries模块作为库模块,并开始将一些实用程序代码从app移动到libraries ,但问题来了。 The problem is, I want my app to use the dependency used in libraries module without having to include same dependency in both build.gradle.问题是,我希望我的app使用libraries模块中使用的依赖项,而不必在两个 build.gradle 中包含相同的依赖项。

In my app module, I want my source code to use the RecyclerView and Retrofit defined in libraries , but I can't do it unless I also include retrofit and recyclerview-v7 in my app/build.gradle.在我的app模块中,我希望我的源代码使用libraries中定义的RecyclerViewRetrofit ,但除非我在我的 app/build.gradle 中还包含retrofitrecyclerview-v7 ,否则我不能这样做。

app/main/src/..../HomeActivity.kt应用程序/main/src/..../HomeActivity.kt

var gridDataset: RecyclerView? = null // RecyclerView not found in classpath

app/build.gradle应用程序/build.gradle

implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation project(":libraries")

libraries/build.gradle库/build.gradle

implementation "com.android.support:recyclerview-v7:$support_library_version"
implementation "com.squareup.retrofit2:retrofit:2.4.0"
implementation "com.squareup.retrofit2:converter-gson:2.4.0"
implementation "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.8.0"

Where did I go wrong?我哪里做错了? Or is it simply impossible to do that?或者根本不可能做到这一点?

specify your dependencies in libraries/build.gradle using api instead of implementation使用 api 而不是实现在 library/build.gradle 中指定您的依赖项

This should add these dependencies to the class path for any module depending on your libraries module这应该将这些依赖项添加到任何模块的类路径中,具体取决于您的库模块

api "com.android.support:recyclerview-v7:$support_library_version"
api "com.squareup.retrofit2:retrofit:2.4.0"
api "com.squareup.retrofit2:converter-gson:2.4.0"
api "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
api "com.squareup.okhttp3:logging-interceptor:3.8.0"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM