Test

#####Gradle 常用命令

./gradlew -v 版本号

./gradlew clean 清除项目(module)目录下的build文件夹

./gradlew build 检查依赖并编译打包

(以上命令需要到项目的根目录下执行,Windows系统上如果执行不了以上命令,用gradlew.bat 代替gradlew)

./gradlew assembleDebug 编译并打Debug包

./gradlew assembleRelease 编译并打Release的包

除此之外,assemble还可以和productFlavors结合使用,具体在下一篇多渠道打包进一步解释。

./gradlew installRelease Release模式打包并安装

./gradlew uninstallRelease 卸载Release模式包
apply plugin: 'android'
//添加依赖包
dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')//添加libs文件夹下的所有jar包
    compile project(':appcompat_v7') //这里添加其他依赖,可以是本地、远程的库,例如本地libraries/support_v7的库
}

//下面一段是将libs/*/*.so文件加入打包
//如果你的项目是使用Eclipse+ADT建立的,则需要这段代码
//task copyNativeLibs(type: Copy) {
//    from(new File('libs')) { include '**/*.so' }
  //  into new File(buildDir, 'native-libs')
//}

android {
//下面两行是编译sdk版本和buildTool的版本
    compileSdkVersion 21
    buildToolsVersion "21.1.0"

    //以下是签名信息
    signingConfigs {
        myConfigs {
            storeFile file("new.keystore")
            keyAlias "new.keystore"
            keyPassword "111111"
            storePassword "111111"
        }
    }

    buildTypes{
        release {
            minifyEnabled true//打包时过滤掉没有用到的代码
            shrinkResources true//打包时过滤掉没有用到的资源文件
            signingConfig signingConfigs.myConfigs
        }
    }
    //Gradle编译禁用Lint报错
    lintOptions {
        abortOnError false
    }

        /**
         * 渠道打包(不同包名)
         */
        productFlavors {
            a {
                applicationId = 'com.demo.yinyongbao'
                manifestPlaceholders = [installChanel:"应用宝"]
            }
            b {
                applicationId='com.demo.wandoujia'
                manifestPlaceholders = [installChanel: "豌豆荚"]
            }
        }
    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [installChanel: name]
    }
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src//... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

Manifest 文件里面写上(以友盟统计为例)

<meta-data android:name="InstallChannel" android:value="${installChanel}"/>

这样就可以进到项目的根目录,执行 gradlew clean 和gradlew build
如果上面的脚本可以被正常执行gradlew build 将会生成下面8个apk包 
Demo-a-debug.apk Demo-a-debug-unaligned.apk Demo-a-release.apk 
Demo-a-release-unaligned.apk Demo-b-debug.apk Demo-b-debug-unaligned.apk
Demo-b-release.apk Demo-b-release-unaligned.apk

我们也可以执行 gradlew assemBRelease(可简写gradlew assemBR) 将会生成两个apk包
Demo-b-release.apk  Demo-b-release-unaligned.apk

build.gradle 还有其他写法

apply plugin: 'com.android.application'

def releaseTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC"))
}

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    defaultConfig {
        applicationId "com.boohee.*"
        minSdkVersion 14
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"

        // dex突破65535的限制
        multiDexEnabled true
        // 默认是umeng的渠道
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }

    lintOptions {
        abortOnError false
    }

    signingConfigs {
        debug {
            // No debug config
        }

        release {
            storeFile file("../yourapp.keystore")
            storePassword "your password"
            keyAlias "your alias"
            keyPassword "your password"
        }
    }

    buildTypes {
        debug {
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"

            versionNameSuffix "-debug"
            minifyEnabled false
            zipAlignEnabled false
            shrinkResources false
            signingConfig signingConfigs.debug
        }

        release {
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"

            minifyEnabled true
            zipAlignEnabled true
            // 移除无用的resource文件
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为boohee_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "boohee_v${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

    // 友盟多渠道打包
    productFlavors {
        wandoujia {}
        _360 {}
        baidu {}
        xiaomi {}
        tencent {}
        taobao {}
        ...
    }

    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:21.0.3'
    compile 'com.jakewharton:butterknife:6.0.0'
    ...
}

Manifest 文件里配置

<meta-data android:name="UMENG_CHANNEL" android:value="${UMENG_CHANNEL_VALUE}" />