【问题标题】:How I can separate version of libraries in "build.gradle" app如何在“build.gradle”应用程序中分离库版本
【发布时间】:2021-10-24 08:08:59
【问题描述】:

我正在尝试将库的版本分开,以便将它们全部放在一个位置,以节省时间和复杂性。

我在某个博客的某条评论中看到一个人,说他是这样做的。他发布了下一个屏幕。

我不能用这种方式来构造gradle,但我认为这是一个好方法。

我的项目 build.gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

// Definition of versions of libraries
ext {

    toolVersions = [

            android :[
                    versionCode    : 1,
                    versionName    : "0.0.1",
                    minSdk          : 16,
                    targetSdk       : 26,
                    compileSdk      : 26,
                    buildTools      : "26.0.2",
                    support         : "26.1.0"
            ],
            espressoCore   : "2.2.2",
            junit           : "4.12"

    ]

    libVersions = [
            glide   :   "4.2.0",
            flubber :   "1.0.1"
    ]

}

我的应用 build.gradle 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion toolVersions.android.compileSdk
    buildToolsVersion toolVersions.android.buildTools
    defaultConfig {
        applicationId "com.maol.brastlewark"
        minSdkVersion toolVersions.android.minSdk
        targetSdkVersion toolVersions.android.targetSdk
        versionCode toolVersions.android.versionCode
        versionName toolVersions.android.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:' + toolVersion.espressoCore, {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

    // SUPPORT LIBRARIES
    compile 'com.android.support:appcompat-v7:' toolVersion.support
    compile "com.android.support:support-core-utils:$rootProject.toolVersion.support"
    testCompile "junit:junit:$rootProject.toolVersion.junit"

    // IMAGE LOADER LIBRARY
    compile "com.github.bumptech.glide:glide:$rootProject.libVersions.glide"
    annotationProcessor "com.github.bumptech.glide:compiler:$rootProject.libVersions.glide"

    // VIEW ANIMATIONS
    compile "com.appolica:flubber:$rootProject.libVersions.flubber"

}

我不知道如何在 build.gradle (app) 中使用它。房间里的任何人都可以给我一些建议吗?

谢谢

【问题讨论】:

    标签: android gradle android-gradle-plugin build.gradle


    【解决方案1】:

    您可以创建一个文件(例如gradleScript/dependencies.gradle):

    ext {
        //Version
        supportLibrary = '26.1.0'
    
        //Support Libraries dependencies
        supportDependencies = [
                design           :         "com.android.support:design:${supportLibrary}",
                recyclerView     :         "com.android.support:recyclerview-v7:${supportLibrary}",
                cardView         :         "com.android.support:cardview-v7:${supportLibrary}",
                appCompat        :         "com.android.support:appcompat-v7:${supportLibrary}",
                supportAnnotation:         "com.android.support:support-annotations:${supportLibrary}",
        ]
    }
    

    在顶层文件build.gradle添加:

    // Load dependencies
    apply from: 'gradleScript/dependencies.gradle'
    

    module1/build.gradle

    // Module build file
    
    dependencies {
        //......
        compile supportDependencies.appCompat
        compile supportDependencies.design
    }
    

    【讨论】:

    • 如何直接实现整个supportDependencies组?
    • 我可能会迟到,但这可能会帮助初学者:申请整个supportDependencies 组做supportDependencies.values()
    • 这种方法不会像miro.medium.com/max/3708/1*hgOycRk5Rds-gOS80Hz6IA.png 那样显示警告
    • 这并不能说明更新是否可用
    【解决方案2】:

    保留 lint 更新检查的解决方案

    您可以使用与您的问题相同的结构。要保留 lint 更新检查,您只需包含版本号(而不是整个依赖项名称)。

    在您的项目级别 build.gradle 文件中创建一个 ext

    ext 块中的每个项目都像一个映射,因此我们可以在一个数组中组织依赖版本。

    ext {
        playServicesVersions = [
                base    : '17.6.0',
                location: '18.0.0',
                maps    : '17.0.0'
        ]
    
        supportVersions = [
                nameHere: '3.0.0'
        ]
    }
    

    访问依赖版本

    请注意,这里使用大括号是因为访问了多个变量。

    dependencies {
        implementation "com.google.android.gms:play-services-base:${playServicesVersions.base}"
        implementation "com.google.android.gms:play-services-location:${playServicesVersions.location}"
        implementation "com.google.android.gms:play-services-maps:${playServicesVersions.maps}"
    }
    

    Lint 检查仍然有效

    【讨论】:

      【解决方案3】:

      为此,您可以在 build.gradle 文件中声明 ext{} 块。

      ext {
          def AAVersion = '4.0-SNAPSHOT' // change this to your desired version
      }
      
      dependencies {
          apt "org.androidannotations:androidannotations:$AAVersion"
          compile "org.androidannotations:androidannotations-api:$AAVersion"
      }
      

      如果你想使用数组:

      ext {
          supportDependencies = [
              design : "com.android.support:design:${supportLibrary}",
              // whatever lib...
          ]
      }
      

      那么当你想调用它时:

      dependencies {
          compile supportDependencies.design
      }
      

      【讨论】:

      • 我项目build.gradle中的ext定义我觉得没问题。问题是我不知道如何在 app build.gradle 中调用这个变量
      • 只需按照我的回答,使用“not”并在调用版本之前加上 $。这样:“groupId:artifactId:$versionDef”
      • 当我尝试将 def 作为数组的前缀时,IDE lint 给了我一个错误。例如,你能给我写一个数组 toolVersion->android 的例子吗?谢谢
      【解决方案4】:

      您可以在 settings.gradle 中声明依赖项:

      enableFeaturePreview('VERSION_CATALOGS')
      dependencyResolutionManagement {
          versionCatalogs {
              libs {
                  alias('protobuf').to('com.google.protobuf:protobuf-java:3.15.8')
                  alias('snappy').to('org.xerial.snappy:snappy-java:1.1.8.4')
                  alias('junit').to('junit:junit:4.13.2')
              }
          }
      }
      

      然后在build.gradle中使用它们:

      dependencies {
          api libs.protobuf
          api libs.snappy
      
          testImplementation libs.junit
      }
      

      它也适用于多项目构建。

      详情请参考official docs

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 2017-08-03
        • 1970-01-01
        • 1970-01-01
        • 2018-03-24
        相关资源
        最近更新 更多