編輯:關於Android編程
筆者目前使用的Gradle配置描述如下:
defaultConfig { //項目包名配置 applicationId "com.wildcreek.demo" minSdkVersion 16 targetSdkVersion 19 versionCode 14 versionName "1.0.14" }
signingConfigs { release {//發布版本的簽名配置 storeFile file(props['KEYSTORE_FILE']) keyAlias props['KEY_ALIAS'] storePassword props['KEYSTORE_PWD'] keyPassword props['KEY_PWD'] } debug {//調試版本的簽名配置 storeFile file(props['DEBUG_KEYSTORE']) keyAlias props['DEBUG_ALIAS'] storePassword props['DEBUG_KEYSTORE_PWD'] keyPassword props['DEBUG_KEY_PWD'] } }
簽名配置文件signing.properties:
KEYSTORE_FILE = release.keystore KEY_ALIAS = wildcreek KEYSTORE_PWD= xxx KEY_PWD= xxx DEBUG_KEYSTORE= debug.keystore DEBUG_ALIAS= androiddebugkey DEBUG_KEYSTORE_PWD= android DEBUG_KEY_PWD= android
注意:
1 .minifyEnabled 會對代碼進行混淆和壓縮,shrinkResources 會對比R文件對無用資源進行刪除
2.minifyEnabled 設置為true時shrinkResources 的設置才會生效
buildTypes {
release {
debuggable true
minifyEnabled true //啟用Proguard
shrinkResources true //是否清理無用資源,依賴於minifyEnabled
zipAlignEnabled true //是否啟用zipAlign壓縮
signingConfig signingConfigs.release
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
debug {
debuggable true
minifyEnabled false //不啟用Proguard
shrinkResources false //是否清理無用資源,依賴於minifyEnabled
zipAlignEnabled false //是否啟用zipAlign壓縮
signingConfig signingConfigs.debug
}
}
4,productFlavors
自定義BuildConfig,工程編譯後會生成BuildConfig類,該類會包含自定義字段。
假設工程包含beijing和shandong兩個productFlavors,且擁有各自不同的服務器等配置信息。
productFlavors{
beijing{
buildConfigField("boolean", "IS_LOCAL", "false")
buildConfigField("String", "SERVER_NAME", "\"BJ\"")
buildConfigField("String", "SERVER_HOST", "\"http://xxx.xxx.xxx.xx:8080\"")
buildConfigField("String", "LOGIN_API", "\"login\"")
}
shandong{
buildConfigField("boolean", "IS_LOCAL", "false")
buildConfigField("String", "SERVER_NAME", "\"SD\"")
buildConfigField("String", "SERVER_HOST", "\"http://xxx.xxx.xxx.xx:8080\"")
buildConfigField("String", "LOGIN_API", "\"loginsd\"")
}
}
5,打包apk重命名
def releaseTime() {
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (variant.buildType.name.equals('release')) {
def fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_release.apk"
if(variant.flavorName.equals("beijing")){
fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_beijing_release.apk"
}else if(variant.flavorName.equals("shandong")){
fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_release.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}else if(variant.buildType.name.equals('debug')){
def fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_debug.apk"
if(variant.flavorName.equals("beijing")){
fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_beijing_debug.apk"
}else if(variant.flavorName.equals("shandong")){
fileName = "Demo_v${defaultConfig.versionName}_${releaseTime()}_shandong_debug.apk"
}
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
6,dependencies 依賴配置
不同buildTypes 和productFlavors 依賴不同的jar的配置情況
dependencies {
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:support-v4:22.2.1'
compile 'com.google.code.gson:gson:2.2.1'
compile 'net.robinx:lib.blur:1.0.1'
debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5'
releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5'
compile fileTree(dir: 'libs', exclude:["*mtclib*.jar","*peerconnection*.jar"], include: "*.jar")
beijingCompile files("libs/mtclib_0908_bj.jar")
beijingCompile files("libs/libjingle_peerconnection_bj.jar")
shandongCompile files("libs/mtclib_170110_sd.jar")
shandongCompile files("libs/libjingle_peerconnection_sd.jar")
}
Github gist:
完整配置參見:
https://gist.github.com/fcf96ba9d265c09694fb24fd5ed34ee7
這套框架終於到了完結篇了,這篇就算是整個框架的重點了,涉及的不僅是重做和撤銷功能,更重要的是牽扯整個框架的架構。之前我們實現的功能是添加圖片和用畫筆繪畫,我們的做法是在一
Android模擬器報Failed To Allocate memory 8錯誤的解決辦法Android在啟動模擬器AVD時,出現下面的異常:“Failed to all
Eclipse/ADTADT是Google在Eclipse基礎上封裝了Android開發工具的環境,最新版本是2014年7月2日發布的adt-bundle-windows
一.Path常用方法表 為了兼容性(偷懶) 本表格中去除了在API21(即安卓版本5.0)以上才添加的方法。忍不住吐槽一下,為啥看起來有些順手就能寫的重載方法