apply plugin: 'com.android.application' //代表你是一個應用
def releaseTime() { //這是gradle語法,聲明瞭一個獲取當前時間的方法
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}
android { //你的應用的配置情況
compileSdkVersion 21 //編譯你的應用的SDK版本
buildToolsVersion '21.1.2' //使用的tool版本
defaultConfig { //默認配置
applicationId "com.boohee.*" //你的包名
minSdkVersion 14 //兼容最低的Android的版本
targetSdkVersion 21 //一般跟compileSdkVersion一樣就可以了
versionCode 1 //版本號,只知道更新用的着
versionName "1.0" //用於改動幅度不大的APP,而又區別原來APP
// dex突破65535的限制 --支持多dex文件
multiDexEnabled true
}
lintOptions { //true--錯誤發生後停止gradle,
//瞭解更多http://blog.csdn.net/berber78/article/details/60766091
abortOnError false
}
signingConfigs {
debug {
// No debug config
}
release {
storeFile file("../yourapp.keystore") //在項目的根目錄下
storePassword "your password"
keyAlias "your alias"
keyPassword "your password"
}
}
buildTypes {
debug {
// 顯示Log
buildConfigField "boolean", "LOG_DEBUG", "true"
versionNameSuffix "-debug" //版本的後綴 如:XXXX-debug.apk
minifyEnabled false//是否混淆代碼
zipAlignEnabled false
shrinkResources false
signingConfig signingConfigs.debug
}
release {
// 不顯示Log
buildConfigField "boolean", "LOG_DEBUG", "false"
minifyEnabled true
zipAlignEnabled true //是否壓縮
// 移除無用的resource文件
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//混淆配置文件定要配好
signingConfig signingConfigs.release
applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
// 輸出apk名稱爲boohee_v1.0_2015-01-15_wandoujia.apk
def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
//${variant.buildType.name} (debug,release)字段
output.outputFile = new File(outputFile.parent+"/${variant.buildType.name}", fileName)
}
}
}
}
}
}
dependencies { //你的依賴
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3'
compile 'com.jakewharton:butterknife:6.0.0'
...
}
http://stormzhang.com/devtools/2015/01/15/android-studio-tutorial6/ 引用自大神的代碼,如有出錯或有更多更好的資源請留言