在Android上的React Native中包含和读取源映射文件,可以通过以下步骤实现:
project.ext.react = [
entryFile: "index.js",
bundleAssetName: "index.android.bundle",
bundleInDebug: true, // 设置为true以在调试模式下包含源映射文件
devDisabledInDebug: false, // 设置为false以在调试模式下启用开发者菜单
]
// 添加以下代码
def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = true
// ...
// 添加以下代码
project.ext.react = [
bundleAssetName: "index.android.bundle",
bundleInDebug: true,
bundleInRelease: true,
devDisabledInDebug: false,
devDisabledInRelease: true, // 设置为true以在发布模式下禁用开发者菜单
enableHermes: false, // 如果使用Hermes引擎,请将其设置为true
enableSeparateBuildPerCPUArchitecture: enableSeparateBuildPerCPUArchitecture,
hermesCommand: "../../node_modules/hermes-engine/%OS-BIN%/hermesc", // 如果使用Hermes引擎,请根据操作系统设置正确的路径
jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
jsBundleDirRelease: "$buildDir/intermediates/assets/release",
jsBundleFile: "$buildDir/intermediates/assets/debug/index.android.bundle",
jsBundleLoader: "com.facebook.react.bridge.JSBundleLoader$FileLoader",
jsMainModuleName: "index",
optimizeEnabled: enableProguardInReleaseBuilds,
packageEncoding: "utf-8",
packageObfuscationEnabled: enableProguardInReleaseBuilds,
resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
shrinkResources: enableProguardInReleaseBuilds,
souceMapFile: "$buildDir/intermediates/assets/debug/index.android.bundle.map", // 设置源映射文件的路径
]
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import com.facebook.soloader.SoLoader;
// 添加以下代码
import com.facebook.react.modules.debug.SourceMapLoader;
import com.facebook.react.modules.debug.Systrace;
import com.facebook.react.modules.debug.interfaces.DeveloperSettings;
import com.facebook.react.modules.debug.interfaces.PackagerStatusCallback;
import com.facebook.react.packagerconnection.RequestHandler;
import com.facebook.react.packagerconnection.RequestHandlerRegistry;
import com.facebook.react.packagerconnection.Responder;
import com.facebook.react.shell.MainPackageConfig;
import com.facebook.react.shell.MainPackageConfig.AppBundleSupplier;
import com.facebook.react.shell.MainPackageConfig.Builder;
import com.facebook.react.shell.MainPackageConfig.Params;
import com.facebook.react.shell.MainPackageConfigProvider;
import com.facebook.react.shell.PackageConfig;
import com.facebook.react.shell.PackageConfigProvider;
import com.facebook.react.shell.PackageList;
import com.facebook.react.shell.ReactPackageLogger;
import com.facebook.react.shell.ReactPackageRegistry;
import com.facebook.react.shell.ReactPackageRegistry.Builder;
import com.facebook.react.shell.ReactPackageRegistryProvider;
import com.facebook.react.shell.ReactPackagesProvider;
import com.facebook.react.shell.ShellReactPackage;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
// ...
// 添加以下代码
private static final String SOURCE_MAP_FILE_NAME = "index.android.bundle.map"; // 源映射文件的文件名
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
// 添加以下代码
if (BuildConfig.DEBUG) {
enableSourceMaps();
}
}
private void enableSourceMaps() {
try {
String sourceMapFilePath = getFilesDir().getAbsolutePath() + File.separator + SOURCE_MAP_FILE_NAME;
File sourceMapFile = new File(sourceMapFilePath);
if (sourceMapFile.exists()) {
ReactInstanceManager reactInstanceManager = getReactNativeHost().getReactInstanceManager();
DeveloperSettings devSettings = reactInstanceManager.getDevSupportManager().getDevSettings();
devSettings.setBundleDeltasEnabled(false);
devSettings.setBundleSourceMapPath(sourceMapFilePath);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// ...
}
cd android
./gradlew clean
./gradlew assembleDebug
这样,Android上的React Native应用就可以包含和读取源映射文件了。源映射文件对于调试和错误追踪非常有用,它可以将压缩后的代码映射回原始源代码,方便开发者定位和修复bug。
推荐的腾讯云相关产品:腾讯云移动应用托管(Mobile Application Hosting),详情请参考腾讯云移动应用托管产品介绍。
领取专属 10元无门槛券
手把手带您无忧上云