关于productFlavors的一些使用方法

在AndroidStudio中可以使用productFlavors配置完成一些工作,比如对不同的环境配置不同的包名

使用不同的applicationId,不同的app名称及图标等...

使用方法示例 在app目录下build.gradle文件中配置

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        minSdk MIN_SDK_VERSION as Integer
        targetSdk TARGET_SDK_VERSION as Integer
        versionName VERSION_NAME
        versionCode VERSION_CODE as Integer

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        //必须要保证所有的flavor 都属于同一个维度
        flavorDimensions "versionCode"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }


    productFlavors {
        develop {
            // 每个环境包名不同
            applicationId DEVELOP_PACKAGE_NAME as String
            dimension "versionCode"
        }
        prev {
            // 每个环境包名不同
            applicationId PREV_PACKAGE_NAME as String
            dimension "versionCode"
        }
        prod {
            //每个环境包名不同
            applicationId PROD_PACKAGE_NAME as String
            dimension "versionCode"
        }

        productFlavors.all { flavor ->
            flavor.manifestPlaceholders += [CHANNEL_VALUE: name]
            if (name.equals('develop')) { //开发&测试环境
                flavor.manifestPlaceholders += [APP_NAME: DEVELOP_APP_NAME as String]
                flavor.manifestPlaceholders += [APP_ICON: DEVELOP_APP_ICON as String]
                flavor.manifestPlaceholders += ["apk.applicationId" : DEVELOP_PACKAGE_NAME as String]
//                flavor.manifestPlaceholders += [BAIDU_MAP_APP_KEY: DEVELOP_BAIDU_MAP_APP_KEY as String]
                
            } else if (name.equals('prev')) { //预发布环境
                flavor.manifestPlaceholders += [APP_NAME: PREV_APP_NAME as String]
                flavor.manifestPlaceholders += [APP_ICON: PREV_APP_ICON as String]
                flavor.manifestPlaceholders += ["apk.applicationId" : PREV_PACKAGE_NAME as String]
//                flavor.manifestPlaceholders += [BAIDU_MAP_APP_KEY: PREV_BAIDU_MAP_APP_KEY as String] //百度地图
                
            } else if (name.equals('prod')) { //生产环境
                flavor.manifestPlaceholders += [APP_NAME: PROD_APP_NAME as String]
                flavor.manifestPlaceholders += [APP_ICON: PROD_APP_ICON as String]
                flavor.manifestPlaceholders += ["apk.applicationId" : PROD_PACKAGE_NAME as String]
//                flavor.manifestPlaceholders += [BAIDU_MAP_APP_KEY: PROD_BAIDU_MAP_APP_KEY as String] //百度地图
                
            }


            //打包配置
            applicationVariants.all { variant ->
                def name = variant.flavorName
                variant.outputs.all {
                    if (name.equals('develop')){
                        outputFileName = "productFlavors-debug-${variant.versionName}.apk"
                    }else if (name.equals('prev')){
                        outputFileName = "productFlavors-prev-${variant.versionName}.apk"
                    }else if (name.equals('prod')) {
                        outputFileName = "productFlavors-prod-${variant.versionName}.apk"
                    }else{
                        outputFileName = "productFlavors-${variant.versionName}.apk"
                    }
                }
            }
        }

    }

}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

然后在gradle.properties文件中配置

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true


VERSION_NAME=1.0.1
VERSION_CODE=102
MIN_SDK_VERSION=21
TARGET_SDK_VERSION=32

##########开发&测试环境#########
#包名
DEVELOP_PACKAGE_NAME=com.test.settingchangedemo.test
#图标
#发布到应用商店图标
DEVELOP_APP_ICON=@mipmap/ic_caomei
#app名称
DEVELOP_APP_NAME=develop_setting
##百度地图APP_KEY
#DEVELOP_BAIDU_MAP_APP_KEY=axsdfdsfsdfsdfsfsd



#########灰度&预发布环境########
#包名
PREV_PACKAGE_NAME=com.test.settingchangedemo.prev
#图标
#发布到应用商店图标
PREV_APP_ICON=@mipmap/ic_chengzi
#app名称
PREV_APP_NAME=prev_setting

##百度地图APP_KEY
#PREV_BAIDU_MAP_APP_KEY=dsgfgfdgfdgdfgfdfg


#########生产环境#############
#包名
PROD_PACKAGE_NAME=com.test.settingchangedemo.prod
#图标
PROD_APP_ICON=@mipmap/ic_putao
#app名称
PROD_APP_NAME=prod_setting
##百度地图APP_KEY
#PROD_BAIDU_MAP_APP_KEY=sdfdsfsfdsfdsdfdsfsdfgdfg

再然后在清单文件中配置

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.settingchangedemo">

    <application
        android:allowBackup="true"
        android:label="${APP_NAME}"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:icon="${APP_ICON}"
        android:theme="@style/Theme.SettingChangeDemo">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

当然 如果使用此配置方式的话  清单文件中需要使用到applicationId的地方 也需要做出相应调整

比如

<provider
            android:name="io.dcloud.common.util.DCloud_FileProvider"
            android:authorities="${applicationId}.dc.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/dcloud_file_provider" />
        </provider>

下面附上效果图 不同的包名 不同的图标  不同的app名称   这里  如果app名称是汉字则会乱码

所以用的英文

 

 

 打包输出图

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值