Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1540097
  • 博文数量: 113
  • 博客积分: 3526
  • 博客等级: 中校
  • 技术积分: 1815
  • 用 户 组: 普通用户
  • 注册时间: 2009-09-08 09:46
个人简介

记录总结自己的工作

文章分类

全部博文(113)

文章存档

2015年(19)

2014年(10)

2013年(6)

2012年(16)

2011年(24)

2010年(21)

2009年(17)

分类: Android平台

2015-07-02 15:37:03

    Android studio 使用gradlew进行打包确实比较灵活,功能强大,特别是面临多个不同版本的时候。以前处理多个不同的版本就是拉取出新的分支,但是还是涉及到bug的修改,包名的不同等问题,给弄得苦不堪言。将项目迁移到Android studio后配置好gradle,这些问题瞬间都不是问题了。
    但是在配置gradle的过程中也碰到了好多的问题,上网翻看了好多文章,十分感谢那些作者的努力,下面把我们自己工程中用到的gradle配置文件附上,希望可以帮助到后来人。主要的功能有:

    1.自动按照版本号和版本名称及日期重命名apk。
    2.自动按照日期升级versionCode。
    3.自动将apk文件复制出来。


点击(此处)折叠或打开

  1. apply plugin: 'com.android.application'
  2. apply plugin: 'com.droidtitan.lintcleaner'
  3. def releaseTime() {
  4.     return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
  5. }
  6. def vCode() {
        return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
    }

  7. android {
  8.     compileSdkVersion 21
  9.     buildToolsVersion "22.0.1"

  10.     signingConfigs {
  11.         apply plugin: 'signing'
  12.         release {
  13.             storeFile file("../androidkey.keystore")
  14.             storePassword "yourpassword"
  15.             keyAlias "youralias"
  16.             keyPassword "yourkey"
  17.         }
  18.     }


  19.     defaultConfig {
  20.         applicationId "your package name"
  21.         minSdkVersion 15
  22.         targetSdkVersion 21
  23.         versionCode Integer.valueOf(vCode())
  24.         versionName "2.1.10"
  25.     }
  26.     //多个版本的打包配置
  27.     productFlavors {
  28.         Version1{
  29.             applicationId "your package name1"
  30.             manifestPlaceholders = [GAO_DE_KEY: "your gaode key1", UMENG_KEY: "your umeng key1"]
  31.         }
  32.         Version2 {
  33.             applicationId "your package name2"
  34.             manifestPlaceholders = [GAO_DE_KEY: "your gaode key2", UMENG_KEY: "your umeng key2"]
  35.         }
  36.     }


  37.     buildTypes {
  38.         release {
  39.             signingConfig signingConfigs.release
  40.             shrinkResources true
  41.             minifyEnabled true
  42.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  43.             //将release版本的包名重命名,加上版本及日期
  44.             applicationVariants.all { variant ->
  45.                 variant.outputs.each { output ->
  46.                     def outputFile = output.outputFile
  47.                     if (outputFile != null && outputFile.name.endsWith('release.apk')) {
  48.                         def fileName = "${variant.productFlavors[0].name}_V${defaultConfig.versionName}_${releaseTime()}.apk"
  49.                         output.outputFile = new File(outputFile.parent, fileName)
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.         debug {
  55.             signingConfig signingConfigs.release
  56.             minifyEnabled false
  57.             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
  58.         }
  59.     }
  60.     lintOptions {
  61.         abortOnError false
  62.     }

  63.     compileOptions {
  64.         sourceCompatibility JavaVersion.VERSION_1_7
  65.         targetCompatibility JavaVersion.VERSION_1_7
  66.     }


  67. }
  68. repositories {
  69.     maven { url " }
  70. }
  71. //用来清除无用资源的插件,详见
  72. lintCleaner {
  73.     // Exclude specific files
  74.     exclude = ['umeng*.png', '*.xml']

  75.     // Ability to ignore all resource files. False by default.
  76.     ignoreResFiles = true

  77. }
  78. //将打包后的文件复制到build目录下,这样就不用深入到apk目录,同时还看不到unaligned的apk了
  79. task copyTask(type: Copy) {
  80.     from 'build/outputs/apk/'
  81.     into 'build/'
  82.     exclude '*-unaligned.apk'
  83. }

  84. task bd(dependsOn: ['assembleDebug', 'assembleRelease', 'copyTask']){
  85.     copyTask.mustRunAfter assembleRelease
  86. }
  87. dependencies {
  88.     compile fileTree(include: ['*.jar'], dir: 'libs')
  89.     // compile 'com.squareup.retrofit:retrofit:1.9.0'
  90.     // compile 'com.squareup.okhttp:okhttp:2.3.0'
  91.     // compile 'com.squareup.okhttp:okhttp-urlconnection:2.3.0'
  92.     // compile 'com.facebook.stetho:stetho:1.1.1'
  93.     // compile 'com.facebook.stetho:stetho-okhttp:1.1.1'
  94.     // compile 'io.reactivex:rxandroid:0.24.0'
  95.     // compile 'com.jakewharton:butterknife:6.1.0'
  96.     compile 'com.facebook.fresco:fresco:0.5.2+'
  97.     compile 'me.imid.swipebacklayout.lib:library:1.0.0'
  98.     compile 'se.emilsjolander:stickylistheaders:2.6.0'
  99.     compile 'de.greenrobot:eventbus:2.4.0'
  100.     compile 'com.github.PhilJay:MPAndroidChart:v2.0.9'
  101.     compile 'com.google.code.gson:gson:2.3.1'
  102.     compile 'com.android.support:support-v13:22.2.0'
  103.     compile project(':carShopSyncLib')
  104. }
    最后在终端运行命令 gradlew bd即可

阅读(11424) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~