Android Gradle的详细示例教程

Gradle相关博文:
1. Groovy脚本基础全攻略:http://blog.csdn.net/yanbober/article/details/49047515
2. Gradle脚本基础全攻略:http://blog.csdn.net/yanbober/article/details/49314255

Module的gradle配置文件

// 声明是Android程序
apply plugin: 'com.android.application'
//预定义
def buildTime() {

    def date = new Date()

    def formattedDate = date.format('yyyyMMdd')
    return formattedDate
}
def TYPE_STRING = "String"
def CONFIG_FILE = "CONFIG_FILE"
def SDK_FILE = "SDK_FILE"
def LOCALE = "LOCALE"

android {
    // 编译SDK的版本
    compileSdkVersion 21// build tools的版本
    buildToolsVersion "21.1.1"

    defaultConfig {
        // 应用的包名
        applicationId "me.storm.ninegag"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0.0"
    }

    // java版本
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    signingConfigs {
        //签名信息
        debug {

            storeFile file("../android/debug.keystore")
            storePassword "android"
            keyAlias "androiddebugkey"
            keyPassword "android"
        }
    }    
    dexOptions {
            //解决导入包过多产生的函数过多编译不通过问题
        javaMaxHeapSize "1g"
    }
    packagingOptions { // 打包配置
        exclude 'META-INF/LICENSE' // 排除一些文件
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    buildTypes {
        debug{
            debuggable false //调试信息是否开启调试模式
            // 是否进行混淆
            minifyEnabled false
            signingConfig signingConfigs.debug    //签名文件debug
        }
        release {
            debuggable false //调试信息是否开启调试模式
            // 是否进行混淆
            minifyEnabled false// 混淆文件的位置
            zipAlignEnabled true    //是否zip对齐

            shrinkResources true // 移除无用的resource文件            
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            resValue("string", "website_host_name", "${production_website_host_name}")        //R.String.website_host_name
            //给生成的apk重新命名
            applicationVariants.all {

                variant ->
                    variant.outputs.each { output ->
                        if (output.outputFile != null && output.outputFile.name.endsWith('.apk')
                                && 'release'.equals(variant.buildType.name)) {
                            def apkFile = new File(
                                    output.outputFile.getParent(),
                                    "mtp_v${variant.versionName}_${buildTime()}_${variant.productFlavors[0].name}.apk")
                            output.outputFile = apkFile
                        }
                    }
            }        }
    }
    //多渠道打包
    productFlavors {
        focusteach{}
        productFlavors.all { flavor ->
            flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
    }
    //jni目录
    sourceSets {
        main {
            jniLibs.srcDirs = ['libs']
        }
    }

// 移除lint检查的error
    lintOptions {
      abortOnError false
    }
}

dependencies {
    // 编译libs目录下的所有jar包
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.0.2'
    compile 'com.etsy.android.grid:library:1.0.5'
    compile 'com.alexvasilkov:foldable-layout:1.0.1'// 编译extras目录下的ShimmerAndroid模块
    compile project(':extras:ShimmerAndroid')
    // 仅在debug包启用leakcanary进行内存监控和提示

    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.4-beta1'
    alphaCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.4-beta1'}    //发布版用空包编译
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值