Android Studio 多渠道打包和打包時設(shè)置包名

使用 Android Studio 進(jìn)行多渠道打包超級簡單,廢話不多說,兩步:

兩種打包方式:


方式一:

1、在清單文件 AndroidManifest.xml 中添加如下帶

在 AndroidManifest.xml 文件 中 application 內(nèi) 添加:

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

這個是在使用友盟統(tǒng)計時的一個代碼示例,meta-data 中的 name 是可以更改的,根據(jù)實際需求如自己修。

2、在 build.gradle 進(jìn)行配置

在 build.gradle 文件中 android 下添加:

android {

    ........
    
    
    productFlavors {

        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
        }

        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
        }

        c360 {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"]
        }
    }
}

只要進(jìn)行這兩步配置,就可以實現(xiàn)多渠道打包,非常簡單。 上面只配置了三個,如果想配置更多仿照這添加相應(yīng)渠道的就可以了

方式二:

1、在清單文件 AndroidManifest.xml 中添加如下帶

在 AndroidManifest.xml 文件 中 application 內(nèi) 添加:

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

這個是在使用友盟統(tǒng)計時的一個代碼示例,meta-data 中的 name 是可以更改的,根據(jù)實際需求如自己修。

2、在 build.gradle 進(jìn)行配置

在 build.gradle 文件中 android 下添加:

android {

    ........
    
    
    productFlavors {

        wandoujia {}

        baidu {}

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

這樣配置也可以實現(xiàn)多渠道打包。

打包時設(shè)置包名

在 build.gradle 文件中 android 的 buildTypes 的 release 下添加:

applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 輸出apk名稱為boohee_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "myApk_${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }

代碼不多,很簡單,不解釋,照著用就OK了。

完整代碼:

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"
    defaultConfig {
        applicationId "com.cfox.gradlemodel"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            applicationVariants.all { variant ->
                variant.outputs.each { output ->
                    def outputFile = output.outputFile
                    if (outputFile != null && outputFile.name.endsWith('.apk')) {
                        // 輸出apk名稱為boohee_v1.0_2015-01-15_wandoujia.apk
                        def fileName = "myApk_${defaultConfig.versionName}_${variant.productFlavors[0].name}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }

    productFlavors {
        wandoujia {}
        baidu {}
        xiaomi {}
    }

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

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}

最后

在打包時候選擇相應(yīng)的渠道就可以了。

下面對配置文件簡單說明一下:

我們看一下 meta-dataandroid:value="${UMENG_CHANNEL_VALUE}" 其中 ${UMENG_CHANNEL_VALUE} 是通配符,和 build.gradle 文件中 manifestPlaceholders = [UMENG_CHANNEL_VALUE: "c360"] 中的 UMENG_CHANNEL_VALUE 相對應(yīng)。

如果以上內(nèi)容收的不對請及時指出來,共同學(xué)習(xí),共同進(jìn)步。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容