I'm aware of this question: How to add dependencies to project's top-level build.gradle file?
But it doesn't give the answer I need.
Lets say my android project has 3 library modules A
, B
and C
.
And all three uses a common dependency say D1
where D1
is library like Dagger
, Room
, etc.
Now instead of adding D1
to all three, I am thinking about adding D1
to project level build.gradle
.
I tried doing something like this but it gives error as:
Could not find method implementation() for arguments [directory 'libs'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
buildscript {
ext.kotlin_version = '1.3.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.google.dagger:dagger-android-support:2.15'
implementation "com.google.dagger:dagger:2.16"
kapt "com.google.dagger:dagger-android-processor:2.16"
kapt "com.google.dagger:dagger-compiler:2.16"
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
What is the correct way of adding dependencies in project level build.gradle
?
Here is the project link: https://github.com/cerberusv2px/android-modular
Create a new module project called, e.g. common-libs
, and put all your common dependencies into this project, open the common-libs/build.gradle
.
change
implementation fileTree(dir: 'libs', include: ['*.jar'])
To
api fileTree(dir: 'libs', include: ['*.jar'])
And then for all the upstream depending projects, just call
dependencies {
...
api project(':common-libs')
}
See: https://developer.android.com/studio/build/dependencies#dependency_configurations
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With