1.替换符的使用
(1)在 app-android-defaultconfig (或者多渠道打包)下面可以这样使用
1
2
3
4
5
6
7
8
|
android { defaultconfig { manifestplaceholders = [ //高德地图key gdkey: "123456789" , ] } } |
(2)在 androidmanifest.xml 文件的 application 标签下面这样引用
1
2
3
4
|
<!-- 高德地图 --> <meta-data android:name= "com.amap.api.v2.apikey" android:value= "${gdkey}" /> |
2.打包设置appname(启动图标类似,res下面的都可以这样使用)
1
2
3
4
5
6
|
android { defaultconfig { //在string.xml中不能出现app_name这个字段,否则生成报错 resvalue "string" , "app_name" , "app名称" } } |
3.生成buildconfig.java字段
在build.gradle中
1
2
3
4
5
6
7
8
|
android { defaultconfig { //生成一个boolea类型的变量 buildconfigfield "boolean" , "is_test_url" , "true" //生成一个字符串变量 buildconfigfield "string" , "app_update_time" , "\"${system.currenttimemillis().tostring()}\"" } } |
在java代码
1
2
3
4
5
|
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")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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.设置签名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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名
1
2
3
4
5
6
7
8
9
|
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中排除
1
2
3
4
|
configurations { compile.exclude module: 'commons' all*.exclude group: 'org.gradle.test.excludes' , module: 'reports' } |
2.在具体的某个dependency中排除
1
2
3
4
5
|
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代码。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
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文件就可以这样来引用了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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文件,比如我的
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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一下就成功了
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.jianshu.com/p/2b0036af4ef3