Android Studio 3.+ 使用gradle配置动态修改包名、图标、常量等,实现动态配置编译不同包名的APP和多渠道打包

Android Studio 3.1.3  + gradle 4.4 + build:gradle:3.1.3

示例代码已上传github https://github.com/weixx/gradle-package

 

配置差别化渠道包

app/build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

def releaseTime() {
    return new Date().format("yyyyMMdd", TimeZone.getTimeZone("UTC"))
}

android {
    // 签名文件
    signingConfigs {
        config {
            keyAlias '111111'
            keyPassword '111111'
            storeFile file('C:\\Users\\Administrator\\Desktop\\11.jks')
            storePassword '111111'
        }
    }


    compileSdkVersion 28
    defaultConfig {
//        applicationId "com.xx"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        signingConfig signingConfigs.config
    }

    flavorDimensions "version"
    // 多渠道/多环境 的不同配置
    productFlavors {
        dev {
            // 每个环境包名不同
            applicationId "com.xx.dev"
            // 动态添加 string.xml 字段;
            // 注意,这里是添加,在 string.xml 不能有这个字段,会重名!!!
            resValue "string", "s1", "dev"
            // 动态修改 常量 字段
            buildConfigField "String", "ENVIRONMENT", '"dev"'
            // 修改 AndroidManifest.xml 里的变量
            manifestPlaceholders = [UMENG_CHANNEL_VALUE:"dev",
                                    app_icon:"@mipmap/ic_launcher",
                                    app_name:"dev_myapp"]
            dimension "version"
        }
        stage {
            applicationId "com.xx.stage"

            resValue "string", "s1", "stage"

            buildConfigField "String", "ENVIRONMENT", '"stage"'

            manifestPlaceholders = [UMENG_CHANNEL_VALUE:"stage",
                                    app_icon:"@mipmap/ic_launcher",
                                    app_name:"stage_myapp"]
            dimension "version"
        }
        prod {
            applicationId "com.xx.prod"

            resValue "string", "s1", "prod"

            buildConfigField "String", "ENVIRONMENT", '"prod"'

            manifestPlaceholders = [UMENG_CHANNEL_VALUE:"prod",
                                    app_icon:"@mipmap/ic_launcher",
                                    app_name:"prod_myapp"]
            dimension "version"
        }
    }

    //移除lint检测的error
    lintOptions {
        abortOnError false
    }

    buildTypes {
        debug {
            buildConfigField("boolean", "LOG_DEBUG", "true")
            signingConfig signingConfigs.config
        }

        release {
            buildConfigField("boolean", "LOG_DEBUG", "false")
            zipAlignEnabled true
            shrinkResources true
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config

            // 批量打包,使用 .\gradlew assemble 命令编译打包所有配置的包
            android.applicationVariants.all {
                variant ->
                    variant.outputs.all {
                        outputFileName = rootProject.getName()+"_" + productFlavors[0].name + "_" + buildType.name + "_v" + defaultConfig.versionName + "-${releaseTime()}.apk"
                    }
            }

        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

AndroidManifest.xml

<application
        <!-- 动态修改图标和名称 -->
        android:icon="${app_icon}"
        android:label="${app_name}"
        ...>
        <meta-data
            android:name="UMENG_CHANNEL"
            <!-- 动态修改渠道名 -->
            android:value="${UMENG_CHANNEL_VALUE}" />

        </activity...>
    </application>

创建差别化资源代码文件夹,为差别化APP指定特定的行为

main/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1B1A2A</color>
    <color name="colorPrimaryDark">#1B1A2A</color>
    <color name="colorAccent">#FF4081</color>
</resources>

dev/res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#1B1A2A</color>
    <color name="colorPrimaryDark">#888888</color>
    <color name="colorAccent">#FF4081</color>
</resources>

以上是为dev渠道的创建了一个特定的colors资源,在打包dev时所有的引用都将指向该文件,string等都类似与该道理,也可以再创建一个stage/res/values/colors.xml为stage指定特定的资源。

 

 

参考

https://blog.csdn.net/aiynmimi/article/details/68944830

https://www.jianshu.com/p/533240d222d3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值