美文网首页
Android Studio3.5 gradle 配置自动打包、

Android Studio3.5 gradle 配置自动打包、

作者: 星邪Ara | 来源:发表于2019-08-28 11:27 被阅读0次

一、app.gradle配置

def keystoreFilepath = ''
def keystorePSW = ''
def keystoreAlias = ''
def keystoreAliasPSW = ''
// default keystore file, PLZ config file path in local.properties
def keyfile = file('s.keystore.temp')

Properties properties = new Properties()
// local.properties file in the root director
properties.load(project.rootProject.file('gradle.properties').newDataInputStream())
keystoreFilepath = properties.getProperty("keystore.path")

if (keystoreFilepath) {
    keystorePSW = properties.getProperty("keystore.password")
    keystoreAlias = properties.getProperty("keystore.alias")
    keystoreAliasPSW = properties.getProperty("keystore.alias_password")
    keyfile = file(keystoreFilepath)
}

android {
    signingConfigs {
        release {
            keyAlias keystoreAlias
            keyPassword keystoreAliasPSW
            storeFile keyfile
            storePassword keystorePSW
            v1SigningEnabled true
            v2SigningEnabled true
        }

    }

    //文件输出
    applicationVariants.all { variant ->
        variant.outputs.all { output ->
            //获取当前模式debug或release
            def buildType = variant.buildType.name
            //渠道名称
            def buildName = ""
            variant.productFlavors.each { product ->
                buildName = product.name
            }
            if (buildName == "rc" || buildName == "beta") {
                // apk生成根目录
                variant.getPackageApplicationProvider().get().outputDirectory = new File("../out_apk")
                //这里修改apk文件名,格式为 module_buildType_version_time.apk
                def currentTime = new Date().format("YYYYMMddHHmmss", TimeZone.getTimeZone("GMT+08:00"))
                outputFileName = "xkwc_${buildType}_${buildName}_${defaultConfig.versionName}_${currentTime}.apk"
            }
        }
    }

    productFlavors {
        //正式
        rc {
            /*** 正式API */
            buildConfigField 'String', 'BASE_URL', '"https://xxxx"'
        }
        //测试
        beta {
            /*** API测试 */
            buildConfigField 'String', 'BASE_URL', '"http://xxxx"'
        }
        //开发
        alpha {
            /*** API测试 */
            buildConfigField 'String', 'BASE_URL', '"http://xxxx"'
        }
    }

defaultConfig {
        resValue "string", "app_name", "xxxx"
    }

buildTypes {
        release {
            minifyEnabled true//是否开启混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release//签名文件
            /*** 正式API */
            buildConfigField 'String', 'BASE_URL', '"https://xxxx"'
            resValue "string", "app_name", "xxxx"
        }

        debug {
            minifyEnabled false//是否开启混淆
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release//签名文件
            if (DEBUG_KEY == 'rc') {
                /*** 正式API */
                buildConfigField 'String', 'BASE_URL', '"xxxx"'
                resValue "string", "app_name", "xxxx"
            } else if (DEBUG_KEY == 'beta') {
                /*** API测试 */
                buildConfigField 'String', 'BASE_URL', '"http://xxxx"'
                resValue "string", "app_name", "xxxx测试版"
            } else if (DEBUG_KEY == 'alpha') {
                /*** API测试 */
                buildConfigField 'String', 'BASE_URL', '"http://xxxx"'
                resValue "string", "app_name", "xxxx开发版"
            }
        }
    }
}

二、gradle.properties 配置

DEBUG_KEY = alpha
#DEBUG_KEY = rc
keystore.path=../xxxx.jks
keystore.password=xxxx
keystore.alias=xxxx
keystore.alias_password=xxxx

三、BASE_URL使用

// 在app module下调用BuildConfig.BASE_URL就可以获取配置好的数据
buildConfigField 'String', 'BASE_URL', '"http://xxxx"'

四、Android Studio3.5,点击运行或调试按钮问题

因为在app.gradle配置了productFlavors (打包渠道),点击运行或调试按钮就会找不到相应的apk,所以我在上面加上了一些处理。


image.png

只有rc和bate才输出到指定目录,alpha不输出到指定目录。到这里我们就要在Android Studio里面配置一下Build Variants,选择app module里面的alpha渠道就可了。

image.png
image.png

五、完事!

相关文章

网友评论

      本文标题:Android Studio3.5 gradle 配置自动打包、

      本文链接:https://www.haomeiwen.com/subject/lpkpectx.html