Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to manage common dependencies for multiple Android library projects?

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

like image 944
Sujin Shrestha Avatar asked Nov 26 '18 06:11

Sujin Shrestha


1 Answers

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

like image 73
shizhen Avatar answered Sep 28 '22 19:09

shizhen