android - 在一个 gradle 文件中设置所有依赖项?

标签 android gradle build.gradle

我有什么

我的 android 项目中有两个不同的模块:

  • app:这是主要的应用程序android模块,几乎所有应用程序的代码都位于其中。
  • core:这是另一个模块,我想在其中指定将在应用程序模块中使用的所有基本代码,例如 BaseFragment、BaseViewModel 或我想在应用程序模块中实现的接口(interface)。

在核心 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

错误表明无法解析引用,就像它不存在一样。

一些想法?

提前致谢!!

最佳答案

我查看了你的 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 - 在一个 gradle 文件中设置所有依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68376122/

相关文章:

android - 我忘记了我的 keystore 密码,我想在 Google Play 上安装我的 apk。我该怎么办?

android - 在 android 中快速和频繁的位置更新..?

android - 在 TabLayout 中导航时打开键盘

android - JavaCV和FFmpegMediaMetadataRetriever:DuplicateFileException

gradle:如何共享包含我是唯一拥有的信息的build.gradle?

gradle - Gradle-从存储库构建包含 war 的耳朵

android - 使用 APK 拆分进行发布但不使用调试构建类型

java - 可扩展 ListView OnClickListeners

java - 想要从 Gradle fat jar 中删除特定的 jar

android - 多卡 1.4.20 "Nothing to document"