gradle语法管理android studio项目配置信息

gradle 初级语法试用

有时候为了让android studio工具能方便的管理工程.我们一般采用gradle语法来操作可能会更加的方便.(更好的管理发布版本)
step1 我们在根目录下面创建一个config.gradle文件(和根目录的 build.gradle同级别)
ext {
   
    androidId = [
            compileSdkVersion: 28,
            buildToolsVersion: "29.0.2",
            minSdkVersion    : 15,
            targetSdkVersion : 28,
            versionCode      : 1,
            versionName      : "1.0"
    ]
    //这里我们可以存放app或者module的包名 统一管理
 appId = [
            "applicationId": "com.asa.xnotelite"
    ]

    dependencies = [
            "appcompat"                    : 'com.android.support:appcompat-v7:28.0.0',
            "constraint"                   : 'com.android.support.constraint:constraint-layout:1.1.3',
            "BaseRecyclerViewAdapterHelper": 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30',
            "recyclerview"                 : 'com.android.support:recyclerview-v7:28.0.0',
            "design"                       : 'com.android.support:design:28.0.0',
            "greendao"                     : 'org.greenrobot:greendao:3.2.2',
            "butterknife": "com.jakewharton:butterknife:9.0.0",
            "butterknifecompiler" :"com.jakewharton:butterknife-compiler:9.0.0"
    ]
step2 在build.gradle文件的开始位置添加 apply from: “config.gradle”,然后我们就可以在工程的任何module中都可以使用config.gradle文件中的配置信息了
//下面是引入config.gradle文件中的信息   相当于导包
def appId = rootProject.ext.appId
def androidId = rootProject.ext.androidId
def support = rootProject.ext.dependencies

android {
    compileSdkVersion androidId.compileSdkVersion
    buildToolsVersion androidId.buildToolsVersion
    defaultConfig {
        applicationId appId.applicationId
        minSdkVersion androidId.minSdkVersion
        targetSdkVersion androidId.targetSdkVersion
        versionCode androidId.versionCode
        versionName androidId.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    ....
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    annotationProcessor support.butterknifecompiler   //常规引用
    support.each { k, v -> implementation v } //简写通过gradle循环语法

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章