react-native-obfuscator crash for a specific file

Viewed 624

I'm trying to obfuscate my JS code using the lib react-native-obfuscator. I need to hide the API keys from my code, but I'm getting the following error:

error src/services/api.js: function (prevType) {
this.state.exprAllowed = false;

if (prevType === types._let || prevType === types._c...<omitted>...
} could not be cloned. Run CLI with --verbose flag for more details.

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:bundleReleaseJsAndAssets'.

When I do not obfuscate the file "src/services/api.js" I get the desired result, but it is the most important. My transform.js file is:

const obfuscatingTransformer = require("react-native-obfuscator")
const filter = filename => { 
return filename.startsWith("src");
} 

module.exports = obfuscatingTransformer({
obfuscatorOptions:{
compact: true, // default true
controlFlowFlattening: false, // default false
controlFlowFlatteningThreshold: 0.75, // default 0.75
deadCodeInjection: false, // default false
deadCodeInjectionThreshold: 1, // default 0.4
debugProtection: false,  // default false
debugProtectionInterval: false, // default false
disableConsoleOutput: true, 
identifierNamesGenerator: 'hexadecimal', // default hexadecimal
log: false,  // default false
renameGlobals: true, // default false
rotateStringArray: true, // default true
seed: 0,// default 0
selfDefending: false,  // default true: not working if true
shuffleStringArray: true, // default true
sourceMapMode: 'separate', // default seperate
splitStrings: true,  // default false
splitStringsChunckLength: 10, // default 10
stringArray: true, // default true
stringArrayEncoding: 'rc4', // default false
stringArrayThreshold: 0.75, // default 0.8
target: 'browser', // default browser
transformObjectKeys: true, // default false
unicodeEscapeSequence: true
},
upstreamTransformer: require('metro-react-native-babel-transformer'),
emitObfuscatedFiles: false,
enableInDevelopment: false,
filter: filter,
trace: true

})

Could someone help me here? Thank you in advance

1 Answers

I tried react-native-obfuscator and man it has to many issues.

So I am using obfuscator-io-metro-plugin and its working great.

Here I will post my settings for you to see.

in metro.config.js Add the following and it will obfuscator all string and js code related files.

const jsoMetroPlugin = require("obfuscator-io-metro-plugin")(
  {
    compact: false,
    sourceMap: true,
    controlFlowFlattening: true,
    controlFlowFlatteningThreshold: 1,
    numbersToExpressions: true,
    simplify: true,
    shuffleStringArray: true,
    splitStrings: true,
    stringArrayThreshold: 1,
  },
  {
    runInDev: false /* optional */,
    logObfuscatedFiles: true /* optional generated files will be located at ./.jso */,
    sourceMapLocation:
      "./index.android.bundle.map" /* optional  only works if sourceMap: true in obfuscation option */,
  }
);
module.exports = {
  transformer: {
    assetPlugins: ['expo-asset/tools/hashAssetFiles'],
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: false,
        inlineRequires: false,
      },
    }),
  },
  ...jsoMetroPlugin
};

And do not forget to include those below in your github .ignore file so it wont be checked in that is if you are using sourceMap

index.android.bundle.map
.jso/
Related