Gradle文件配置(版本号与依赖)

最近在导入一些开源项目的时候,发现配置文件的写法和自己平时写的不一样,平时是这样写的。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.example.hecun.myapplication"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
   ........
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.nineoldandroids:library:2.4.0'
}

其实还有另外一种写法,有些项目中会这样写。

android {

    compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion

    defaultConfig {
        applicationId rootProject.ext.android.applicationId
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName
    }

........
}
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(":libraries:headsupcompat")
    compile project(":libraries:smooth-app-bar-layout")
    compile rootProject.ext.dependencies["design"]
    compile rootProject.ext.dependencies["appcompat-v7"]
    compile rootProject.ext.dependencies["recyclerview-v7"]
    compile rootProject.ext.dependencies["picasso"]
    compile rootProject.ext.dependencies["numberprogressbar"]
    compile rootProject.ext.dependencies["photoview"]
    compile rootProject.ext.dependencies["umeng-analytics"]
    compile rootProject.ext.dependencies["retrofit"]
    compile rootProject.ext.dependencies["rxandroid"]
    compile rootProject.ext.dependencies["okhttp-urlconnection"]
    compile rootProject.ext.dependencies["okhttp"]
    compile rootProject.ext.dependencies["butterknife"]
    compile rootProject.ext.dependencies["otto"]
    compile rootProject.ext.dependencies["glide"]
}

可以看到和我们写的不一样,应该是引用了某个地方的东西,我们去看看
Gradle文件配置(版本号与依赖)_第1张图片

我们打开这个文件


ext {

    android = [compileSdkVersion: 22,
               buildToolsVersion: "22.0.1",
               applicationId    : "me.drakeet.meizhi",
               minSdkVersion    : 14,
               targetSdkVersion : 22,
               versionCode      : 359,
               versionName      : "2.5.9"]

    dependencies = ["appcompat-v7"        : "com.android.support:appcompat-v7:22.2.1",
                    "design"              : "com.android.support:design:22.2.1",
                    "recyclerview-v7"     : "com.android.support:recyclerview-v7:22.2.1",
                    "nineoldandroids"     : "com.nineoldandroids:library:2.4.0",
                    "picasso"             : "com.squareup.picasso:picasso:2.5.2",
                    "photoview"           : "com.github.chrisbanes.photoview:library:1.2.3",
                    "numberprogressbar"   : "com.daimajia.numberprogressbar:library:1.2@aar",
                    "umeng-analytics"     : "com.umeng.analytics:analytics:latest.integration",
                    "retrofit"            : "com.squareup.retrofit:retrofit:1.9.0",
                    "rxandroid"           : "io.reactivex:rxandroid:1.0.0",
                    "okhttp-urlconnection": "com.squareup.okhttp:okhttp-urlconnection:2.0.0",
                    "okhttp"              : "com.squareup.okhttp:okhttp:2.0.0",
                    "butterknife"         : "com.jakewharton:butterknife:7.0.1",
                    "otto"                : "com.squareup:otto:1.3.8",
                    "glide"               : "com.github.bumptech.glide:glide:3.6.1"]
}

注意,这时候需要在跟目录build.gradle中添加下面一句话

apply from: "config.gradle"

你可能感兴趣的:(Gradle文件配置(版本号与依赖))