Developers Manual > Cocos2d-x > Modules > Scripting > Javascript Binding > How to Obfuscate Javascript Source Code on JSB

How to Obfuscate Javascript Source Code on JSB

Obfuscation of javascript sources on the top of cocos2d JSB is available since cocos2d-x v2.1.3

Related modifications

Background Knowledge

Google Closure Compiler

We use Google Closure Compiler to obfuscate javascript source code on both cocos2d-html5 and cocos2d-x. You can download its jar package from, while I have located the latest version in cocos2d-x/tools/closure-compiler/compiler.jar

Before using Closure Compiler, the ant tool is required, you can download it from You can run ant -buildfile myconfig.xml to generate obfuscated javascript code, the magic behind is myconfig.xml, which is prepared at cocos2d-x/tools/closure-compiler/template.xml, and the will scan the input folder and fill all file list into this template, then create an obfuscate.xml for you.
h3. Exclude Class names and Functions
The difference between obfuscating cocos2d-html5 games and cocos2d-x jsb games are, in cocos2d-x jsb games, functions exported from c*+ mustn’t be obfuscated. I wrote the exclude functions in
* cocos2d-x/scripting/javascript/bindings/obfuscate/obfuscate_exclude_cocos2d.js
* cocos2d-x/scripting/javascript/bindings/obfuscate/obfuscate_exclude_chipmunk.js
These files are written in the obfuscate.xml as
<externs dir="${basedir}/scripting/javascript/bindings/obfuscate">
<file name="obfuscate_exclude_cocos2d.js"/>
<file name="obfuscate_exclude_chipmunk.js"/>
If you export some javascript binding API from C/C*+ to javascript, don’t forget to add the exclude class names and functions here, otherwise the game code can’t find proper C++ functions after obfuscation.
h3. Don’t use “require” in javascript codes
As you can see, I move all “require” instructions into only one file, which will be include in the debug build. Because debug build should not obfuscate javascript codes to make debugging easier. While in release build, the obfuscate operation will merge all javascript codes into one file: game.js.
Google Closure Compiler doesn’t obfuscate javascript keywords, so “require(xxx.js)” instructions will be left in game.js. This will lead to “Can’t find xxx.js file” error at runtime. Actually, the content of xxx.js file is already in your obfuscated big game.js file.
h2. Starting the Job
h3. Step 1: Using to generate obfuscate.xml
I wrote cocos2d-x/tools/closure-compiler/ to scan javascript files in
input folder, generate obfuscate.xml from template.xml and call ant tools to generate game.js for you.

For example, in samples/javascript/TestJavascript, we can run the script like this

Walzers-mbpr:TestJavascript walzer$ /workspace/cocos2d-x/tools/closure-compiler/ -input ../Shared/tests/ -output ./ -cocos2d /workspace/cocos2d-x/
preparing configs...
generating obfuscate.xml for google closure compiler
Successful! obfuscate.xml is generate
Note: Please reoder the files sequence in obfuscate.xml, keep it the same order as javascript "requrie" instruction,
then call "ant -buildfile obfuscate.xml" to obfuscate your js codes.
Walzers-mbpr:TestJavascript walzer$ 

Step 2: Modify the files sequence in obfuscate.xml

In step 1, the python script scanned the input folder to generate obfuscate.xml, which can be used by ant and google closure compiler. But the script is not smart enough to know the “require” sequence in your javascript codes. For examples, in, there’s a require sequence in it. Now you should apply this sequence in your obfuscate.xml

Step 3: Config your Projects

You can take TestJavascript.xcodeproj as a sample. I created 2 targets, one is TestJavascript, another is TestJavascriptObfuscated.
In TestJavascriptObfuscated, before “Copy Bundle Resources” phase, a “Build Phases” was added, which runs a script, regenerate game.js from obfuscate.xml each time. And in the “Copy Bundle Resources” phase, only game.js this javascript code is involved.

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.