Gradle的使用

最近研究Android Studio打多渠道包,现总结如下:

一.Gradle最新版本2.4,下载地址: https://gradle.org/downloads/

二.Gradle官方文档教程 http://tools.android.com/tech-docs/new-build-system/user-guide

三.Gradle的安装,

   1.解压下载的安装包;

   2.配置变量环境 GRADLE_HOME

   3.添加bin目录到path环境变量下;

   4.运行gradle -v 验证是否安装好;

apply plugin: 'android'

android {
    compileSdkVersion 21
    buildToolsVersion '21.1.2'

    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }

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

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... 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')
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    dependencies {
        compile fileTree(dir: 'libraries', include: '*.jar')
        compile project(':diy_common')
        compile project(':international_zh')
    }

    repositories {
        mavenCentral()
        maven {
            url "xxx"
        }
    }
    signingConfigs {
        debug {
            storeFile file("chenzhen.keystore")
            keyAlias 'chenzhen'
            keyPassword '162534'
            storePassword '162534'
        }

        release {
            storeFile file("chenzhen.keystore")
            storePassword "162534"
            keyAlias "chenzhen"
            keyPassword "162534"
        }
    }
    defaultConfig {
        testInstrumentationRunner "android.test.InstrumentationTestRunner"
        testHandleProfiling true
        testFunctionalTest true
        applicationId 'com.yy.only'
        // dex突破65535的限制
        multiDexEnabled true
        versionCode 340
        versionName '3.4'
        minSdkVersion 14
        targetSdkVersion 21
        manifestPlaceholders = [CHANNEL_NAME: "dev"]
        signingConfig signingConfigs.release
    }
    buildTypes {
        release {
            minifyEnabled true
            proguardFile 'proguard.cfg'
            debuggable true
            renderscriptDebuggable true
            zipAlignEnabled true
            proguardFile 'proguard.cfg'
            // 不显示Log
            buildConfigField "boolean", "LOG_DEBUG", "false"
            // 移除无用的resource文件
            shrinkResources true
            /*applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 输出apk名称为lock_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "lock_${defaultConfig.versionName}_${releaseTime()}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }*/
        }
        debug {
            debuggable true
            renderscriptDebuggable true
            minifyEnabled false
            zipAlignEnabled false
            // 显示Log
            buildConfigField "boolean", "LOG_DEBUG", "true"
        }
    }
    productFlavors {
        channel_360 {
            applicationId 'com.yy.only.pro'
            manifestPlaceholders = [CHANNEL_NAME: "360",AUTHRORITIES: "com.yy.only.pro"]
        }
        wangdoujia {
            applicationId 'com.yy.only.free'
            manifestPlaceholders = [CHANNEL_NAME: "wangdoujia", AUTHRORITIES : "com.yy.only.free"]
        }
    }

    //替换渠道信息
    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [CHANNEL_NAME: name, AUTHRORITIES:name]
    }

    //拷备so文件
    task copyNativeLibs(type: Copy) {
        // third party lib so
        from(new File(projectDir, 'libs')) { include '*/*.so' }
        into new File(buildDir, 'native-libs')
    }

    tasks.withType(JavaCompile) {
        compileTask ->
            //noinspection all
            compileTask.dependsOn copyNativeLibs
    }

    //noinspection all
    tasks.withType(com.android.build.gradle.tasks.PackageApplication) {
        pkgTask ->
            pkgTask.jniFolders = new HashSet<File>()
            pkgTask.jniFolders.add(new File(buildDir, 'native-libs'))
    }


}

 


运行gradle clean  

运行gradel build 即打包

或gradel assemble[release|debug|channel]buildle


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值