美文网首页
Android开发 Gradle配置多渠道多环境

Android开发 Gradle配置多渠道多环境

作者: 吃苹果的猫N | 来源:发表于2017-10-19 11:26 被阅读0次

    1.配置多环境

    1.1 配置build.gradle

    在新建立项目后,app目录下的build.gradle中,会默认有buildTypes属性,就像这样。

    默认buildTypes

    buildTypes:编译类型,默认的就是debugrelease

    虽然上图中只有一个release属性,但是debug其实也是默认存在的,关于这点,你可以打开Android studioBuild Variants(默认位置在Android studio的左下角靠左边栏处)自行查看,如下图。

    Build Variants :构建变种版本

    我们要做的就是其实就是对这个buildTypes 进行扩充和重写

    以博主公司为例,分teststagepro 三个环境,每个对应不同的baseURLappName

     buildTypes {
            //pro环境
            release {
                buildConfigField("String", "BASE_URL", "\"http://pro.cn\"")
            }
    
            //stage环境
            stage {
                buildConfigField("String", "BASE_URL", "\"http://stage.cn\"")
            }
    
            //test环境
            debug {
                buildConfigField("String", "BASE_URL", "\"http://test.cn\"")
            }
        }
    

    buildConfigField : 配置宏,可以在gradle里配置一个值让java 代码中访问到

    buildTypes默认就有debugrelease两种类型
    debugrelease分别重写为testpro环境,再另外多加一个stage,这样就可以构成三个环境。

    1.2 项目内调用

    在项目build完后,会生成一个BuildConfig文件。

    这里写图片描述
    你就可以直接在自己项目里调用BuildConfig
    这里写图片描述

    如果你要切换环境,就又回到了上面提到的Build Variants,此时你再打开,它就变成了下面这样。


    可以任意切换自己想要的环境。

    2.配置多渠道

    2.1 配置build.gradle

    多渠道配置需要手动添加另一个属性productFlavors

        productFlavors {
            "other"{} //别的渠道及本地调试
            "baidu" {} //百度
            "weixin" {} //微信
            "toutiao" {} //今日头条
            "sougou" {} //搜狗
        }
    
        productFlavors.all {
            flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
    

    productFlavors:从项目中构建了一个应用的自定义版本
    manifestPlaceholders:用于替换 AndroidManifest清单文件的指定位置数据

    2.2 配置 AndroidManifest.xml

    <meta-data
         android:name="UMENG_CHANNEL"
         android:value="${UMENG_CHANNEL_VALUE}" />
    

    这样在build的时候,就可以把manifestPlaceholders 指定的[UMENG_CHANNEL_VALUE: name] 键值对映射到AndroidManifest.xml${UMENG_CHANNEL_VALUE}


    这个时候再查看Build Variants就已经变成了这个样子,在这里选择自己想要的环境及渠道。

    至此,大功告成,在打包apk的时候就可以选择自己想要的环境和渠道。



    build.gradle

    
    android {
        compileSdkVersion 26
        buildToolsVersion "26.0.1"
        defaultConfig {
            applicationId "com.gradlesimple.jyn"
            minSdkVersion 15
            targetSdkVersion 26
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                buildConfigField("String", "BASE_URL", "\"http://pro.cn\"")
                manifestPlaceholders = [
                        app_name: "proAPP"
                ]
            }
    
            //stage环境
            stage {
                buildConfigField("String", "BASE_URL", "\"http://stage.cn\"")
                manifestPlaceholders = [
                        app_name: "stageAPP"
                ]
            }
    
            //test环境
            debug {
                buildConfigField("String", "BASE_URL", "\"http://test.cn\"")
                manifestPlaceholders = [
                        app_name: "testAPP"
                ]
            }
        }
    
        productFlavors {
            "other"{} //别的渠道及本地调试
            "baidu" {} //百度
            "weixin" {} //微信
            "toutiao" {} //今日头条
            "sougou" {} //搜狗
        }
    
        productFlavors.all {
            flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
        }
    }
    

    AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.gradlesimple.jyn">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="${app_name}">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <meta-data
                android:name="UMENG_CHANNEL"
                android:value="${UMENG_CHANNEL_VALUE}" />
        </application>
    </manifest>
    

    相关文章

      网友评论

          本文标题:Android开发 Gradle配置多渠道多环境

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