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

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'
    ......
}

app目录下的build.gradle配置

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"
    ......
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值