1.替換符的使用
為麥積等地區用戶提供了全套網頁設計制作服務,及麥積網站建設行業解決方案。主營業務為做網站、網站制作、麥積網站設計,以傳統方式定制建設網站,并提供域名空間備案等一條龍服務,秉承以專業、用心的態度為用戶提供真誠的服務。我們深信只要達到每一位用戶的要求,就會得到認可,從而選擇與我們長期合作。這樣,我們也可以走得更遠!
(1)在 app-android-defaultConfig (或者多渠道打包)下面可以這樣使用
android { defaultConfig { manifestPlaceholders = [ //高德地圖key GDKEY: "123456789", ] } }
(2)在 AndroidManifest.xml 文件的 application 標簽下面這樣引用
<!-- 高德地圖 --> <meta-data android:name="com.amap.api.v2.apikey" android:value="${GDKEY}" />
2.打包設置appname(啟動圖標類似,res下面的都可以這樣使用)
android { defaultConfig { //在string.xml中不能出現app_name這個字段,否則生成報錯 resValue "string", "app_name", "app名稱" } }
3.生成BuildConfig.java字段
在build.gradle中
android { defaultConfig { //生成一個boolea類型的變量 buildConfigField "boolean", "IS_TEST_URL", "true" //生成一個字符串變量 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.設置簽名
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時mm分") outputFileName = "${variant.flavorName}(${variant.versionCode}_${variant.versionName})(${time.format(nowTime)}).apk" } } }
7.引入第三方庫的時候,剔除某些不需要的包或者重復的包
1.直接在configuration中排除
configurations { compile.exclude module: 'commons' all*.exclude group: 'org.gradle.test.excludes', module: 'reports' }
2.在具體的某個dependency中排除
dependencies { implementation("com.github.bumptech.glide:glide:4.8.0") { exclude module: 'appcompat-v7' } }
PS:Android Studio開發Build.gradle小技巧
引用版本統一規范
Android開發存在著眾多版本的不同,比如compileSdkVersion、minSdkVersion、targetSdkVersion以及項目中依賴第三方庫的版本,不同的module及不同的開發人員都有不同的版本,所以需要一個統一版本規范的文件,現在我就來介紹一種方式。
在項目根目錄,也就是跟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" ] }
有了這個規范,那么我們在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文件即可把所有依賴版本都修改
對資源進行分包
我們可以對每個頁面的資源都進行具體分類,不只是layout,還有drawable及value,是不是心動了,趕緊照著如下配置試一試吧,別再讓資源文件們“混為一潭”了。
方法很簡單,配置我們的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', ] } } }
新建相關文件夾,配置完之后,sync project一下就成功了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。
當前標題:淺析Android中build.gradle的實用技巧
瀏覽路徑:http://m.newbst.com/article20/gceeco.html
成都網站建設公司_創新互聯,為您提供ChatGPT、小程序開發、微信小程序、響應式網站、電子商務、動態網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯