Android 进行友盟多渠道打包步骤详解

1、首先在AndroidManifest.xml文件中的application标签内加入

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

2、在应用的build.gradle文件中,添加如下代码

plugins {
    
    
    //表示这是一个应用程序模块,com.android.library表示这是一个库模块
    id 'com.android.application'
}

android {
    
    
    //编译SDK版本
    compileSdkVersion 30
    //编译工具版本
    buildToolsVersion "30.0.3"

    defaultConfig {
    
    
        //appId
        applicationId "com.example.exercisetest"
        //最小支持SDK版本
        minSdkVersion 23
        //目标版本
        targetSdkVersion 30
        //版本号
        versionCode 1
        //版本名称
        versionName "1.0"

        //测试单元
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
	       //必须要保证所有的flavor 都属于同一个维度
        flavorDimensions "default"
    }

    //构建类型
    buildTypes {
    
    
        //生产环境
        release {
    
    
            //表示是否对项目的代码进行混淆,true表示混淆 false表示不混淆
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

    }
    compileOptions {
    
    
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    lintOptions {
    
    
        checkReleaseBuilds false
        abortOnError false
    }

    //方式一
//    productFlavors {
    
    
//        wandoujia {
    
    
//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "wandoujia"]
//        }
//        xiaomi {
    
    
//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
//        }
//        yingyongbao {
    
    
//            manifestPlaceholders = [UMENG_CHANNEL_VALUE: "yingyongbao"]
//        }
//    }

    //优化1
//    productFlavors {
    
    
//        wandoujia {
    
    
//        }
//        xiaomi {
    
    
//        }
//        yingyongbao {
    
    
//        }
//    }
//    productFlavors.all {
    
     flavor ->
//        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
//    }

    //优化二
    productFlavors {
    
    
        wandoujia {
    
    
        }
        xiaomi {
    
    
        }
        yingyongbao {
    
    
        }
    }
    productFlavors.all {
    
     flavor ->
        flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]
    }

    applicationVariants.all {
    
     variant ->
        variant.outputs.all {
    
     output ->
            def outputFile = output.outputFile
            if (outputFile != null && outputFile.name.endsWith('.apk')) {
    
    
            	//打包的apk包名变为了
            	//app-具体的渠道名-release-1.0.apk
                def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}.apk")
                outputFileName = fileName
            }
        }
    }

}

//依赖相关信息
dependencies {
    
    

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

3.可以在Java代码中,获取apk对应的渠道名

public String getChannel() {
    
    
        ApplicationInfo applicationInfo = null;
        try {
    
    
            //getPackageManager():返回PackageManager实例以查找全局包信息。
            //getApplicationInfo(String packageName, int flags):
            // 检索我们所了解的有关特定软件包/应用程序的所有信息。
            //参数一:	getPackageName():返回此应用程序包的名称
            //参数二:PackageManager.GET_META_DATA
            // ComponentInfo标志:返回与组件关联的metaData数据Bundle 。 这适用于返回ComponentInfo子类的任何API。
            applicationInfo = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
            if (applicationInfo != null) {
    
    
                //metaData属性在其ApplicationInfo类的父类PackageItemInfo里面
                //metaData:与此组件关联的其他元数据。
                //BaseBundle.getString():返回与给定键相关联的值
                return applicationInfo.metaData.getString("UMENG_CHANNEL");
            }
        } catch (PackageManager.NameNotFoundException e) {
    
    
            e.printStackTrace();
        }
        return "";
    }

猜你喜欢

转载自blog.csdn.net/lu202032/article/details/124685066