抛砖引玉系列之gradle文件配置差异化打包

这篇博客介绍了如何通过Gradle配置,在项目打包时根据不同的环境(如local和online)设置不同的服务器地址,避免为每个环境单独打包。通过定义productFlavors和buildConfigField,可以在编译时动态插入不同的服务器URL,简化打包流程。
摘要由CSDN通过智能技术生成

在项目开发的过程中,总能碰到这样的需求,打包的时候发布两个相同版本的包,但是服务器地址不同,这时候就不想打两个包的时候只想运行一句打包命令打出两个服务器地址的包,这个时候就想到可以根据gradle配置打出差异化版本的包。

需求如上,gradle代码配置如下:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 27
    buildToolsVersion '28.0.3'


    defaultConfig {
        applicationId "com.haocang.mangomax"
        minSdkVersion 21 
        targetSdkVersion 26
        versionCode 10042
        versionName "2.0.0.0042E"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        flavorDimensions "default"
        //Enabling multidex support.
        multiDexEnabled true

        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath = true
                arguments = [moduleName: project.getName()]
            }
        }
        configurations.all {
            exclude group: 'com.android.support', module: 'support-v13'
        }
        multiDexEnabled true

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    splits {
        abi {
            enable true
            reset()
            include 'armeabi-v7a'
            universalApk false
        }
    }

    dexOptions {
        preDexLibraries false
        javaMaxHeapSize "3072m"
    }

    signingConfigs {
        release {
            keyAlias 'commonsign'
            keyPassword 'haocangapp'
            storeFile file('./commonSign.jks')
//            storeFile file('D:/AndroidStudio/Mango/commonSign.jks')
            storePassword 'haocangapp'
        }
        loadSigningConfigs() //加载签名信息
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.release
            buildConfigField("String", "Address", "\"http://122.114.164.162:16801/\"")
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
    }
    productFlavors {

        local {
            buildConfigField("String", "Address", "\"http://192.168.8.246/\"")
        }

        online {
            buildConfigField("String", "Address", "\"http://122.114.164.162:16801/\"")
        }

    }
    applicationVariants.all { variant ->
        variant.outputs.all {
            if (!variant.buildType.isDebuggable()) {
                variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/apk")
            }
            outputFileName = "taihe_APP_${defaultConfig.versionName}_${releaseTime()}_${variant.buildType.name}_${variant.productFlavors[0].name}.apk"
        }
    }

}

def releaseTime() {
    return new Date().format("yyyyMMdd", TimeZone.getTimeZone("GMT+08:00"))
}
def loadSigningConfigs() {
    def Properties props = new Properties()
    def propFile = file('../signing.properties') //加载properties文件
    if (propFile.canRead()) {
        props.load(new FileInputStream(propFile))
        if (props != null && props.containsKey('RELEASE_STORE_FILE') && props.containsKey('RELEASE_STORE_PASSWORD') &&
                props.containsKey('RELEASE_KEY_ALIAS') && props.containsKey('RELEASE_KEY_PASSWORD')) {
            android.signingConfigs.release.storeFile = file(props['RELEASE_STORE_FILE'])
            android.signingConfigs.release.storePassword = props['RELEASE_STORE_PASSWORD']
            android.signingConfigs.release.keyAlias = props['RELEASE_KEY_ALIAS']
            android.signingConfigs.release.keyPassword = props['RELEASE_KEY_PASSWORD']
        } else {
            android.buildTypes.release.signingConfig = null
        }
    } else {
        android.buildTypes.release.signingConfig = null
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //    implementation project(':equiment')
    //    implementation project(path: ':mango-lib')
    //    implementation project(':messge')
    //    implementation project(':monitor')
    //    implementation project(':patrol')
    //    implementation project(':repair')
    //    implementation project(':fault')
    //    implementation project(':curve')
    //    implementation project(':maintain')
    implementation('com.alibaba:arouter-api:1.3.1') {
        exclude group: 'com.android.support', module: 'support-v4'
    }
    annotationProcessor 'com.alibaba:arouter-compiler:1.1.4'
    api project(':water-link')
    api project(':hc-lib')
}

关键代码:

flavorDimensions "default"
productFlavors {

    local {
        buildConfigField("String", "Address", "\"http://192.168.8.246/\"")
    }

    online {
        buildConfigField("String", "Address", "\"http://122.114.164.162:16801/\"")
    }

}
applicationVariants.all { variant ->
    variant.outputs.all {
        if (!variant.buildType.isDebuggable()) {
            variant.getPackageApplication().outputDirectory = new File(project.rootDir.absolutePath + "/apk")
        }
        outputFileName = "taihe_APP_${defaultConfig.versionName}_${releaseTime()}_${variant.buildType.name}_${variant.productFlavors[0].name}.apk"
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值