Sergio
Sergio

Reputation: 788

Set up all dependencies in one gradle file?

What I have

I have two different modules in my android project:

In the core build.gradle file, I have all of these dependencies:

    //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"

All of these dependencies I would like not to be declared also in the app build.gradle file.

I am reading some blogs and the Gradle official documentation but I am not able to successfully achieve this.

What I want

I want to "inherit" all the dependencies just from the core module gradle file

The structure of the project should be something like this:

core
|_build.gradle (common dependencies)
|_app
  |_build.gradle (just specific dependencies)

What I have tried

I have read that this is possible implementing the core project as follows (in the app build.gradle file):

dependencies {
  implementation project (":core")
  ......
}

But I have some classes in the app module that uses core classes` that could not find. For example:

import com.example.core.presentation.base.BaseViewModel

The error says that could not resolve the reference like it not exists.

Some ideas?

Thanks in advance!!

Upvotes: 2

Views: 1109

Answers (2)

Blundell
Blundell

Reputation: 76458

I took a look at your GitHub :-)

I think this is the example you are trying

https://github.com/smoralb/BaseApplication/tree/feature/core?

What you describe in your question should work, so I took a look.

The problem is in your core modules build.gradle line 1

(https://github.com/smoralb/BaseApplication/blob/feature/core/core/build.gradle#L1)

You have this:

apply plugin: 'com.android.application'

When you should have this:

apply plugin: 'com.android.library'

then using

implementation project (":core")

inside your app module, will give you access to the classes from core in app.

Upvotes: 1

p.streef
p.streef

Reputation: 3815

Simplest way to achieve this is with a convention plugin. Don't let the plugin part scare you too much, it's a bit of setup but not that complex.

You get 3 options there:

  1. put the plugins in a buildSrc dir and have gradle pick it up automatically
  2. put the plugin in a specific module and include the build (composite build)
  3. put the plugin in a specific module, release it and load it as a binary dependency plugin.

more info can be found here: https://docs.gradle.org/current/samples/sample_convention_plugins.html

alternatively, if you just want to maintain the versions in 1 place but don't mind putting the dependencies in both build gradle files a plaform might be even easier: https://docs.gradle.org/current/userguide/java_platform_plugin.html

Upvotes: 1

Related Questions