如何管理多个Android库项目的公共依赖项? [英] How to manage common dependencies for multiple Android library projects?

查看:462
本文介绍了如何管理多个Android库项目的公共依赖项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题:如何将依赖项添加到项目的顶级build.gradle文件?

但它没有给出我需要的答案。

But it doesn't give the answer I need.

假设我的android项目有3个库模块 A B C
所有三个使用共同的依赖关系说 D1 其中 D1 是像这样的库Dagger 房间等。

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.

现在而不是添加 D1 这三个,我正在考虑将 D1 添加到项目级别 build.gradle

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:

无法在类型为org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler的对象上找到参数[directory'libs']的方法实现()。

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
}

在项目级别添加依赖项的正确方法是什么 build.gradle

What is the correct way of adding dependencies in project level build.gradle ?

这是项目链接: https://github.com/cerberusv2px/android-modular

解决方案

创建一个名为的新模块项目,例如: common-libs ,并将所有常见的依赖项放入此项目中,打开 common-libs / build.gradle

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.

更改

implementation fileTree(dir: 'libs', include: ['*.jar']) 

api fileTree(dir: 'libs', include: ['*.jar'])

然后对于所有上游依赖项目,只需调用

And then for all the upstream depending projects, just call

dependencies {
    ...
    api project(':common-libs')
}

请参阅: https://developer.android.com/studio/build/dependencies #dedency_configurations

这篇关于如何管理多个Android库项目的公共依赖项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆