您的当前位置:首页正文

Gradle实战:Android多渠道打包方案及自定义APP名称

2024-11-23 来源:个人技术集锦

1、AndroidManifest.xml

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

2、build.gradle

defaultConfig {
    applicationId "com.katao54.card"
    minSdkVersion rootProject.ext.minSdkVersion
    targetSdkVersion rootProject.ext.targetSdkVersion
    versionCode 3
    versionName "1.1.2"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    //日志打印
    buildConfigField("boolean", "IS_DEBUG_ON", "true")

    multiDexEnabled true
    manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    //版本名后面添加一句话,意思就是flavor dimension 它的维度就是该版本号,这样维度就是都是统一的了
    flavorDimensions "35"
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        //日志打印
        buildConfigField("boolean", "IS_DEBUG_ON", "false")
    }
    debug {
        //日志打印
        buildConfigField("boolean", "IS_DEBUG_ON", "true")
    }
}

android.applicationVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "katao-${buildType.name}-${variant.productFlavors[0].name}-${defaultConfig.versionName}.apk"
    }
}

或者

android.applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def file = output.outputFile
                    output.outputFile = new File(file.parent, file.name.replace(".apk", "-" + defaultConfig.versionName + ".apk"))
                }
            }

productFlavors {
    qq {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "qq"]
    }
    xiaomi {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
    }
    /*  '360zhushou' {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "360zhushou"]
    }*/
    baidu {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
    }
    wandoujia {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
    }
    umeng {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }
    google {
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "google"]
    }
}

3、assemble结合Build Variants来创建task

  • 打包所有Release版本:

    gradlew assembleRelease


  • 打包所有Debug版本:

    gradlew assembleDebug


  • 打包wandoujia渠道的release版本:

    gradlew assembleWandoujiaRelease


  • 只打wandoujia渠道版本(此命令会生成wandoujia渠道的Release和Debug版本):

    gradlew assembleWandoujia

显示全文