1.各種不想解決的黃色小提示,不想寫contentDescription,設(shè)計稿上字體就是小于12sp等等
lintOptions {
//發(fā)生錯誤的時候停止構(gòu)建
abortOnErrortrue
//不忽略警告
ignoreWarningsfalse
//lint 規(guī)則配置
lintConfig file("../lint.xml")
}
然后具體的lint規(guī)則配置:lint.xml
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<!-- list of issues to configure -->
<issue id="SmallSp" severity="ignore" />
<issue id="ContentDescription" severity="ignore" />
</lint>
2.簽名配置 signingConfigs--使用gradle打包的時候
signingConfigs {
config {
keyAlias KEY_ALIAS
keyPassword KEY_PASSWORD
storeFile file(STORE_FILE)
storePassword STORE_PASSWORD
}
}
新建一個Android 項目的時候,,rootProject下面會生成gradle.properties和local.properties(sdk 位置)文件,gradle.properties中的內(nèi)容不需要顯示調(diào)用,可以直接在build.gradle中使用,如上。當(dāng)配置比較簡單的時候,可以將這些內(nèi)容直接在gradle.properties配置。
gradle.properties中的內(nèi)容:
KEY_ALIAS=測試
KEY_PASSWORD=666666
STORE_FILE=../測試.jks
STORE_PASSWORD=666666
當(dāng)然,gradle.properties也可以配置其他很多東西,SDK Version,app version 啊,第三方的key secret啊,當(dāng)然這些東西多了以后,你就會發(fā)現(xiàn)我的天,這個
gradle.properties里面怎么什么都有?!
這個時候,我們就需要新建一個**.properties文件了,就跟你有一個student bean ,然后又有一堆course相關(guān)的屬性一樣,當(dāng)然你可以放到student里面,但是最好還是新建一個course類啦,好,我們現(xiàn)在新建一個keystore.properties,把上圖的內(nèi)容放到keystore.properties里面,有多個環(huán)境的key就每個分開寫,那在build.gradle里面怎么使用這個新建的properties呢?
def keystorePropertiesFile = rootProject.file("keystore.properties")
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
為啥要定義變量呢,因為你接下來要用啊,2333...怎么用呢,不要著急,馬上來了:
keyAlias keystoreProperties['keyAlias']
keyPassword keystoreProperties['keyPassword']
storeFile file("../${keystoreProperties['storeFile']}")
storePassword keystoreProperties['storePassword']
其他properties請自由發(fā)揮。
3.剛才講到多個環(huán)境,為啥會有多個環(huán)境呢,哦,因為dev一個環(huán)境,QA一個環(huán)境,prod一個環(huán)境呀,這么多環(huán)境請求地址這些都不一樣,怎么辦呢?好辦!
productFlavors了解一下。你可以在這里面干些什么呢?
先分一下環(huán)境吧
prod{
}
qa{
}
dev{
}
A.不同的url地址:
prod{
buildConfigField 'String', 'BASE_URL', '"https://www.baidu.com"'
}
qa{
buildConfigField 'String', 'BASE_URL', '"https://www.google.com"'
}
好,你在代碼里面請求的時候,只需要調(diào)用BuildConfig.BASE_URL就可以了,麻麻再也不用擔(dān)心切換環(huán)境了。
gradle3.0以后,需要在defaultConfig里面添加flavorDimensions 屬性
defaultConfig {
applicationId "com.test"
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
versionCode VERSION_CODE as int
versionName VERSION_NAME
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
flavorDimensions "channel","charge","env"
}
flavorDimensions是將Flavors中的內(nèi)容進行分類,比如渠道(百度,360,應(yīng)用寶等等),比如是否收費免費之類的,比如環(huán)境,然后不同分類可以互相組合,比如百度收費,百度免費,360收費,360免費等等
申請這個屬性以后,需要在Flavors中聲明這是屬于哪個分類,所以將之前的改寫成
prod{
dimension "env"
buildConfigField 'String', 'BASE_URL', '"https://www.baidu.com"'
}
qa{
dimension "env"
buildConfigField 'String', 'BASE_URL', '"https://www.google.com"'
}
B.不同的環(huán)境不同的簽名文件
prod{
signingConfig signingConfigs.prod
}
qa{
signingConfig signingConfigs.qa
}
這個signingConfigs.prod和signingConfigs.qa是什么呀?哦,自己在signingConfigs里面添加的啊,欲知后事如何,需繳納五塊錢:
signingConfigs{
prod{
keyAlias keystoreProperties['keyAliasStage']
keyPassword keystoreProperties['keyPasswordStageProd']
storeFile file("../${keystoreProperties['storeFileStageProd']}")
storePassword keystoreProperties['storePasswordStageProd']
}
qa{
keyAlias keystoreProperties['keyAliasStageQa']
keyPassword keystoreProperties['keyPasswordStageProdQa']
storeFile file("../${keystoreProperties['storeFileStageProdQa']}")
storePassword keystoreProperties['storePasswordStageProdQa']
}
}
嗯,看完上訴,你的信用卡已自動扣款五塊,感謝各位支持!!??
你說啥?五塊錢還想要手機上同時安裝各個環(huán)境的包?!
好嘛,applicationIdSuffix了解一下,同樣還是在我們的productFlavors里面:
prod{
applicationIdSuffix ".prod"
}
qa{
applicationIdSuffix ".qa"
}
你說啥?包名變了第三方庫沒法用了?唉,你的五塊錢好值錢啊。
寫在build.gradle里面的,都可以像2那樣,放在.properties了,放在AndroidManifest里面的呢,額,不告訴你!
<meta-data
android:name="**.***.ApiKey"
android:value="${***_apikey}" />
這個好像JS啊,啊,是啊,我也不知道什么像什么,那value的值寫到哪里呢,都跟你說productFlavors了解一下啦,當(dāng)然這個不寫在productFlavors里面,哈哈哈,有本事跟著網(wǎng)線爬過來打我啊,我用無線網(wǎng),哈哈哈
android {
buildTypes {
prod {
manifestPlaceholders = [**_apikey: "12345664"]
}
qa {
manifestPlaceholders = [**_apikey: "123456677"]
}
}
}
不同環(huán)境不同的第三方簽名,意味著你可以對不同環(huán)境發(fā)生的各種事情進行監(jiān)控,嗯,你懂的。
4.configurations 依賴分組
在Gradle中,依賴都被會分配到某一個具體的configuration中。Configuration代表著一個或多個構(gòu)件及構(gòu)件所需依賴的一個分組。
系統(tǒng)已經(jīng)預(yù)定義了一些configuration,比如,compile,runtime,testCompile,testRuntime等,這個好像是gradle3.0以前,3.0以后是implementation,testImplementation。
compile/implementation 放在這個configuration下的依賴是在編譯產(chǎn)品代碼時所使用的,但它作為一個分組,包含產(chǎn)品代碼和編譯所需的依賴。
runtime 產(chǎn)品代碼在運行時需要的依賴,默認(rèn),也會包含compile中的依賴。
testCompile 編譯測試代碼時所需要的依賴,默認(rèn),被編譯的產(chǎn)品代碼和產(chǎn)品代碼需要的編譯依賴也屬于該分組。
testRuntime 運行測試時需要的依賴。默認(rèn),包含compile,runtime和testCompile的分組的構(gòu)建和依賴。
上面??這一大段都是抄的!感謝復(fù)制粘貼讓我少些了好多字,廢話少說,比如我們又一個監(jiān)控內(nèi)存泄漏的leakcanary,這個玩意兒引入以后,會生產(chǎn)一個查看內(nèi)存泄漏的app,但是在我們版本發(fā)布以后我們是不想要這個的,為啥不想要???你還買一送一啊?!所以我們經(jīng)常會區(qū)分debug版本和release版本,當(dāng)然不同環(huán)境下可能需求也不一樣,所以:
prodDebugCompile "com.squareup.leakcanary:leakcanary-android:1.5"
prodReleaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:1.5"
qaDebugCompile "com.squareup.leakcanary:leakcanary-android:1.5"
qaReleaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:1.5"
然后配置configuration:
configurations {
prodReleaseCompile
prodDebugCompile
qaReleaseCompile
qaDebugCompile
}
這樣,在我們打包的時候,gradle就會根據(jù)不同場景打包不同的依賴。