在一个 gradle 文件中设置所有依赖项?
Set up all dependencies in one gradle file?
我有什么
我的 android 项目中有两个不同的模块:
- app:这是主要的应用程序 android 模块,几乎所有的应用程序代码都位于此。
- 核心:这是另一个模块,我想在其中指定将在应用程序模块中使用的所有基本代码,如 BaseFragment、BaseViewModel 或我想在应用程序模块中实现的接口。
在核心 build.gradle
文件中,我有所有这些依赖项:
//Moshi
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
//Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$moshi_converter_version"
//okhttp
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_loggin_interceptor"
//Koin
implementation "io.insert-koin:koin-android:$koin_version"
implementation "io.insert-koin:koin-androidx-viewmodel:$koin_viewmodel_version"
我不希望在应用 build.gradle
文件中声明所有这些依赖项。
我正在阅读一些博客和 Gradle official documentation,但我无法成功实现。
我想要的
我想“继承”核心模块gradle文件中的所有依赖项
项目的结构应该是这样的:
core
|_build.gradle (common dependencies)
|_app
|_build.gradle (just specific dependencies)
我试过的
我了解到可以按如下方式实施核心项目(在应用 build.gradle 文件中):
dependencies {
implementation project (":core")
......
}
但是我在使用核心 类` 的应用程序模块中有一些 类 找不到。例如:
import com.example.core.presentation.base.BaseViewModel
错误提示无法解析引用,就像它不存在一样。
一些想法?
提前致谢!!
实现此目的的最简单方法是使用约定插件。不要让插件部分吓到你,它的设置有点复杂,但并不复杂。
那里有 3 个选项:
- 将插件放在 buildSrc 目录中并让 gradle 自动选取它
- 将插件放入特定模块并包含构建(复合构建)
- 将插件放在特定的模块中,释放它并加载它作为二进制依赖插件。
可以在此处找到更多信息:
https://docs.gradle.org/current/samples/sample_convention_plugins.html
或者,如果您只想在 1 个地方维护版本,但不介意将依赖项放在两个构建 gradle 文件中,那么平台可能更容易:https://docs.gradle.org/current/userguide/java_platform_plugin.html
我看了你的 GitHub :-)
我认为这就是您正在尝试的示例
https://github.com/smoralb/BaseApplication/tree/feature/core?
你在问题中描述的应该有效,所以我看了一下。
问题出在你的核心模块build.gradle
第1行
(https://github.com/smoralb/BaseApplication/blob/feature/core/core/build.gradle#L1)
你有这个:
apply plugin: 'com.android.application'
什么时候你应该拥有这个:
apply plugin: 'com.android.library'
然后使用
implementation project (":core")
在您的应用程序模块内,您可以访问 app
中 core
的 类。
我有什么
我的 android 项目中有两个不同的模块:
- app:这是主要的应用程序 android 模块,几乎所有的应用程序代码都位于此。
- 核心:这是另一个模块,我想在其中指定将在应用程序模块中使用的所有基本代码,如 BaseFragment、BaseViewModel 或我想在应用程序模块中实现的接口。
在核心 build.gradle
文件中,我有所有这些依赖项:
//Moshi
implementation "com.squareup.moshi:moshi-kotlin:$moshi_version"
//Retrofit
implementation "com.squareup.retrofit2:retrofit:$retrofit_version"
implementation "com.squareup.retrofit2:converter-moshi:$moshi_converter_version"
//okhttp
implementation "com.squareup.okhttp3:okhttp:$okhttp3_version"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttp_loggin_interceptor"
//Koin
implementation "io.insert-koin:koin-android:$koin_version"
implementation "io.insert-koin:koin-androidx-viewmodel:$koin_viewmodel_version"
我不希望在应用 build.gradle
文件中声明所有这些依赖项。
我正在阅读一些博客和 Gradle official documentation,但我无法成功实现。
我想要的
我想“继承”核心模块gradle文件中的所有依赖项
项目的结构应该是这样的:
core
|_build.gradle (common dependencies)
|_app
|_build.gradle (just specific dependencies)
我试过的
我了解到可以按如下方式实施核心项目(在应用 build.gradle 文件中):
dependencies {
implementation project (":core")
......
}
但是我在使用核心 类` 的应用程序模块中有一些 类 找不到。例如:
import com.example.core.presentation.base.BaseViewModel
错误提示无法解析引用,就像它不存在一样。
一些想法?
提前致谢!!
实现此目的的最简单方法是使用约定插件。不要让插件部分吓到你,它的设置有点复杂,但并不复杂。
那里有 3 个选项:
- 将插件放在 buildSrc 目录中并让 gradle 自动选取它
- 将插件放入特定模块并包含构建(复合构建)
- 将插件放在特定的模块中,释放它并加载它作为二进制依赖插件。
可以在此处找到更多信息: https://docs.gradle.org/current/samples/sample_convention_plugins.html
或者,如果您只想在 1 个地方维护版本,但不介意将依赖项放在两个构建 gradle 文件中,那么平台可能更容易:https://docs.gradle.org/current/userguide/java_platform_plugin.html
我看了你的 GitHub :-)
我认为这就是您正在尝试的示例
https://github.com/smoralb/BaseApplication/tree/feature/core?
你在问题中描述的应该有效,所以我看了一下。
问题出在你的核心模块build.gradle
第1行
(https://github.com/smoralb/BaseApplication/blob/feature/core/core/build.gradle#L1)
你有这个:
apply plugin: 'com.android.application'
什么时候你应该拥有这个:
apply plugin: 'com.android.library'
然后使用
implementation project (":core")
在您的应用程序模块内,您可以访问 app
中 core
的 类。