開發(fā)一個(gè)Android項(xiàng)目不僅僅需要你會(huì)寫java/kotlin代碼芭毙,而且你還要了解各種配置文件筋蓖。例如。AndroidManifest.xml退敦,混淆文件粘咖,build.gradle等。這里面最難理解也是最重要的非build.gradle莫屬了侈百,接下來(lái)我們就講一講一個(gè)成熟的項(xiàng)目的build.gradle文件是怎么樣的:
首先我們建立一個(gè)新工程并打開build.gradle文件
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "26.0.0"
defaultConfig {
applicationId "com.example.ljw.basedemo"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
testCompile 'junit:junit:4.12'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
這是AS默認(rèn)給我們生成的build.gradle瓮下,我們需要對(duì)它進(jìn)行一個(gè)改造:
如果你不需要單元測(cè)試我們可以刪掉這些文件:
// testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
//androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
// exclude group: 'com.android.support', module: 'support-annotations'
//})
// testCompile 'junit:junit:4.12'
ok,刪完了以后我們就可愉快的開始我們build.gradle的建立了翰铡,
首先,一個(gè)項(xiàng)目想要發(fā)布讽坏,簽名是必要的東西锭魔,所以我們要配置一下簽名文件:
signingConfigs {
storeFile file('../key/yourname.jks') //你的jks文件路徑
storePassword "youstorepassword"
keyAlias "youAlias"
keyPassword "yourpassword"
}
如果你的項(xiàng)目夠大那這句話是必須的
// dex突破65535的限制
multiDexEnabled true
接下來(lái)是配置我們的buildTypes,具體解釋見(jiàn)注釋
buildTypes {
debug {
// 顯示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
//混淆
minifyEnabled false
//Zipalign優(yōu)化
zipAlignEnabled false
// 移除無(wú)用的resource文件
shrinkResources false
signingConfig null
}
release {
// 不顯示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
minifyEnabled true
zipAlignEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱為ljw_v1.0__baidu.apk
def fileName = "ljw_v${variant.productFlavors[0].versionName}_${variant.name}.apk"
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
}
}
為gradle添加UTF-8支持
tasks.withType(JavaCompile) {
options.encoding = "UTF-8"
}
最后就是多渠道打包了
productFlavors {
liuliang {//這里你可以給不同渠道的包設(shè)置不同的版本號(hào),名稱路呜,甚至applicationId迷捧,若是你設(shè)置不同的applicationId了,那你的不同渠道的包就可以同時(shí)存在在一個(gè)手機(jī)上胀葱。 }
xiaomi { }
wandoujia {}
OPPO {}
Lenovo { }
Unicom {}
qh360 { }
VIVO { }
sougou { }
anzhi { }
leshi { }
ppzhushou { }
_91zhushou { }
yingyonghui { }
baidu {}
yingyongbao { }
huawei { }
meizu { }
}
ok漠秋,基本上一個(gè)項(xiàng)目的build.gradle文件差不多就配置好了,如果有特殊需要你們?cè)谧约杭訓(xùn)|西抵屿。
下一篇文章預(yù)告-Android 項(xiàng)目開發(fā)必備-為你的項(xiàng)目選擇優(yōu)質(zhì)框架