如何在Android模块之间共享依赖关系

And*_*rew 24 android gradle android-studio

我有一个Android应用程序模块(app)和一个Android库模块(库).app和library都包含这些相同的依赖项:

dependencies {
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'io.reactivex:rxjava:1.0.13'
    compile 'io.reactivex:rxandroid:0.25.0'
}
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试将该块添加到项目 build.gradle时,它抱怨不知道"编译"DSL.

编辑:我问的是将这个依赖项块放在PROJECT build.gradle中,以避免在每个模块的build.gradle中重复.

Jul*_*les 61

从Gradle Plugin 3.0.0开始,有一种更好的方法可以做到这一点.我们可以控制每个依赖项是仅可用于当前模块,还是当前模块和依赖它的任何模块.这将使我们能够轻松地共享项目中模块之间的依赖关系.

以下是我们用于声明依赖项的方法:

  • 编译 'example.dependency:1.0.0'

以下是应该替换compile的新配置:

  • implementation'example.dependency:1.0.0' - >此依赖项仅在此模块中使用
  • API "example.dependency:1.0.0" - >这种依赖性也将在任何可用的生成依赖于此模块

以下是如何使用您在问题中提到的架构来实现这一点.假设我们有一个名为'library'的模块被'app'模块使用,我们可以使用api配置来声明依赖项应该与依赖它的任何模块共享.

库模块build.gradle

dependencies {

    // dependencies marked 'implementation' will only be available to the current module
    implementation 'com.squareup.okhttp:okhttp:2.4.0'

    // any dependencies marked 'api' will also be available to app module
    api 'com.squareup.retrofit:retrofit:1.9.0'
    api 'io.reactivex:rxjava:1.0.13'
    api 'io.reactivex:rxandroid:0.25.0'
}
Run Code Online (Sandbox Code Playgroud)

app module build.gradle:

dependencies {

    // declare dependency on library module
    implementation project(':library')

    // only need to declare dependencies unique to app 
    implementation 'example.dependency:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息和图表,请参阅本指南.


Unk*_*ack 7

依赖项块(closure)需要DependencyHandler作为委托

您需要将每个项目的DependencyHandler传递给项目 gradle.build中的共享依赖项。

项目 build.gradle

ext.sharedGroup = {dependencyHandler->
    delegate = dependencyHandler

    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.retrofit:retrofit:1.9.0'
    compile 'io.reactivex:rxjava:1.0.13'
    compile 'io.reactivex:rxandroid:0.25.0'
}
Run Code Online (Sandbox Code Playgroud)

应用程序 build.gradle

dependencies {
    sharedGroup dependencies
}
Run Code Online (Sandbox Code Playgroud)

参考 https://github.com/b1uec0in/DependencyVersionResolver

(请参阅2.使用默认依赖项组。此示例说明了许多其他的技巧,这些技巧用于共享库版本,sdk版本...,用于具有许多模块的大型项目。)


fra*_*nch 6

您可以在库模块中定义共享的 gradle 依赖项,如果 app 模块将库作为依赖项,则无需指定两次。更进一步,您可以创建一个需要共享 gradle 依赖项的“通用”模块,并使 app 和库模块都需要通用模块。


SMK*_*MKS 6

您可以这样做,其中项目build.gradle 将指定所需的依赖项作为变量名称,然后在应用程序build.gradle 文件中您只需要包含变量名称。当您有很多模块并且不想在版本号更改时编辑每个人时,这非常有用!

项目build.gradle

buildscript {
    ext {
        googlePlayServicesVersion = '7.5.0'
        supportLibVersion = '22.2.0'
    }
... (the rest of your repositories/dependency info here) ...
}

ext {
    minSdkVersion=16
    targetSdkVersion=21
    buildToolsVersion='22.0.1'
    compileSdkVersion=21

    //Android Dependencies
    supportV4 = 'com.android.support:support-v4:' + supportLibVersion
    supportAnnotations = 'com.android.support:support-annotations:' + supportLibVersion
    recyclerView = 'com.android.support:recyclerview-v7:' + supportLibVersion
    cardView = 'com.android.support:cardview-v7:' + supportLibVersion
    palette = 'com.android.support:palette-v7:' + supportLibVersion
    appCompat = 'com.android.support:appcompat-v7:' + supportLibVersion
    multidex = 'com.android.support:multidex:1.0.1'
    appCompat = 'com.android.support:appcompat-v7:' + supportLibVersion
    supportDesign = 'com.android.support:design:' + supportLibVersion
    playServicesAnalytics = 'com.google.android.gms:play-services-analytics:' + googlePlayServicesVersion
}
Run Code Online (Sandbox Code Playgroud)

应用程序build.gradle 文件

dependencies {
   compile rootProject.ext.supportV4
    compile rootProject.ext.appCompat
    compile rootProject.ext.supportAnnotations
    compile rootProject.ext.recyclerView
    compile rootProject.ext.cardView
    compile rootProject.ext.palette
    compile rootProject.ext.appCompat
    compile rootProject.ext.multidex
    compile rootProject.ext.supportDesign
    compile rootProject.ext.playServicesAnalytics

}
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助!