How to manage common dependencies for multiple Android library projects?

喜夏-厌秋 提交于 2019-12-23 20:03:09

问题


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


回答1:


I believe you are looking for subprojects instead of allprojects as the main module is not configured with any build options.




回答2:


Looking at Android Projects Built by Google itself, they usually create ExtraPropertiesExtension at the top level build.gradle file.

Doing so, you could create a global extension property to all gradle modules your project is using.

As a sample you could do something like this in your build.gradle:

ext.versions = [ 'dependencyVersion': 28 ]

and so in your module's build.gradle module, you culd reference to this attribute like so:

dependencies {
    ....
    implementation "com.some.dependency.you:use:${versions.dependencyVersion}"
}

Here's a link to Nick Butcher's App called Plaid:Plaid

The same pattern is used with the Google IO and Android Dev Summit App, You could at them here: Android Dev Summit App 2018 Google IO Schedule App 2018




回答3:


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




回答4:


Method that I use is

Using separate gradle file for common things in each module and applying that gradle file in the module's gradle file,

which I learned from this article.

Example :

common.gradle

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion Versions.TARGET_SDK
    buildToolsVersion Versions.BUILD_TOOLS


    defaultConfig {
        minSdkVersion Versions.MIN_SDK
        targetSdkVersion Versions.TARGET_SDK
        versionCode 1
        versionName "1.0"

        consumerProguardFiles 'consumer-rules.pro'
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    implementation AndroidDeps.KOTLIN_STD_LIB
    implementation AndroidDeps.APP_COMPAT
    implementation AndroidDeps.CORE_KTX

    implementation ToolingDeps.TIMBER
}

feature_stats/build.gradle

android{
    repositories {
        maven { url 'https://jitpack.io' }
    }

    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation AndroidDeps.CONSTRAINT_LAYOUT
    implementation AndroidDeps.NAV_FRAGMENT_KTX
    implementation AndroidDeps.NAV_UI_KTX
    implementation AndroidDeps.MATERIAL_DESIGN

    implementation CustomViewDeps.MP_ANDROID_CHART
}


来源:https://stackoverflow.com/questions/53475508/how-to-manage-common-dependencies-for-multiple-android-library-projects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!