安卓工程統一管理gradle變量

我們複製一個gradle文件 然後重新命名一個與項目相關的名字


打開imooc.gradle文件,重新定義一下里面的內容

ext {
    android = [
            applicationId:'com.tencent.musicproject',
            compileSdkVersion: 28,
            minSdkVersion    : 19,
            targetSdkVersion : 28,
            versionCode      : 1,
            versionName      : '1.0'

    ]

    //三方版本號
    depsVersion = [
            appcompat: '1.4.0'
    ]
    //三方地址
    depsLibs = [

           appcompat:"androidx.appcompat:appcompat:${depsVersion.appcompat}"

    ]

}

我們把項目中的所有版本號信息 依賴項 都編輯到裏面 創建數組名稱自定義 來管理對應的版本號和依賴
這裏面depsLibs 裏面要用雙引號,因爲中間使用了$符號,單引號失去效果

編輯好imooc.gradle文件後 需要在工程的總build.gradle文件中引入該文件

apply from : file('imooc.gradle')
//apply from: this.rootProject.file('imooc.gradle')

引入完畢重新構建一下項目 就可以在項目中進行替換了

apply plugin: 'com.android.application'

android {
    compileSdkVersion this.rootProject.android.compileSdkVersion
    buildToolsVersion "30.0.0"

    defaultConfig {
        applicationId this.rootProject.android.applicationId
        minSdkVersion this.rootProject.android.minSdkVersion
        targetSdkVersion this.rootProject.android.targetSdkVersion
        versionCode this.rootProject.android.versionCode
        versionName this.rootProject.android.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    configurations.all {
        resolutionStrategy {
            force 'androidx.core:core-ktx:1.6.0'
        }
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation this.rootProject.depsLibs.appcompat
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

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