淺析Android中build.gradle的實(shí)用技巧
1.替換符的使用
(1)在 app-android-defaultConfig (或者多渠道打包)下面可以這樣使用
android { defaultConfig { manifestPlaceholders = [ //高德地圖key GDKEY: "123456789", ] } }
(2)在 AndroidManifest.xml 文件的 application 標(biāo)簽下面這樣引用
<!-- 高德地圖 --> <meta-data android:name="com.amap.api.v2.apikey" android:value="${GDKEY}" />
2.打包設(shè)置appname(啟動(dòng)圖標(biāo)類似,res下面的都可以這樣使用)
android { defaultConfig { //在string.xml中不能出現(xiàn)app_name這個(gè)字段,否則生成報(bào)錯(cuò) resValue "string", "app_name", "app名稱" } }
3.生成BuildConfig.java字段
在build.gradle中
android { defaultConfig { //生成一個(gè)boolea類型的變量 buildConfigField "boolean", "IS_TEST_URL", "true" //生成一個(gè)字符串變量 buildConfigField "String", "APP_UPDATE_TIME", "\"${System.currentTimeMillis().toString()}\"" } }
在java代碼
public final class BuildConfig { // Fields from product flavor: 渠道名 public static final String APP_UPDATE_TIME = "1551754973086"; public static final boolean IS_TEST_URL = false; }
4.多渠道打包(注意在defaultConfig下面添加flavorDimensions "versionCode")
android { compileSdkVersion 28 defaultConfig { minSdkVersion 19 targetSdkVersion 28 testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" flavorDimensions "versionCode" productFlavors { name1 { applicationId "com.test.name" versionName "0.1.4" versionCode 5 resValue "string", "app_name", "app名字" buildConfigField "boolean", "IS_TEST_URL", "false" buildConfigField "String", "APP_UPDATE_TIME", "\"${System.currentTimeMillis().toString()}\"" } }
5.設(shè)置簽名
android{ signingConfigs { release { keyAlias '' keyPassword '' storeFile file('./sign.jks') storePassword '' v2SigningEnabled false } } buildTypes { release { debuggable false minifyEnabled true shrinkResources true useProguard true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } debug { debuggable true minifyEnabled false shrinkResources false useProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' signingConfig signingConfigs.release } } }
6.更改打包的apk名
android{ android.applicationVariants.all { variant -> variant.outputs.all { Date nowTime = new Date() SimpleDateFormat time = new SimpleDateFormat("MM月dd日HH時(shí)mm分") outputFileName = "${variant.flavorName}(${variant.versionCode}_${variant.versionName})(${time.format(nowTime)}).apk" } } }
7.引入第三方庫的時(shí)候,剔除某些不需要的包或者重復(fù)的包
1.直接在configuration中排除
configurations { compile.exclude module: 'commons' all*.exclude group: 'org.gradle.test.excludes', module: 'reports' }
2.在具體的某個(gè)dependency中排除
dependencies { implementation("com.github.bumptech.glide:glide:4.8.0") { exclude module: 'appcompat-v7' } }
PS:Android Studio開發(fā)Build.gradle小技巧
引用版本統(tǒng)一規(guī)范
Android開發(fā)存在著眾多版本的不同,比如compileSdkVersion、minSdkVersion、targetSdkVersion以及項(xiàng)目中依賴第三方庫的版本,不同的module及不同的開發(fā)人員都有不同的版本,所以需要一個(gè)統(tǒng)一版本規(guī)范的文件,現(xiàn)在我就來介紹一種方式。
在項(xiàng)目根目錄,也就是跟app同一目錄下的build.gradle文件,如下圖所示
在其最后添加如下groovy代碼。
ext { compileSdkVersion = 25 buildToolsVersion = "25.0.0" minSdkVersion = 19 targetSdkVersion = 19 supportVersion = '25.3.1' rxjavaVersion = '1.1.8' rxandroidVersion = '1.2.1' glideVersion = '3.6.1' junitVersion = '4.12' deps = [ appcompatv7 : "com.android.support:appcompat-v7:$supportVersion", supportv4 : "com.android.support:support-v4:$supportVersion", recyclerviewv7: "com.android.support:recyclerview-v7:$supportVersion", rxjava : "io.reactivex:rxjava:$rxjavaVersion", rxandroid : "io.reactivex:rxandroid:$rxandroidVersion", glide : "com.github.bumptech.glide:glide:$glideVersion", junit : "junit:junit:$junitVersion" ] }
有了這個(gè)規(guī)范,那么我們在app下的build.gradle文件就可以這樣來引用了
android { compileSdkVersion rootProject.compileSdkVersion buildToolsVersion rootProject.buildToolsVersion defaultConfig { applicationId "com.ecarx.thememanager" minSdkVersion rootProject.minSdkVersion targetSdkVersion rootProject.targetSdkVersion versionCode 1 versionName "1.0" } ... } dependencies { compile fileTree(include: ['*.jar'], dir: 'libs') compile deps.supportv4 compile deps.appcompatv7 compile deps.recyclerviewv7 compile deps.rxjava compile deps.rxandroid compile deps.glide testCompile deps.junit }
是不是一勞永逸了,今后修改版本只需要修根目錄下的build.gradle文件即可把所有依賴版本都修改
對資源進(jìn)行分包
我們可以對每個(gè)頁面的資源都進(jìn)行具體分類,不只是layout,還有drawable及value,是不是心動(dòng)了,趕緊照著如下配置試一試吧,別再讓資源文件們“混為一潭”了。
方法很簡單,配置我們的app文件夾下的build.gradle文件,比如我的
android { ... sourceSets { main { res.srcDirs = [ 'src/main/res', 'src/main/res/layouts', 'src/main/res/layouts/home', 'src/main/res/layouts/hot_sale', 'src/main/res/layouts/amuse', 'src/main/res/layouts/delicacy', 'src/main/res/layouts/food_management', 'src/main/res/layouts/settings', ] } } }
新建相關(guān)文件夾,配置完之后,sync project一下就成功了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View實(shí)現(xiàn)帶音效和震動(dòng)的SeekBar
這篇文章主要為大家詳細(xì)介紹了Android如何自定義View實(shí)一個(gè)帶音效和震動(dòng)的SeekBar,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-03-03Android實(shí)現(xiàn)背景可滑動(dòng)登錄界面 (不壓縮背景彈出鍵盤)
這篇文章主要介紹了Android實(shí)現(xiàn)背景可滑動(dòng)登錄界面 (不壓縮背景彈出鍵盤),需要的朋友可以參考下2017-04-04Android UI設(shè)計(jì)系列之HTML標(biāo)簽實(shí)現(xiàn)TextView設(shè)置中文字體加粗效果(6)
這篇文章主要介紹了Android UI設(shè)計(jì)系列之使用HTML標(biāo)簽,實(shí)現(xiàn)在TextView中對中文字體加粗的效果,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android App內(nèi)監(jiān)聽截圖加二維碼功能代碼
Android截屏功能是一個(gè)常用的功能,可以方便的用來分享或者發(fā)送給好友,本文介紹了如何實(shí)現(xiàn)app內(nèi)截屏監(jiān)控功能,需要的朋友可以參考下2017-08-08Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法
這篇文章主要介紹了Android編程重寫ViewGroup實(shí)現(xiàn)卡片布局的方法,實(shí)例分析新建FlowLayout繼承ViewGroup類及設(shè)置布局文件實(shí)現(xiàn)卡片布局效果的相關(guān)技巧,需要的朋友可以參考下2016-02-02關(guān)于Android Device Monitor 無法打開問題
大家好,本篇文章主要講的是關(guān)于Android Device Monitor 無法打開問題,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01Android Studio 多層級 Module 對 aar 引用問題解決方法
這篇文章主要介紹了Android Studio 多層級 Module 對 aar 引用問題的解決方法,需要的朋友參考下2017-12-12