一个完整Android项目所需要用到的gradle配置技巧

来源:互联网 发布:js new date(s) 编辑:程序博客网 时间:2024/06/02 00:37

Gradle配置技巧

本人总结了四条gradle配置技巧,这些技巧配置在Android项目中可以更加的方便工程的调试和打包等等 
1.使用gradle的自定义Property实现Android项目的配置和依赖统一管理 
2.使用gradle的productFlavors实现Android项目多渠道打包 
3.使用gradle实现Android项目debug版与release版共存 
4.使用gradle实现批量修改生成的apk文件名

完整的Android项目gradle配置:

完整gradle配置所用到的技巧可以参考上面的四条gradle技巧总结

项目根目录的build.gradle配置

// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript {    repositories {        jcenter()    }    dependencies {        classpath 'com.android.tools.build:gradle:2.2.3'        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'        classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'    }}plugins {    id "com.jfrog.bintray" version "1.6"}allprojects {    repositories {        jcenter()        maven { url "https://jitpack.io" }    }}task clean(type: Delete) {    delete rootProject.buildDir}ext {    //Android configs    configs = [            applicationId    : "com.example.myapplication",            compileSdkVersion: 25,            buildToolsVersion: '25.0.2',            minSdkVersion    : 17,            targetSdkVersion : 25,            versionCode      : 17,            versionName      : '3.1.5'    ]    // App dependencies    supportLibraryVersion = '25.1.1'    junitVersion = '4.12'    multidexVersion = '1.0.1'    gsonVersion = '2.4'    butterknifeVersion = '8.4.0'    ......}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48

app目录下的build.gradle配置

宁波整形医院http://www.zuanno.com/
宁波最好的整形医院http://www.zuanno.com/

apply plugin: 'com.android.application'apply plugin: 'android-apt'def buildTime() {    def date = new Date()    def formattedDate = date.format('yyyy-MM-dd', TimeZone.getTimeZone("UTC"))    return formattedDate}android {    signingConfigs {        release {            storeFile file('../yourapp.keystore')            keyAlias 'your password'            keyPassword 'your alias'            storePassword 'your password'        }    }    compileSdkVersion configs.compileSdkVersion    buildToolsVersion configs.buildToolsVersion    defaultConfig {        applicationId configs.applicationId        minSdkVersion configs.minSdkVersion        targetSdkVersion configs.targetSdkVersion        versionCode configs.versionCode        versionName configs.versionName        // Enabling multidex support.        multiDexEnabled true    }    buildTypes {        release {            //开启混淆            minifyEnabled true            //打包时自动读取签名配置文件            signingConfig signingConfigs.release            //开启zip对齐            zipAlignEnabled true             //移除无用的resource文件            shrinkResources true             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            //设置release版本的app_name和application_id            manifestPlaceholders = [                    APP_NAME      : "@string/app_name",                    APPLICATION_ID: "@string/application_id"            ]            //设置release版本只包含armeabi和armeabi-v7a的so包            ndk {                abiFilters "armeabi", "armeabi-v7a"            }            //批量修改生成的apk文件名            applicationVariants.all { variant ->                variant.outputs.each { output ->                    def outputFile = output.outputFile                    if (outputFile != null && outputFile.name.endsWith('.apk')) {                        // 输出apk名称为AppName_v1.0_2017-02-09_wandoujia.apk                        def apkFile = "AppName_v${defaultConfig.versionName}_${buildTime()}" +                                "_${variant.productFlavors[0].name}.apk"                        output.outputFile = new File(outputFile.parent, apkFile)                    }                }            }        }        debug {            //设置debug版本的报名为<应用id>.debug            applicationIdSuffix ".debug"            //设置debug版本的app_name和application_id            manifestPlaceholders = [                    APP_NAME      : "@string/app_name_debug",                    APPLICATION_ID: "@string/application_id_debug"            ]            //设置debug版本包含x86的so文件            ndk {                abiFilters "armeabi", "armeabi-v7a", "x86"            }        }    }    packagingOptions {        exclude 'META-INF/LICENSE'        exclude 'META-INF/NOTICE'    }    dexOptions {        javaMaxHeapSize "2g"    }    //友盟多渠道打包    productFlavors {        wandoujia {}        baidu {}        _360 {}        _91 {}        anzhuomarket {}        huawei {}        xiaomi {}        lenovo {}        tencent {}        coolapk {}        meizu {}        vivo {}        ali {}        yulin {}    }    productFlavors.all { flavor ->        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]    }}dependencies {    testCompile "junit:junit:$rootProject.junitVersion"    compile "com.android.support:multidex:$rootProject.multidexVersion"    compile "com.google.code.gson:gson:$rootProject.gsonVersion"    //MaterialDesign    compile "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"    compile "com.android.support:design:$rootProject.supportLibraryVersion"    //CardView    compile "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"    //RecyclerView    compile "com.android.support:recyclerview-v7:$rootProject.supportLibraryVersion"    //butterKnife    compile "com.jakewharton:butterknife:$rootProject.butterknifeVersion"    apt "com.jakewharton:butterknife-compiler:$rootProject.butterknifeVersion"    ......}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131

转自:http://blog.csdn.net/lj402159806/article/details/54958056