简体   繁体   中英

How I can separate version of libraries in "build.gradle" app

I'm trying to separate versions of the libraries to have all of them in one location in order to save time and complexity.

I saw a guy in some comment in some blog that sais the way he use to do this. He posted the next screens.

第一个屏幕

第二屏

I can't use this way to construct the gradle, but I think that is a good way.

My Project build.gradle file:

// 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"
    ]

}

My app build.gradle file:

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"

}

I don't know how to used this in the build.gradle (app). Anyone in the room can advised me something?

Thank you

You can create a file (for example 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}",
    ]
}

In the top level file build.gradle add:

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

In the module1/build.gradle :

// Module build file

dependencies {
    //......
    compile supportDependencies.appCompat
    compile supportDependencies.design
}

Solution that preserves lint update checks

You can use the same construct as in your question. To preserve the lint update checks you'd only include the version number (rather than the entire dependency name).

Create an ext block in your project level build.gradle file

Each item in the ext block acts like a map so we can organize dependency versions in an array.

ext {
    playServicesVersions = [
            base    : '17.6.0',
            location: '18.0.0',
            maps    : '17.0.0'
    ]

    supportVersions = [
            nameHere: '3.0.0'
    ]
}

Access dependency versions

Note that curly braces are used here because more than one variable is accessed.

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 checks still active

皮棉检查仍处于活动状态

To make that possible you can declare ext{} block in your build.gradle file.

ext {
    def AAVersion = '4.0-SNAPSHOT' // change this to your desired version
}

dependencies {
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

If you want to work with arrays:

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

then when you want to call it:

dependencies {
    compile supportDependencies.design
}

You can declare the dependencies in your settings.gradle :

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')
        }
    }
}

And then use them in build.gradle :

dependencies {
    api libs.protobuf
    api libs.snappy

    testImplementation libs.junit
}

It works for multi-project builds as well.

Please refer to official docs for details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM