欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 实现多渠道打包

程序员文章站 2024-01-11 16:34:04
...
1,build.gradle
apply plugin: 'com.android.application'


android {
    compileSdkVersion 26
    // API环境
    def apiEnvironment = API_ENVIRONMENT ? API_ENVIRONMENT : project.API_ENVIRONMENT
    flavorDimensions 'api'

    productFlavors {
        if (apiEnvironment == "RELEASE") {
            baidu {
                // 每个环境包名不同
                applicationId "app.lcb.com.myapplication.baidu"
                // 动态添加 string.xml 字段;
                // 注意,这里是添加,在 string.xml 不能有这个字段,会重名!!!
                resValue "string", "app_name", "百度"
                resValue "bool", "auto_updates", 'false'
                // 动态修改 常量 字段
                buildConfigField "String", "ENVIRONMENT", '"我是百度首页"'
                // 修改 AndroidManifest.xml 里渠道变量
                manifestPlaceholders = [CHANNEL_VALUE: "baidu"
                                        ,app_icon   : "@mipmap/logo_baidu"]
            }
//            _360 {}
            xiaomi {
                applicationId "app.lcb.com.myapplication.xiaomi"

                resValue "string", "app_name", "小米"
                resValue "bool", "auto_updates", 'true'
                resValue "drawable", "isrRank", 'true'

                buildConfigField "String", "ENVIRONMENT", '"我是小米首页"'

                manifestPlaceholders = [CHANNEL_VALUE: "xiaomi"
                                        ,app_icon   : "@mipmap/icon_xiaomi"]
            }
//            huawei {}
//            _91 {}
//            appchina {}
//            anzhi {}
//            aliapp {}
            qq {
                applicationId "app.lcb.com.myapplication.qq"

                resValue "string", "app_name", "腾讯"
                resValue "bool", "auto_updates", 'true'

                buildConfigField "String", "ENVIRONMENT", '"我是腾讯首页"'

                manifestPlaceholders = [CHANNEL_VALUE: "qq"
                                        ,app_icon   : "@mipmap/icon_qq"]
            }
        } else if (apiEnvironment == "PRO") {
            official {}
        } else {
            dev {}
        }
    }

    productFlavors.all {
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //适配Android studio 3.0的版本,必须要productFlavors 一致
        flavorDimensions "versionCode"

    }
        //签名
        signingConfigs {
            release {
                //设置release的签名信息
                storeFile file(project.STORE_FILE)      //签名文件
                storePassword project.STORE_PASSWORD
                keyAlias project.KEY_ALIAS
                keyPassword project.KEY_PASSWORD  //签名密码
            }
        }

        lintOptions {
            abortOnError false
            // if true, only report errors
            ignoreWarnings true
        }


        buildTypes {
            debug {
                // debug模式下,显示log
                buildConfigField("boolean", "LOG_DEBUG", "true")

                //为已经存在的applicationId添加后缀
                applicationIdSuffix ".debug"
                // 为版本名添加后缀
                versionNameSuffix "-debug"
                // 不开启混淆
                minifyEnabled true
                // 不开启ZipAlign优化
                zipAlignEnabled false
                // 不移除无用的resource文件
                shrinkResources true
                // 使用config签名
                signingConfig signingConfigs.release

            }

            release {
                // release模式下,不显示log
                buildConfigField("boolean", "LOG_DEBUG", "false")
                // 为版本名添加后缀
                versionNameSuffix "-relase"
                // 不开启混淆
                minifyEnabled true
                // 开启ZipAlign优化
                zipAlignEnabled true
                // 移除无用的resource文件
                shrinkResources true
                // 使用config签名
           signingConfig signingConfigs.release
                // 混淆文件位置
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

//            // 批量打包
                applicationVariants.all { variant ->
                    variant.outputs.all {
                        outputFileName = "driver_${variant.productFlavors[0].name}_v${variant.versionName}.apk"
                    }
                }

            }
        }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    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'
    configurations.all {
        resolutionStrategy.force 'com.android.support:support-annotations:26.1.0'
    }
}

2,把value 里的string的app_name删掉:资源文件logo图片 必须保证一致

Android 实现多渠道打包