Android studio 使用gradlew进行打包确实比较灵活,功能强大,特别是面临多个不同版本的时候。以前处理多个不同的版本就是拉取出新的分支,但是还是涉及到bug的修改,包名的不同等问题,给弄得苦不堪言。将项目迁移到Android studio后配置好gradle,这些问题瞬间都不是问题了。
但是在配置gradle的过程中也碰到了好多的问题,上网翻看了好多文章,十分感谢那些作者的努力,下面把我们自己工程中用到的gradle配置文件附上,希望可以帮助到后来人。主要的功能有:
1.自动按照版本号和版本名称及日期重命名apk。
2.自动按照日期升级versionCode。
3.自动将apk文件复制出来。
-
apply plugin: 'com.android.application'
-
apply plugin: 'com.droidtitan.lintcleaner'
-
def releaseTime() {
-
return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
-
}
-
def vCode() {
return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}
-
android {
-
compileSdkVersion 21
-
buildToolsVersion "22.0.1"
-
-
signingConfigs {
-
apply plugin: 'signing'
-
release {
-
storeFile file("../androidkey.keystore")
-
storePassword "yourpassword"
-
keyAlias "youralias"
-
keyPassword "yourkey"
-
}
-
}
-
-
-
defaultConfig {
-
applicationId "your package name"
-
minSdkVersion 15
-
targetSdkVersion 21
-
versionCode Integer.valueOf(vCode())
-
versionName "2.1.10"
-
}
-
//多个版本的打包配置
-
productFlavors {
-
Version1{
-
applicationId "your package name1"
-
manifestPlaceholders = [GAO_DE_KEY: "your gaode key1", UMENG_KEY: "your umeng key1"]
-
}
-
Version2 {
-
applicationId "your package name2"
-
manifestPlaceholders = [GAO_DE_KEY: "your gaode key2", UMENG_KEY: "your umeng key2"]
-
}
-
}
-
-
-
buildTypes {
-
release {
-
signingConfig signingConfigs.release
-
shrinkResources true
-
minifyEnabled true
-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
-
//将release版本的包名重命名,加上版本及日期
-
applicationVariants.all { variant ->
-
variant.outputs.each { output ->
-
def outputFile = output.outputFile
-
if (outputFile != null && outputFile.name.endsWith('release.apk')) {
-
def fileName = "${variant.productFlavors[0].name}_V${defaultConfig.versionName}_${releaseTime()}.apk"
-
output.outputFile = new File(outputFile.parent, fileName)
-
}
-
}
-
}
-
}
-
debug {
-
signingConfig signingConfigs.release
-
minifyEnabled false
-
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
-
}
-
}
-
lintOptions {
-
abortOnError false
-
}
-
-
compileOptions {
-
sourceCompatibility JavaVersion.VERSION_1_7
-
targetCompatibility JavaVersion.VERSION_1_7
-
}
-
-
-
}
-
repositories {
-
maven { url " }
-
}
-
//用来清除无用资源的插件,详见
-
lintCleaner {
-
// Exclude specific files
-
exclude = ['umeng*.png', '*.xml']
-
-
// Ability to ignore all resource files. False by default.
-
ignoreResFiles = true
-
-
}
-
//将打包后的文件复制到build目录下,这样就不用深入到apk目录,同时还看不到unaligned的apk了
-
task copyTask(type: Copy) {
-
from 'build/outputs/apk/'
-
into 'build/'
-
exclude '*-unaligned.apk'
-
}
-
-
task bd(dependsOn: ['assembleDebug', 'assembleRelease', 'copyTask']){
-
copyTask.mustRunAfter assembleRelease
-
}
-
dependencies {
-
compile fileTree(include: ['*.jar'], dir: 'libs')
-
// compile 'com.squareup.retrofit:retrofit:1.9.0'
-
// compile 'com.squareup.okhttp:okhttp:2.3.0'
-
// compile 'com.squareup.okhttp:okhttp-urlconnection:2.3.0'
-
// compile 'com.facebook.stetho:stetho:1.1.1'
-
// compile 'com.facebook.stetho:stetho-okhttp:1.1.1'
-
// compile 'io.reactivex:rxandroid:0.24.0'
-
// compile 'com.jakewharton:butterknife:6.1.0'
-
compile 'com.facebook.fresco:fresco:0.5.2+'
-
compile 'me.imid.swipebacklayout.lib:library:1.0.0'
-
compile 'se.emilsjolander:stickylistheaders:2.6.0'
-
compile 'de.greenrobot:eventbus:2.4.0'
-
compile 'com.github.PhilJay:MPAndroidChart:v2.0.9'
-
compile 'com.google.code.gson:gson:2.3.1'
-
compile 'com.android.support:support-v13:22.2.0'
-
compile project(':carShopSyncLib')
-
}
最后在终端运行命令 gradlew bd即可
阅读(11499) | 评论(0) | 转发(0) |