Android Studio常用的build.gradle 配置

//正式环境
def API_RELEASE_HOST = "\"xxxxxxx\""
//测试环境
def API_TEST_HOST = "\"xxxxxxx\""

//开发环境

def API_DEV_HOST = "\"xxxxxxxx\""

//获取当前时间
def getCurrentTime() {
    return new Date().format("yyyyMMddhhmmss", TimeZone.getTimeZone("UTC"))
}

//配置keystore签名  

signingConfigs {
    release {
        storeFile file("xxx.jks")
        storePassword "xxxx"
        keyAlias "xxxxx"
        keyPassword "xxxxx"
    }
    dev {
        storeFile file("xxxxx.jks")
        storePassword "xxxxx"
        keyAlias "xxxxx"
        keyPassword "xxxxxx"
    }
}
buildTypes {
    release {

        //id 根据你自己的需要设置是否可以同时在一个手机上运行多个自己的app。
        applicationIdSuffix ".release"
        //是否输出日志
        buildConfigField "boolean", "LOG_DEBUG", "false"
        //url动态替换,正式url
        buildConfigField "String", "API_HOST", "${API_RELEASE_HOST}"
        // 移除无用的resource文件
        shrinkResources true
        //是否混淆
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    dev {
        applicationIdSuffix ".dev"
        //是否输出日志
        buildConfigField "boolean", "LOG_DEBUG", "true"
        //url动态替换
        buildConfigField "String", "API_HOST", "${API_DEV_HOST}"//API Host
        // 移除无用的resource文件
        shrinkResources true
        //是否混淆
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

    }

多工程全局配置

先在project的根目录下的build.gradle定义ext全局变量:

ext {
    compileSdkVersion = 27
    buildToolsVersion = '27.0.3'
    minSdkVersion = 16
    targetSdkVersion = 27
    versionCode = 13
    versionName = "v1.13"
}
android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    defaultConfig {
        applicationId "com.xwan.inspect"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode rootProject.ext.versionCode
        versionName rootProject.ext.versionName
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [moduleName: project.getName()]
            }
        }


        buildConfigField("String", "API_HOST", "${API_DEV_HOST}")
        flavorDimensions "v1.13"


        ndk {
            //设置支持的SO库架构
            abiFilters 'armeabi' //, 'x86', 'armeabi-v7a', 'x86_64', 'arm64-v8a'
        }

    }

apk输出包重新命名

applicationVariants.all { variant ->
    variant.outputs.all { output ->
        outputFileName = new File("../../apk/${variant.buildType.name}", "demo-${variant.buildType.name}-${variant.versionName}-${variant.productFlavors[0].name}-${getCurrentTime()}.apk")
    }
}

猜你喜欢

转载自blog.csdn.net/wangwangli6/article/details/79804358