Android多渠道打包踩坑笔记

  1. 为什么会需要多渠道打包
  2. 开始多渠道打包

1. 为什么会需要多渠道打包?

  • 给运营和产品提供分析的数据支撑,为下一步战略部署准备。

2. 开始多渠道打包(以友盟统计多渠道打包为例)

冗余且无效的配置

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    flavorDimensions "default"
    defaultConfig {
        applicationId "com.qdb.myapplication"
        minSdkVersion 18
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        
        manifestPlaceholders = ["CHANNEL_NAME": "yingyongbao"]
    }

    signingConfigs {
        releaseA {
            storeFile file("testchannel.jks")
            storePassword '123456'
            keyPassword '123456'
            keyAlias = 'testchan'
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.releaseA
            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith(".apk")) {
                        def fileName = "${variant.productFlavors[0].name}" + ".apk"
                        output.outputFileName = fileName
                    }
                }
            }

        }
    }

    productFlavors {
        yingyongbao {
             manifestPlaceholders = [CHANNEL_NAME: "yingyongbao"]
        }
        baidu {
             manifestPlaceholders = [CHANNEL_NAME: "baidu"]
        }
        sanliuling {
             manifestPlaceholders = [CHANNEL_NAME: "sanliuling"]
        }
        huawei {
             manifestPlaceholders = [CHANNEL_NAME: "huawei"]
        }
        oppo {
             manifestPlaceholders = [CHANNEL_NAME: "oppo"]
        }
        vivo {
             manifestPlaceholders = [CHANNEL_NAME: "vivo"]
        }
        productFlavors.all { flavor ->
            flavor.manifestPlaceholders = [CHANNEL_NAME: name]
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.core:core-ktx:1.2.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

    implementation 'com.umeng.umsdk:analytics:8.0.0'
    implementation 'com.umeng.umsdk:common:2.0.0'
}

此时打包出来的渠道包永远渠道都是yingyongbao

优化

因为在defaultConfig节点下配置的(manifestPlaceholders = ["CHANNEL_NAME": "yingyongbao"])值是固定的 ,需要做出修改:

  1. defaultConfig 节点下的manifestPlaceholders = ["CHANNEL_NAME": "yingyongbao"] 修改为manifestPlaceholders = ["CHANNEL_NAME": name]

  2. productFlavors 后面仍旧有如下代码:

     productFlavors.all { flavor ->
                flavor.manifestPlaceholders = [CHANNEL_NAME: name]
     }
    

    有这段代码其实是优化了上面的赋值步骤,所以这段代码代码可以优化为:

        productFlavors {
            yingyongbao {
            }
            baidu {
            }
            sanliuling {
            }
            huawei {
            }
            oppo {
            }
            vivo {
            }
            productFlavors.all { flavor ->
                flavor.manifestPlaceholders = [CHANNEL_NAME: name]
            }
        }
    

    最终代码:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-android-extensions'
    
    android {
        compileSdkVersion 29
        buildToolsVersion "29.0.3"
        flavorDimensions "default"
        defaultConfig {
            applicationId "com.qdb.myapplication"
            minSdkVersion 18
            targetSdkVersion 29
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
            manifestPlaceholders = [CHANNEL_NAME: name]
        }
    
        signingConfigs {
            releaseA {
                storeFile file("testchannel.jks")
                storePassword '123456'
                keyPassword '123456'
                keyAlias = 'testchan'
            }
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                signingConfig signingConfigs.releaseA
                applicationVariants.all { variant ->
                    variant.outputs.each { output ->
                        def outputFile = output.outputFile
                        if (outputFile != null && outputFile.name.endsWith(".apk")) {
                            def fileName = "${variant.productFlavors[0].name}" + ".apk"
                            output.outputFileName = fileName
                        }
                    }
                }
    
            }
        }
    
        productFlavors {
            yingyongbao {
            }
            baidu {
            }
            sanliuling {
            }
            huawei {
            }
            oppo {
            }
            vivo {
            }
            productFlavors.all { flavor ->
                flavor.manifestPlaceholders = [CHANNEL_NAME: name]
            }
        }
    }
    
    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'androidx.appcompat:appcompat:1.1.0'
        implementation 'androidx.core:core-ktx:1.2.0'
        implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    
        implementation 'com.umeng.umsdk:analytics:8.0.0'
        implementation 'com.umeng.umsdk:common:2.0.0'
    }
    

打包

  1. 一次性打所有包:gradlew assembleRelease
  2. 打指定渠道包(以vivo渠道包为例):gradlew assembleVivoRelease
  3. 也可以使用Generate Signed Bundle or APK选择需要打包的版本打包
  4. 还可以使用三方的多渠道打包工具,比如美团

使用命令打包的文件在build.outputs.apk文件夹下

总结

  1. 为了给产品和运营提供战略层的数据进行多渠道打包;
  2. 在打包配置的时候如果在defaultConfig节点下配置了参数记得不要写成固定值了;
  3. 打包配置和输出路径。

你可能感兴趣的:(Android多渠道打包踩坑笔记)