Android多渠道打包(友盟)

1.在Manifest.xml文件中添加權(quán)限和AppKey

權(quán)限

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

AppKey和渠道變量:

<meta-data android:value="59ba89cff43e48471f0000eb" android:name="UMENG_APPKEY"/>
//設(shè)置動(dòng)態(tài)渠道變量
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>

2.在build.gradle中設(shè)置productFlavors

為不同的渠道市場(chǎng)進(jìn)行設(shè)置:

android {  
    productFlavors {
        xiaomi {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
        }
        baidu {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
        }
        wandoujia {
            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
        }
    }  
}

?

//通過(guò)gradle腳本語(yǔ)法統(tǒng)一設(shè)置不同的渠道
android {  
    productFlavors {
        xiaomi {}
        baidu {}
        wandoujia {}
    }  
    productFlavors.all{
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }
}

3.對(duì)應(yīng)用進(jìn)行打包操作

在AndroidStudio菜單欄點(diǎn)擊Build菜單–>Generate signed APK


4.通過(guò)studio命令行工具,進(jìn)行打包

命令:gradlew assembleRelease 開(kāi)始打包

打包成功

查看apk

至此,多渠道打包就已經(jīng)完成??!

4.簽名

//添加簽名文件配置
signingConfigs{
    debug{}
    //為release包添加簽名文件配置
    release{
        storeFile file("Multi.jks")
        storePassword "5258168699"
        keyAlias "Multi"
        keyPassword "5258168699"
    }
}

注意:signingConfigs代碼塊一定要寫在buildTypes前面,否則會(huì)報(bào)錯(cuò):
Could not find property 'debugConfig' on SigningConfig container.

5.自己的多渠道打包

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "xiaoming.com.multi"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        //默認(rèn)設(shè)置友盟
        manifestPlaceholders = [UMENG_CHANNEL_VALUE: "umeng"]
    }
    //添加簽名文件配置
    signingConfigs{
        debug{}
        //為release包添加簽名文件配置
        release{
            storeFile file("Multi.jks")
            storePassword "5258168699"
            keyAlias "Multi"
            keyPassword "5258168699"
        }
    }
    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名稱為wooyun_v1.0_wandoujia.apk
                        def fileName = "${variant.productFlavors[0].name}_app_v${defaultConfig.versionName}.apk"
                        output.outputFile = new File(outputFile.parent, fileName)
                    }
                }
            }
        }
    }
    productFlavors{
//        xiaomi{
////            manifestPlaceholders = [UMENG_CHANNEL_VALUE : "xiaomi"]
//            //對(duì)不同的渠道進(jìn)行值替換
//            resValue("string","app_name","xiaomi")
//        }
//        wandoujia{
////            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
//            resValue("string","app_name","wandoujia")
//        }
//        baidu{
////            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "baidu"]
//            resValue("string","app_name","baidu")
//        }

        //為不同功能的apk進(jìn)行打包測(cè)試,可以在同一手機(jī)上安裝同一應(yīng)用進(jìn)行不同功能的測(cè)試
        okhttp{
            applicationIdSuffix "okhttp"
            resValue "string","app_name","okHttp"
        }
        jpush{
            applicationIdSuffix "jpush"
            resValue "string","app_name","jpush"
        }
    }
    //通過(guò)gradle腳本語(yǔ)法統(tǒng)一設(shè)置不同的渠道
    productFlavors.all{
        flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL_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:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    //添加友盟應(yīng)用統(tǒng)計(jì)支持
    compile 'com.umeng.analytics:analytics:latest.integration'
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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