本文將介紹平常android開發(fā)當(dāng)中項(xiàng)目module的gradle文件中的基本配置(不包含創(chuàng)建的library進(jìn)行上傳到j(luò)center的腳本配置)怠惶,以及針對一些常見的問題進(jìn)行說明宗侦。
歡迎加入交流群:微信群(AndroidRunner )践宴、QQ群(314896948)
歡迎關(guān)注微信公眾號:AndroidRunner
在項(xiàng)目的module目錄下的gradle文件主要包含三個節(jié)點(diǎn)兢交,分別是apply、android和dependencies会烙,下面就將這三個節(jié)點(diǎn)分別做一個介紹半等。
1.apply節(jié)點(diǎn)
apply所標(biāo)識的用以區(qū)分了application以及l(fā)ibrary。這個節(jié)點(diǎn)的作用就是用來區(qū)分當(dāng)前module是可以作為應(yīng)用程序還是作為一個應(yīng)用程序的支持庫module拾氓。
application module使用如下:
applyplugin:'com.android.application'
library使用如下:
apply plugin: 'com.android.library'
2.android節(jié)點(diǎn)
-
packagingOptions
packagingOptions { pom.properties' exclude 'project.properties' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/LICENSE' exclude 'META-INF/NOTICE.txt' exclude 'META-INF/NOTICE' exclude 'META-INF/DEPENDENCIES.txt' exclude 'META-INF/DEPENDENCIES' }
我們在開發(fā)過程當(dāng)中可能會遇到這樣的問題冯挎,導(dǎo)入了很多的三方庫或者框架,在項(xiàng)目編譯的時候會報(bào)重復(fù)文件錯誤咙鞍,如下圖
解決這個錯誤的方法就可以在packagingOptions中加入以下兩句:
exclude 'META-INF/maven/com.squareup.okio/okio/pom.xml'
exclude 'META-INF/maven/com.squareup.okio/okio/pom.properties'
-
lintOptions
lintOptions { disable 'MissingTranslation' abortOnError false ignoreWarnings true }
其中disable 'MissingTranslation'
是在lint檢查的時候禁用翻譯缺少檢查织堂,如果你的項(xiàng)目不考慮國際化可以加上,如果需要國際化就去掉該行即可奶陈。abortOnError false
設(shè)置在lint檢查時如果出現(xiàn)error是否需要中斷,如果對lint檢查要求不高可以設(shè)置為false附较,關(guān)心lint檢查的話設(shè)置true即可吃粒。ignoreWarnings true
設(shè)置是否忽略警告,在lint檢查時會對一些代碼進(jìn)行警告比如有用了過時API的注解地方拒课,一般可以設(shè)置為true徐勃,忽略掉警告。lintOptions還有其他一些控制開關(guān)早像,具體可以點(diǎn)進(jìn)lintOptions源碼查看僻肖。
-
signingConfigs
signingConfigs { debug { keyAlias 'android' keyPassword 'android' storeFile file('E:/debug.jks') storePassword 'android' } release { keyAlias 'psw' keyPassword 'psw' storeFile file('keystore addresss') storePassword 'psw' } }
signingConfigs是項(xiàng)目module的簽名配置信息,主要包括調(diào)試版和發(fā)布版卢鹦,考慮隱私信息可以僅使用debug即可臀脏,release在導(dǎo)出包使用手動添加。幾個字段含義應(yīng)該開發(fā)者都比較清楚了冀自,這里不做介紹揉稚。
-
compileSdkVersion
compileSdkVersion 23
編譯SDk的版本號,改版本號決定使用的SDK對應(yīng)的android源碼熬粗,android源碼隨著版本的不斷更新搀玖,各個版本間也有著一定的區(qū)別,這里所填寫的版本號關(guān)系到具體代碼的一些API的使用驻呐,可以根據(jù)當(dāng)前APP的版本兼容要求做相應(yīng)的兼容灌诅。
-
buildToolsVersion
buildToolsVersion "23.0.2"
使用的編譯工具版本號指定芳来,對于應(yīng)編譯版本號即可。
-
defaultConfig
defaultConfig { applicationId "com.xxx"http://app的唯一標(biāo)識符猜拾,一般使用包名 minSdkVersion 14 //app要求最低的SDK版本號 對于應(yīng)android可使用的最低系統(tǒng)版本 targetSdkVersion 23 //目標(biāo)版本SDK版本號 versionCode 1 // app版本號 versionName "1.0.0" // app版本名稱 // 多渠道打包默認(rèn)渠道(以umeng為例) manifestPlaceholders = [UMENG_CHANNEL_VALUE: "GF"] }
defaultConfig里面可以設(shè)置app的一些相關(guān)默認(rèn)配置信息即舌,包括應(yīng)用的id,可使用最低版本,版本號关带,版本名稱等侥涵。
-
buildTypes
buildTypes { debug { minifyEnabled false zipAlignEnabled false shrinkResources false } release { minifyEnabled true zipAlignEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }
buildTypes設(shè)置編譯對應(yīng)的版本時所需的一些配置設(shè)置,可以針對調(diào)試版和發(fā)布版進(jìn)行不同的設(shè)置是否需要混淆宋雏、是否需要壓縮芜飘、是否需要刪減無用資源等。
-
sourceSets
sourceSets.main { jniLibs.srcDir 'libs' }
sourceSets用來設(shè)置項(xiàng)目對應(yīng)文件資源的路徑位置磨总,比如習(xí)慣平常的把jni文件so包存放在libs目錄下即可設(shè)置jniLibs.srcDir 'libs'
嗦明,也可以設(shè)置其他文件目錄,包括源碼蚪燕,資源娶牌,jar包等等。
-
productFlavors
productFlavors { GF {} wandoujia {} //... add more } productFlavors.all { flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name] }
productFlavors提供了多渠道打包的渠道設(shè)置馆纳,成列了可生產(chǎn)的取到列表诗良,productFlavors.all通過遍歷productFlavors中的所有條目對應(yīng)替換AndroidManifest.xml下面meta節(jié)點(diǎn)key為UMENG_CHANNEL_VALUE的value,從而生成對應(yīng)渠道的apk包
3.dependencies
dependencies下面是項(xiàng)目的依賴第三方支持庫存放鲁驶,包括libs目錄及目錄樹下子目錄所有的jar包鉴裹,以及按照jcenter倉庫庫命名的的三方支持庫。樣例如下:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.squareup.picasso:picasso:2.5.2' //picasso
compile 'com.squareup.okio:okio:1.6.0' //okio
compile 'com.squareup:otto:1.3.8' //otto
compile 'com.alibaba:fastjson:1.2.7' //fastjson
}
下面給出一個完整的gradle基本配置#
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'project.properties'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/DEPENDENCIES.txt'
exclude 'META-INF/DEPENDENCIES'
}
lintOptions {
disable 'MissingTranslation'
abortOnError false
ignoreWarnings true
}
signingConfigs {
debug {
keyAlias 'android'
keyPassword 'android'
storeFile file('E:/debug.jks')
storePassword 'android'
}
release {
keyAlias 'psw'
keyPassword 'psw'
storeFile file('keystore addresss')
storePassword 'psw'
}
}
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.xxx"http://app的唯一標(biāo)識符钥弯,一般使用包名
minSdkVersion 14 //app要求最低的SDK版本號 對于應(yīng)android可使用的最低系統(tǒng)版本
targetSdkVersion 23 //目標(biāo)版本SDK版本號
versionCode 1 // app版本號
versionName "1.0.0" // app版本名稱
// 多渠道打包默認(rèn)渠道(以umeng為例)
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "GF"]
}
buildTypes {
debug {
minifyEnabled false
zipAlignEnabled false
shrinkResources false
}
release {
minifyEnabled true
zipAlignEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets.main {
jniLibs.srcDir 'libs'
}
productFlavors {
GF {}
wandoujia {}
//... add more
}
productFlavors.all { flavor ->
flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.squareup.picasso:picasso:2.5.2' //picasso
compile 'com.squareup.okio:okio:1.6.0' //okio
compile 'com.squareup:otto:1.3.8' //otto
compile 'com.alibaba:fastjson:1.2.7' //fastjson
}