美文网首页
Android应用同时发布 dev、test、release版本

Android应用同时发布 dev、test、release版本

作者: realchen7 | 来源:发表于2020-02-18 09:49 被阅读0次

背景:

需要同时发布 测试版 和 正式版,且都需可以自动更新

测试版(内部测试部使用)
真实版(客户使用)

当然还会同时存在一个开发版,app端开发 接口端开发使用

相信很多人跟我一样,大部分情况下都是之前使用的 AS 自带的打包功能


image.png

解决方案

如何来实现上文提到的需求呢,废话不多说,直接上代码吧,找到app下的build.gradle文件

在 android 目录下

signingConfigs {
        appDev {
            storeFile file(MYAPP_DEV_STORE_FILE)
            storePassword MYAPP_DEV_STORE_PASSWORD
            keyAlias MYAPP_DEV_KEY_ALIAS
            keyPassword MYAPP_DEV_KEY_PASSWORD
        }
        appTest {
            storeFile file(MYAPP_TEST_STORE_FILE)
            storePassword MYAPP_TEST_STORE_PASSWORD
            keyAlias MYAPP_TEST_KEY_ALIAS
            keyPassword MYAPP_TEST_KEY_PASSWORD
        }
        appRelease {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }
//我们把key相关信息配置在 gradle.properties

    buildTypes {
        //开发环境
        appDev {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.appDev
            manifestPlaceholders = [
                    app_label  : "@string/app_name_dev",
                    versionCode: "10000",
                    versionName: "V0.1"
            ]
            applicationIdSuffix = '.dev'
            buildConfigField "String", "IP_MES_BASE", "\"http://xxx:8081/\""
            buildConfigField "String", "URL_MES_BASE", "\"http://xxx:8081/app/\""
            debuggable = true
        }
        //测试环境
        appTest {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.appTest
            manifestPlaceholders = [
                    app_label  : "@string/app_name_test",
                    versionCode: "2",
                    versionName: "V0.2"
            ]
            applicationIdSuffix = '.test'
            buildConfigField "String", "IP_MES_BASE", "\"http://xxx:8081/\""
            buildConfigField "String", "URL_MES_BASE", "\"http://xxxx:8081/app/\""
        }
        //正式环境
        appRelease {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.appRelease
            manifestPlaceholders = [
                    app_label  : "@string/app_name_release",
                    versionCode: "12",
                    versionName: "V0.12"
            ]
            buildConfigField "String", "IP_MES_BASE", "\"http://xxx/\""
            buildConfigField "String", "URL_MES_BASE", "\"http://xxx/app/\""
        }
    }

    //去掉默认的debug及release两个buildType
    variantFilter { variant ->
        if (variant.buildType.name.equals('release') || variant.buildType.name.equals('debug')) {
            variant.setIgnore(true)
        }
    }

    applicationVariants.all { variant ->
        variant.outputs.all { output ->

            if (variant.buildType.name.equals('appDev')) {
                outputFileName = "xxx_" +
                        "DEV" +
                        ".apk"
            } else if (variant.buildType.name.equals('appTest')) {
                outputFileName = "xxx_" +
                        "TEST" +
                        ".apk"
            } else if (variant.buildType.name.equals('appRelease')) {
                outputFileName = "xxx" + ".apk"
            }
        }
    }

gradle.properties

MYAPP_DEV_STORE_FILE=xxx_key_test.keystore
MYAPP_DEV_KEY_ALIAS=xxx_key_test
MYAPP_DEV_STORE_PASSWORD=xxx123
MYAPP_DEV_KEY_PASSWORD=xxx123
MYAPP_TEST_STORE_FILE=xxx_key_test.keystore
MYAPP_TEST_KEY_ALIAS=xxx_key_test
MYAPP_TEST_STORE_PASSWORD=xxx123
MYAPP_TEST_KEY_PASSWORD=xxx123
MYAPP_RELEASE_STORE_FILE=xxx_key_app.jks
MYAPP_RELEASE_KEY_ALIAS=xxx_key_app
MYAPP_RELEASE_STORE_PASSWORD=xxx123
MYAPP_RELEASE_KEY_PASSWORD=xxx123

在manifest文件中,进行一些动态的配置

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.xxx.xxx"
    android:versionCode="${versionCode}"
    android:versionName="${versionName}">

动态显示APP名称,这边对于图标没有需求,使用的都是同一套logo,如果有需要,这边icon也可以做成配置的

 <application
        android:name=".application.MyApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${app_label}"
        android:networkSecurityConfig="@xml/network_security_config"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning"
        tools:replace="android:label">

打包

执行命令 gradlew assemble,执行完毕后会在 app\build\outputs\apk 里产生 三个apk文件

在执行打包命令前,最好先执行下 gradlew clean 命令

相关文章

网友评论

      本文标题:Android应用同时发布 dev、test、release版本

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