Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

First Screen

Second Screen

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

like image 259
Víctor Martín Avatar asked Oct 25 '17 11:10

Víctor Martín


2 Answers

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
}
like image 115
Gabriele Mariotti Avatar answered Nov 14 '22 18:11

Gabriele Mariotti


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

Lint check still active

like image 4
Markymark Avatar answered Nov 14 '22 17:11

Markymark