Code Obfuscation in React Native: A Comprehensive Guide

Aqeel Ahmad
3 min readFeb 23, 2024

--

Introduction
Code obfuscation is a technique used to make code difficult to understand and analyze. In React Native, code obfuscation can be used to protect intellectual property, prevent reverse engineering, and improve the security of your application.

Why Code Obfuscation is Needed
There are several reasons why you might want to obfuscate your React Native code:

  • Protect intellectual property: Obfuscating your code makes it more difficult for others to steal your ideas or copy your work.
  • Prevent reverse engineering: Obfuscation can make it difficult for attackers to reverse engineer your app and identify vulnerabilities.
  • Improve security: Obfuscated code is more difficult to exploit, as attackers will have a harder time understanding how it works.

How to Obfuscate React Native Code
There are a number of different ways to obfuscate React Native code. Some of the most popular methods include:

  • Plugin method: You can use a plugin to obfuscate your React Native code. There are a number of different plugins available, such as: obfuscator-io-metro-plugin
    @smartface/obfuscator-io-metro-plugin
    react-native-obfuscating-transformer
  • ProGuard: ProGuard is a free and open-source tool that can be used to obfuscate Java code. It is included in the Android SDK and can be used to obfuscate React Native code that is written in Java.
  • R8: R8 is a newer tool that is also included in the Android SDK. It is designed to replace ProGuard and offers a number of advantages, including improved performance and support for newer versions of Java.
  • Hermes: Hermes is a JavaScript engine that is developed by Facebook. It is designed to be more efficient and secure than the default JavaScript engine in React Native. Hermes includes a built-in obfuscator that can be used to obfuscate React Native code that is written in JavaScript.
  • DexGuard: DexGuard is a commercial obfuscator that is specifically designed for Android apps which offers a number of advanced features.
  • iXGuard: iXGuard is a commercial obfuscator that is specifically designed for iOS apps which offers a number of advanced features.

Today, I will focus on plugin method to obfuscate our code in React Native. I will explain each step how we can obfuscate our code using obfuscator-io-metro-plugin.

Obfuscate React Native Code using Metro Plugin
To use the Obfuscator.io Metro Plugin, you will need to install it from npm. You can do this by running the following command:

npm i -D obfuscator-io-metro-plugin

Once the plugin is installed, you will need to add it to your Metro configuration file. You can do this by adding the following code to your metro.config.js file:

const jsoMetroPlugin = require("obfuscator-io-metro-plugin")(
{
// for these option look javascript-obfuscator library options from above url
compact: false,
sourceMap: false, // source Map generated after obfuscation is not useful right now so use default value i.e. false
controlFlowFlattening: true,
controlFlowFlatteningThreshold: 1,
numbersToExpressions: true,
simplify: true,
stringArrayShuffle: true,
splitStrings: true,
stringArrayThreshold: 1,
},
{
runInDev: false /* optional */,
logObfuscatedFiles: true /* optional generated files will be located at ./.jso */,
}
);

module.exports = {
transformer: {
getTransformOptions: async () => ({
transform: {
experimentalImportSupport: false,
inlineRequires: false,
},
}),
},
...jsoMetroPlugin, /*add this line in your previous module after defined above*/
};

How to Check if Obfuscation is Working

After you have successfully set up the Obfuscator.io Metro Plugin, you can check if the code is obfuscated or not by creating a build using assembleRelease or bundleRelease or an IPA from Xcode and checking the code at the following location:

/.jso

If the code in this location is obfuscated, then the plugin is working correctly.

Conclusion

Code obfuscation is a valuable tool that can help you protect your intellectual property, prevent reverse engineering, and improve the security of your React Native application. However, it is important to weigh the pros and cons carefully before deciding whether or not to obfuscate your code.

--

--