5. gradle配置模板

2.png
主要檔案如上圖:
//如果沒看懂,可以先看下面幾篇:
2. build.gradle 使用each迴圈引入第三方框架
3. build.gradle中buildConfigField使用
4. build.gradle中buildConfigField使用注意:以下第1、2、 3點是配置資訊,第4是尋找配置的檔案,第5是最終引用
1. app目錄下的sign.gradle(test.jks 簽名信息)
ext { //簽名信息 STORE_FILE = "test.jks" STORE_PASSWORD = "123456" KEY_ALIAS = "123456" KEY_PASSWORD = "123456" }
2.專案根目錄下config包下app.gradle (App主要配置資訊)
ext { //sdk版本管理 COMPILE_SDK_VERSION = 26// 用於編譯的SDK版本 BUILD_TOOLS_VERSION = "28.0.3" // 用於Gradle編譯專案的工具版本 APPLICATION_ID = "com.liys.gradleconfig" //包名 MIN_SDK_VERSION = 19// 最低支援Android版本 TARGET_SDK_VERSION = 26// 目標版本 VERSION_CODE = 1//版本號 VERSION_NAME = "1.0" MINIFY_ENABLED = false //是否混淆 //日誌管理 LOG_DEBUG_TYPE = "boolean" //是否輸出日誌 變數型別 LOG_DEBUG_NANE = "LOG_DEBUG" //是否輸出日誌 變數名 LOG_DEBUG_DEBUG = "true"//是否輸出日誌(debug版) LOG_DEBUG_RELEASE = "false" //是否輸出日誌(正式版) //baseUrl地址 BASE_URL_TYPE = "String" //返回值 BASE_URL_NAME = "BASE_URL" //變數名 BASE_URL_RELEASE = "\"http:\"" //線上地址 BASE_URL_DEBUG = "\"http:\"" // 本地地址 // App dependencies appcompatVersion = "26.1.0" constraintVersion = "1.1.3" junitVersion = "4.12" runnerVersion = "1.0.2" espressoVersion = "3.0.2" //公共的(系統的) roots = [ //專案基本的(每個專案需要的) "appcompatV7":"com.android.support:appcompat-v7:${appcompatVersion}", "constraint":"com.android.support.constraint:constraint-layout:${constraintVersion}", "junit":"junit:junit:${junitVersion}", "runner":"com.android.support.test:runner:${runnerVersion}", "espresso":"com.android.support.test.espresso:espresso-core:${espressoVersion}", ] //第三方框架 thirdFrame = [ "gson":"com.google.code.gson:gson:2.8.0", //gson解析 "glide":"com.github.bumptech.glide:glide:3.7.0", //glide ] //App需要匯入的包 app = [ thirdFrame.gson, thirdFrame.glide, ] }
3.專案根目錄下config包下source.gradle (資源分包目錄)
ext{ //res資原始檔 resSource = [ 'src/main/res', 'src/main/res-main/home', ] }
4.專案根目錄下build.gradle,尾部加上:
apply from: 'config/app.gradle' apply from: 'config/source.gradle'
5.app目錄下build.gradle
apply plugin: 'com.android.application' apply from: 'sign.gradle' android { //SDK和App版本 compileSdkVersion COMPILE_SDK_VERSION defaultConfig { applicationId APPLICATION_ID minSdkVersion MIN_SDK_VERSION targetSdkVersion TARGET_SDK_VERSION versionCode VERSION_CODE versionName VERSION_NAME testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } //簽名 signingConfigs{ release { storeFile file(STORE_FILE) storePassword STORE_PASSWORD keyAlias KEY_ALIAS keyPassword KEY_PASSWORD } debug { release } } //混淆和日誌 buildTypes { release { minifyEnabled MINIFY_ENABLED proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' buildConfigField LOG_DEBUG_TYPE, LOG_DEBUG_NANE, LOG_DEBUG_RELEASE //是否輸出LOG資訊 buildConfigField BASE_URL_TYPE, BASE_URL_NAME, BASE_URL_RELEASE // baseUrl signingConfigs.release } debug{ buildConfigField LOG_DEBUG_TYPE, LOG_DEBUG_NANE, LOG_DEBUG_DEBUG //是否輸出LOG資訊 buildConfigField BASE_URL_TYPE, BASE_URL_NAME, BASE_URL_DEBUG //baseUrl } } //資源分包 sourceSets{ main { res.srcDirs = [ resSource.each{it} ] } } } dependencies { //系統的 implementation fileTree(include: ['*.jar'], dir: 'libs') implementation roots.appcompatV7 implementation roots.constraint testImplementation roots.junit androidTestImplementation roots.runner androidTestImplementation roots.espresso //匯入第三方框架 app.each { implementation it } //依賴庫 implementation project(':testlibrary') }