较完整的配置AS项目的build.gradle文件

apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife'
//java8 支持
apply plugin: 'me.tatarka.retrolambda'

apply plugin: 'android-apt'

//定义打包时间函数,由于中国所在时区与UTC相差1个时区,即八个小时因此这里要+8
def packageTime() {
    return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("GMT+8"))
}
android {
    signingConfigs {
        release {
            keyAlias project.KS_ALIAS
            keyPassword project.KS_PASSWORD
            storeFile file(KS_FILE_PATH)
            storePassword project.KS_STORE_PASSWORD
        }
        debug{
            keyAlias project.KS_ALIAS
            keyPassword project.KS_PASSWORD
            storeFile file(KS_FILE_PATH)
            storePassword project.KS_STORE_PASSWORD
        }
    }
    compileSdkVersion rootProject.ext.android.compileSdkVersion
    buildToolsVersion rootProject.ext.android.buildToolsVersion
    aaptOptions{
        cruncherEnabled = false
        useNewCruncher = false
    }
    dexOptions {
        javaMaxHeapSize "4g"
    }
    //程序在buid的时候,会执行lint检查,有任何的错误或者警告提示,都会终止构建,我们可以将其关掉
    lintOptions {
        abortOnError false
    }
    defaultConfig {
        applicationId rootProject.ext.android.applicationId
        minSdkVersion rootProject.ext.android.minSdkVersion
        targetSdkVersion rootProject.ext.android.targetSdkVersion
        versionCode rootProject.ext.android.versionCode
        versionName rootProject.ext.android.versionName
        multiDexEnabled true
        // 默认是umeng的渠道
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
        ndk {
            //设置支持的SO库架构
            //注:放开'arm64-v8a'会找不到腾讯人脸识别的类库包,>=android7.0 手机会崩溃
            abiFilters  'armeabi-v7a', 'x86','arm64-v8a'//'armeabi-v7a', 'x86','armeabi'','x86_64','arm64-v8a'
        }
    }
    buildTypes {
        release {
            //是否启用混淆
            minifyEnabled true
            //是否清理无用资源
            shrinkResources true
            //是否启用zipAlign压缩
            zipAlignEnabled true
            buildConfigField "boolean", "API_ENV", "true"     //正式环境
            buildConfigField "boolean", "LOG_DEBUG", "false"  //不打印log
            buildConfigField "String", "HOST", "\"https://api.xxx.com/\""//正式环境地址
            buildConfigField "String", "PAGE","\"https://admin.xxx.com/\""//全部页面(h5)使用这个
            buildConfigField "boolean", "OPEN_UMENG_DEBUG","false"  //不开启友盟推送和分享的debug模式
            signingConfig signingConfigs.release   //签名
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            shrinkResources false
            zipAlignEnabled false
            // 如果没有提供混淆规则文件,则设置默认的混淆规则文件(SDK/tools/proguard/proguard-android.txt)
            pseudoLocalesEnabled true
            minifyEnabled false
            buildConfigField "boolean", "API_ENV", "false"     //测试环境false 正式环境true
            buildConfigField "boolean", "LOG_DEBUG", "true"  //打印log
            buildConfigField "String", "HOST", "\"https://apitest.xxx.com/\""//测试环境地址
            buildConfigField "String", "PAGE","\"https://admintest.xxx.com/\"" //全部页面(h5)使用这个
            buildConfigField "boolean", "OPEN_UMENG_DEBUG","true"  //开启友盟推送和分享的debug模式

            signingConfig signingConfigs.debug
        }
    }
    //修改生成的最终文件名
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
                File outputDirectory = new File(outputFile.parent);
                def fileName
                if (variant.buildType.name == "release") {
                    // 输出apk名称为wallet_v1.0.0_2015-06-15-102830_xiaomi.apk
                    fileName = "hrmp_v${defaultConfig.versionName}_${packageTime()}_${variant.productFlavors[0].name}.apk"
                } else {
                    fileName = "hrmp_v${defaultConfig.versionName}_${packageTime()}_beta.apk"
                }
                output.outputFile = new File(outputDirectory, fileName)
            }
        }
    }
    // 友盟多渠道打包
    productFlavors {
        _360 {}
        xiaomi {}
        tencent {}
    }

    productFlavors.all { flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
    sourceSets {
        main {
            jniLibs.srcDir(['libs'])
        }
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
    compileOptions {
        targetCompatibility JavaVersion.VERSION_1_8
        sourceCompatibility JavaVersion.VERSION_1_8
    }
    repositories {
        flatDir {
            dirs 'libs' //this way we can find the .aar file in libs folder
        }
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

repositories {
    jcenter()
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    //0. appcompat-v7
    compile rootProject.ext.dependencies["appcompat-v7"]
    //1. 云刷脸SDK
//    compile(name: 'WbCloudFaceVerifySdk-proRelease-v1.3.89-c596764', ext: 'aar')
    //2.云公共组件
//    compile(name: 'WbCloudNormal-release-eb447e8', ext: 'aar')
    // 3. 依赖的第三方jar包

    //网络请求json解析

    //网络请求

    //4. 云Ocr SDK
//    compile(name: 'WbCloudOcrSdk-proRelease-v0.1.0-6900374', ext: 'aar')
    compile rootProject.ext.dependencies["support-v13"]
    compile rootProject.ext.dependencies["support-v4"]
    compile rootProject.ext.dependencies["design"]
    compile rootProject.ext.dependencies["eventbus"]
    compile rootProject.ext.dependencies["retrofit"]
    compile rootProject.ext.dependencies["converter-gson"]
    compile rootProject.ext.dependencies["adapter-rxjava2"]
    compile rootProject.ext.dependencies["logging-interceptor"]
    compile rootProject.ext.dependencies["rxandroid"]
    compile rootProject.ext.dependencies["rxjava"]
    compile rootProject.ext.dependencies["permissiongen"]
    compile rootProject.ext.dependencies["crashreport_upgrade"]
    compile rootProject.ext.dependencies["nativecrashreport"]
    compile rootProject.ext.dependencies["multidex"]
    compile rootProject.ext.dependencies["gson"]
    compile rootProject.ext.dependencies["okhttp-urlconnection"]
    compile rootProject.ext.dependencies["butterknife"]
    apt rootProject.ext.dependencies["butterknife-compiler"]
    compile rootProject.ext.dependencies["permissionsdispatcher"]
    apt rootProject.ext.dependencies["permissionsdispatcher-processor"]
    compile rootProject.ext.dependencies["glide-transformations"]
    compile rootProject.ext.dependencies["ptr-load-more"]
    compile rootProject.ext.dependencies["patternlock"]
    compile rootProject.ext.dependencies["analytics"]

    //LeakCanary
    debugCompile 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
    testCompile 'junit:junit:4.12'
    //    compile files('libs/android-support-multidex.jar')

    //    exclude group:'multidex'
    compile 'junit:junit:4.12'
    compile files('libs/SocialSDK_QQ_Simplify.jar')
    compile files('libs/SocialSDK_WeChat_Simplify.jar')
    compile files('libs/umeng_shareboard_widget.jar')
    compile files('libs/umeng_social_api.jar')
    compile files('libs/umeng_social_net.jar')
    compile files('libs/umeng_social_shareboard.jar')
    compile files('libs/umeng_social_tool.jar')
    compile project(':IDCardLib')
    compile project(':livenesslib')
    compile project(':PushSDK')
}

其中配置签名文件,即 signingConfigs:在app项目的根目录下新建一个文件,取名 gradle.properties,设置内容如下:

KS_PASSWORD=xxx
KS_ALIAS=xx
KS_STORE_PASSWORD=xxxx                
KS_FILE_PATH=D:/WorkSpace/Git/xxx.jks   签名文件

依赖的类库,即dependencies下面的rootProject.ext.dependencies引用配置:在project的根目录下创建一个文件,取名config.gradle,设置内容如下:

ext {

    android = [
            compileSdkVersion: 25,
            buildToolsVersion: "25.0.3",
            applicationId    : "xxx",
            minSdkVersion    : 14,
            targetSdkVersion : 25,
            versionCode      : 210,
            versionName      : "2.1.0"
    ]

    dependencies = [
            //其中latest.release指代最新版本号,也可以指定明确的版本号,例如1.2.0
            "support-v13"               : 'com.android.support:support-v13:25.3.1',
            "support-v4"                : 'com.android.support:support-v4:25.1.1',
            "appcompat-v7"              : 'com.android.support:appcompat-v7:25.3.1',
            "design"                    : 'com.android.support:design:25.3.1',
            "eventbus"                  : 'org.greenrobot:eventbus:3.0.0',
            "retrofit"                  : 'com.squareup.retrofit2:retrofit:2.3.0',
            "converter-gson"            : 'com.squareup.retrofit2:converter-gson:2.3.0',
            "adapter-rxjava2"           : 'com.squareup.retrofit2:adapter-rxjava2:2.3.0',
            "logging-interceptor"       : 'com.squareup.okhttp3:logging-interceptor:3.7.0',
            "rxandroid"                 : 'io.reactivex.rxjava2:rxandroid:2.0.1',
            "rxjava"                    : 'io.reactivex.rxjava2:rxjava:2.1.0',
            "permissiongen"             : 'com.lovedise:permissiongen:0.0.6',
            "crashreport_upgrade"       : 'com.tencent.bugly:crashreport_upgrade:latest.release',
            "nativecrashreport"         : 'com.tencent.bugly:nativecrashreport:latest.release',
            "multidex"                  : 'com.android.support:multidex:1.0.1',
            "gson"                      : 'com.google.code.gson:gson:2.3.1',
            "okhttp-urlconnection"      : 'com.squareup.okhttp:okhttp-urlconnection:2.4.0',
            "butterknife"               : 'com.jakewharton:butterknife:8.6.0',
            "butterknife-compiler"      : 'com.jakewharton:butterknife-compiler:8.6.0',
            "permissionsdispatcher"     : 'com.github.hotchemi:permissionsdispatcher:2.4.0',
            "permissionsdispatcher-processor" : 'com.github.hotchemi:permissionsdispatcher-processor:2.4.0',
            "glide-transformations"     : 'jp.wasabeef:glide-transformations:2.0.2',
            "ptr-load-more"             : 'in.srain.cube:ptr-load-more:1.0.6',
            "patternlock"               : 'me.zhanghai.android.patternlock:library:2.1.2',
            "analytics"                 : 'com.umeng.analytics:analytics:latest.integration'
    ]
}

最后在project根目录下的build.gralde最上面添加 apply from:  "config.gradle",如图:

较完整的配置AS项目的build.gradle文件_第1张图片


你可能感兴趣的:(Gradle,Android,Studio)