美文网首页
关于AS中的config.gradle文件

关于AS中的config.gradle文件

作者: 墨染草 | 来源:发表于2019-03-14 17:33 被阅读0次

    AS根目录下的 config.gradle 文件是用来 全局定义项目的配置信息(含compileSdkVersion、targetSdkVersion、versionCode、versionName、项目用到的公共依赖库)
    写法示例:

    ext {
        versionReleaseName = "1.0.00"
        android = [
                compileSdkVersion: 26,
                minSdkVersion    : 19,
                targetSdkVersion : 22,
                versionCode      : 13,
                versionName      : versionReleaseName
        ]
        appId = [
                "app": "com.newland.sft",
        ]
        supportV7Library = "26.1.0"
        dependencies = [
                "appcompat-v7"     : "com.android.support:appcompat-v7:${supportV7Library}",
                "support-v4"       : "com.android.support:support-v4:${supportV7Library}",
                "recyclerview-v7"  : "com.android.support:recyclerview-v7:${supportV7Library}",
                "design"           : "com.android.support:design:${supportV7Library}",
                "cardview"         : "com.android.support:cardview-v7:${supportV7Library}",
                "junit"            : "junit:junit:4.12",
                "runner"           : "com.android.support.test:runner:1.0.2",
                "espresso-core"    : "com.android.support.test.espresso:espresso-core:3.0.2",
                "constraint-layout": "com.android.support.constraint:constraint-layout:1.1.2",
                "logger": "com.orhanobut:logger:2.1.1",
                "gson": "com.google.code.gson:gson:2.8.2"
        ]
    }
    

    写好config.gradle文件后,需要到项目的根build.gradle文件里加上一句应用代码:

    apply from: "config.gradle"
    当有的模块需要用到公共配置文件config.gradle里面定义的信息,如三方依赖库,可以这样进行引用 ext闭包

    apply plugin: 'com.android.application'
    
    def cfg = rootProject.ext.android    // 引用ext闭包
    def appid = rootProject.ext.appId  // 引用ext中定义的appId字段
    def dependLib = rootProject.ext.dependencies // 引用ext中定义的dependencies字段
    
    android {
        compileSdkVersion cfg.compileSdkVersion
        defaultConfig {
            applicationId appid['app']
            minSdkVersion cfg.minSdkVersion
            targetSdkVersion cfg.targetSdkVersion
            versionCode cfg.versionCode
            versionName cfg.versionName
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        // 引用依赖库
        implementation dependLib['appcompat-v7']
        implementation dependLib['design']
        implementation dependLib['constraint-layout']
        testImplementation dependLib['junit']
        androidTestImplementation dependLib['runner']
        androidTestImplementation dependLib['espresso-core']
        implementation dependLib['logger']
        implementation project(':picker')
        implementation project(':command')
        implementation project(':b_bank_card')
        implementation project(':b_pos_tong')
        implementation project(':b_public')
    }
    
        //定义签名
        signingConfigs {
            newland {
                keyAlias 'androiddebugkey'
                keyPassword 'android'
                storeFile file('../xxx.keystore')
                storePassword 'android'
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                //使用签名newland
                signingConfig signingConfigs.newland
            }
            debug {
                signingConfig signingConfigs.newland
            }
        }
        compileOptions {
            sourceCompatibility 1.8
            targetCompatibility 1.8
        }
    }
    

    相关文章

      网友评论

          本文标题:关于AS中的config.gradle文件

          本文链接:https://www.haomeiwen.com/subject/hyycmqtx.html