-
創(chuàng)建一個(gè)java類(lèi)和一個(gè)react package榄鉴。
在AppInfoPackage.java類(lèi)中寫(xiě)入需要傳遞的值:(按alt+enter自動(dòng)引入包)
package com.reactnativedemo.modules;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.reactnativedemo.BuildConfig;
import java.util.HashMap;
import java.util.Map;
public class AppInfoPackage extends ReactContextBaseJavaModule {
public AppInfoPackage(ReactApplicationContext reactContext) {
super(reactContext);
}
@NonNull
@Override
public String getName() {
return "AppInfoModule";
}
@Nullable
@Override
// 獲取環(huán)境
public Map<String, Object> getConstants() {
final Map<String, Object> constants = new HashMap<>();
constants.put("APP_ENV", BuildConfig.BUILD_TYPE);
constants.put("APPLICATION_ID", BuildConfig.APPLICATION_ID);
constants.put("VERSION_CODE", BuildConfig.VERSION_CODE);
constants.put("VERSION_NAME", BuildConfig.VERSION_NAME);
constants.put("BASE_URL", BuildConfig.BASE_URL);
constants.put("IS_HERMES_ENABLED", BuildConfig.IS_HERMES_ENABLED);
constants.put("IS_NEW_ARCHITECTURE_ENABLED", BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
return constants;
}
}
- 在RNReactPackage.java中添加這個(gè)類(lèi):(按alt+enter自動(dòng)引入包)
package com.reactnativedemo.modules;
import androidx.annotation.NonNull;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class RNReactPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
@NonNull ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
// 添加一個(gè)安卓原生的activity模塊
modules.add(new AppInfoPackage(reactContext));
return modules;
}
}
-
在mainapplication中將RNReactPackage添加進(jìn)去:(按alt+enter自動(dòng)引入包)
val packages: MutableList<ReactPackage> = PackageList(this).packages.toMutableList()
packages.add(RNReactPackage())
return packages
- 在js中幔欧,src目錄下創(chuàng)建modules文件夾滞欠,并在modules文件夾下創(chuàng)建AppInfoModule.ts文件調(diào)用原生參數(shù)
import { NativeModules, Platform } from 'react-native';
const { AppInfoModule } = NativeModules;
console.log(NativeModules.AppInfoModule)
// 明確告知 TypeScript 對(duì)象的結(jié)構(gòu)
interface EnvironmentDictionary {
[key: string]: string;
}
const { APP_ENV: app_env } = AppInfoModule;
//可按需引入安卓代碼AppInfoPackage.java中定義的變量
// @Nullable
// @Override
// // 獲取環(huán)境
// public Map<String, Object> getConstants() {
// final Map<String, Object> constants = new HashMap<>();
// constants.put("APP_ENV", BuildConfig.BUILD_TYPE);
// constants.put("APPLICATION_ID", BuildConfig.APPLICATION_ID);
// constants.put("VERSION_CODE", BuildConfig.VERSION_CODE);
// constants.put("VERSION_NAME", BuildConfig.VERSION_NAME);
// constants.put("BASE_URL", BuildConfig.BASE_URL);
// constants.put("IS_HERMES_ENABLED", BuildConfig.IS_HERMES_ENABLED);
// constants.put("IS_NEW_ARCHITECTURE_ENABLED", BuildConfig.IS_NEW_ARCHITECTURE_ENABLED);
// return constants;
// }
const envDic: EnvironmentDictionary = Platform.select<EnvironmentDictionary>({
ios: {
"1": 'debug',
"2": 'test',
"3": 'stage',
"4": 'production'
},
android: {
debug: 'debug',
releaseTest: 'test',
releaseStage: 'stage',
release: 'production',
}
}) as EnvironmentDictionary;
const appEnv = envDic[app_env] || 'production';
console.log('當(dāng)前環(huán)境', appEnv);
export default {
appEnv,
isDebug: appEnv === 'debug',
isTest: appEnv === 'test',
isStage: appEnv === 'stage',
isProd: appEnv === 'production',
};
-
業(yè)務(wù)層調(diào)用appEnv: