如何在“build.gradle”应用

时间:2017-10-25 11:13:20

标签: android gradle android-gradle build.gradle

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

我在一些博客中看到一个人发表评论,这就是他用来做这件事的方式。他发布了下一个屏幕。

First Screen

Second Screen

我不能用这种方式构建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)中使用它。房间里的任何人都可以给我一些建议吗?

谢谢

2 个答案:

答案 0 :(得分:6)

您可以创建文件(例如gradleScript/dependecies.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
}

答案 1 :(得分:1)

为了实现这一点,您可以在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
}