环境react native 0.63
1.修改AndroidManifest文件
加入以下代码
<meta-data android:value="${UMENG_CHANNEL_VALUE}" android:name="UMENG_CHANNEL"/>
2.修改app.build文件
打包apk命名为app+渠道名+日期+版本号
android {
defaultConfig {
flavorDimensions "default"
}
productFlavors {
dev{
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "dev"]
}
huawei {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "huawei"]
}
oppo {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "oppo"]
}
xiaomi {
manifestPlaceholders = [UMENG_CHANNEL_VALUE: "xiaomi"]
}
}
// applicationVariants are e.g. debug, release
applicationVariants.all { variant ->
variant.outputs.each { output ->
// For each separate APK per architecture, set a unique version code as described here:
// https://developer.android.com/studio/build/configure-apk-splits.html
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) { // null for the universal-debug, universal-release variants
output.versionCodeOverride =
versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
}
if(variant.buildType.name.equals('release')){
def date = new Date().format("yyyyMMdd" , TimeZone.getTimeZone("GMT+08"))
def fileName = "app_${variant.productFlavors[0].name}_${date}_${versionName}.apk"
output.outputFileName = fileName
}
}
}
}
3.react native 中获取渠道名
//俩种方法
第一种使用友盟的AnalyticsConfig.getChannel()方法
@ReactMethod
public void getChannel(final Callback successCallback){
String channelName = AnalyticsConfig.getChannel(getReactApplicationContext());
successCallback.invoke(channelName);
}
//第二种
@ReactMethod
public void getAppMetaData(String key, final Promise promise) {
String resultData = null;
if (getReactApplicationContext() == null || TextUtils.isEmpty(key)) {
resultData = null;
}
try {
PackageManager packageManager = getReactApplicationContext().getPackageManager();
if (packageManager != null) {
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getReactApplicationContext().getPackageName(), PackageManager.GET_META_DATA);
if (applicationInfo != null) {
if (applicationInfo.metaData != null) {
resultData = applicationInfo.metaData.get(key).toString();
}
}
}
promise.resolve(resultData);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
promise.reject(e);
}
}
//js端封装
var { NativeModules } = require('react-native');
const UMShareModule = NativeModules.UMShareModule;
class ShareUtilClass {
getAppMetaData(key){
return UMShareModule.getAppMetaData(key)
}
}
let shareUtil = new ShareUtilClass()
export default shareUtil
//调用
shareUtil.getAppMetaData('UMENG_CHANNEL').then(res=>{
console.log('UMENG_CHANNEL',res)
})
4.调试
加入多渠道包后运行react-native run-android会报错
Task 'installDebug' not found in project ':app'. Some candidates are: 'installDevDebug'.
此时要运行react-native run-android --variant devdebug
为了方便使用,在package.json中修改
"scripts": {
"android": "react-native run-android --variant devdebug"
}
然后在终端运行yarn android即可
5.打包
Android studio 右侧栏gradle,找到app->build->assembleRelease
就会生成所有的渠道包
如果出现下面报错情况
image.png
解决方法如下:
https://stackoverflow.com/questions/59044161/react-native-expiring-daemon-because-jvm-heap-space-is-exhausted
1、gradle.properties文件加入
org.gradle.jvmargs=-Xmx4g -XX:MaxPermSize=2048m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
2、app/build.gradle加入
android {
dexOptions {
javaMaxHeapSize "4g"
}
}
网友评论