【问题标题】:How do I share dependencies between Android modules如何在 Android 模块之间共享依赖项
【发布时间】:2015-10-28 17:53:49
【问题描述】:

我有一个 Android 应用程序模块 (app) 和一个 Android 库模块 (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'
}

但是,当我尝试将该块添加到 project build.gradle 时,它​​抱怨不知道“编译”DSL。

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

【问题讨论】:

标签: android android-studio gradle


【解决方案1】:

从 Gradle 插件版本 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'
}

应用模块 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'
}

请参阅this guide 了解更多信息和图表。

【讨论】:

  • 无法正常工作。完全一样,我也在图书馆中使用改造,但仍然。当我将我的库模块导入为 .aar 时,编译成功后,它在设备上崩溃并出现 classnotfound 异常。有什么建议吗?
  • 嗨@Jules ...你能回答我的问题stackoverflow.com/questions/51694352/…
  • kapt 怎么样?
【解决方案2】:

依赖块(闭包)需要 DependencyHandler 作为委托

您需要将每个项目的 DependencyHandler 传递给 project 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'
}

app build.gradle

dependencies {
    sharedGroup dependencies
}

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

(参见 2. 使用默认依赖组。 此示例解释了共享库版本、sdk 版本的许多其他技巧……对于具有许多模块的大型项目。)

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      您可以这样做,其中 project build.gradle 将指定所需的依赖项作为变量名,然后在 app 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
      }
      

      app 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
      
      }
      

      希望这会有所帮助!

      【讨论】:

      • 这是在 Android 项目的所有模块中使用依赖项的更好方法
      • 你如何处理传递选项?
      【解决方案5】:

      基于@SMKS 的回答,我更喜欢这个解决方案,因为它具有传递选项功能和简单性

      项目 build.gradle

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

      app build.gradle 文件

      dependencies {
          compile 'com.android.support:support-v4:' + supportLibVersion
          compile ' com.android.support:support-annotations:' + supportLibVersion
          compile = 'com.android.support:recyclerview-v7:' + supportLibVersion {
              transitive = true // do not know if this make sens/interest just for example
          }
         ...
      }
      

      【讨论】:

        【解决方案6】:

        在根项目模块中使用 ext 块共享库

        这是在所有模块中使用库的简单方法 安卓项目

        请按以下步骤操作:

        1. 在根项目 gradle 文件中添加 ext 块(用于为项目定义额外的属性)
        2. 在 ext 块中使用变量名添加公共库 例如名称 = [ 没有实现关键字的库 ]
        3. 在模块级别使用了这个 ext 块,使用实现和变量名 例如实现变量名

        完整实现见下面代码

        build.gradle :project

        buildscript {
        ... (the rest of your repositories here) ...
        }
        
        ext { **// ext block start here**
        
        
        
        
        appModuleLibraries = [
                    commonLibraries,
                    /*Projects*/
                    project(':hco-cutout'),
                    project(':utils')
        
            ]
        
        commonLibraries = [
                /*Android Libs*/
                'androidx.core:core-ktx:1.7.0',
                'androidx.appcompat:appcompat:1.4.1',
                'com.google.android.material:material:1.5.0',
                'androidx.constraintlayout:constraintlayout:2.1.3',
                /*Gesture viw for image zooming */
                'com.alexvasilkov:gesture-views:2.5.2',
        
        
        ]
        cutoutModulleLibraries = [
                commonLibraries,
                project(':utils'),
                // Selfie segmentation
                'com.google.mlkit:segmentation-selfie:16.0.0-beta4',
        
                /*checker board drawable*/
                'com.github.duanhong169:checkerboarddrawable:1.0.2',
        ]
        } **// ext block end here**
        

        build.gradle :app

              dependencies {
            
              /*App Module Libraries in root project gradle*/
        
                implementation appModuleLibraries
            }
        

        build.gradle :cutout

         dependencies {
            /*cutout Module Libraries in root project gradle*/
        
            implementation cutoutModulleLibraries
        
            implementation project(':utils')
        
        }
        

        希望这会有所帮助!

        【讨论】:

          猜你喜欢
          • 2019-02-15
          • 2015-03-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多