Check and optimize Phonegap apps by using Google's Closure compiler

The Closure Compiler

The Closure Compiler is an open source tool for making JavaScript download and run faster, and is also a free online service offered by Google. Instead of compiling from a source language to native machine code, Closure Compiler compiles from JavaScript to optimized JavaScript.

Running your scripts through the Google Closure Compiler really makes a difference. Code will be smaller. For this reason, compiled code will parse quicker on the mobile device. Additionally, Closure Compiler provides other benefits in regards to code optimization.

This tool also checks syntax, variable references and types. Once it does, it warns about common JavaScript pitfalls. In other words, Closure Compiler is a great tool to automatically spot common syntax or semantic errors. More importantly, this tool parses your JavaScript code, analyzes it, removes whatever dead code (code that’s never used or called) and rewrites to finally minimize what’s left.

Creating Android Applications with Phonegap/Apache Cordova

PhoneGap is an online platform that allows programmers to build applications for Android and other mobile devices. In essence, PhoneGap is a wrapper of Apache Cordova, an open source software for developing applications using JavaScript, HTML5, and CSS3.

Although Phonegap is a very powerful tool for developing multi-platform applications, debugging support for your PhoneGap applications is not as well-developed as for native Apps or pure web apps.

Debugging Applications

Debugging and optimizing CSS and HTML code is rather simple using a Web Browser with good CSS3 and HTML5 support (such as Chrome or Firefox). Having said that, doing the same with the JavaScript layer connection the front-end with the device’s hardware is quite a different story.

The main hurdle is that the code to debug is on your mobile device or simulator, not your browser. This makes it difficult to synchronize breakpoints or retrieve stack traces. Console logging can help somehow but not as much as in Web browsers. With that in mind, wouldn’t it be better if errors were caught before debugging? That’s why you want to use this tool, as it does exactly that.

Benefits of using the Closure Compiler

In short, using the Closure Compiler brings the following benefits to the development toolset:

Code checking. Closure Compiler provides warnings for illegal JavaScript an as well as potentially dangerous operations. This helps developers to produce less buggy and easier to maintain JavaScript code.

Efficiency. It reduces the size of your JavaScript code helping your application to load faster and reducing your bandwidth needs. Also “dead” code is removed, reducing parsing overhead even further. In other words, if you run your JS files through the Compiler, you’ll end up with smaller and more efficient code.

Code obfuscation. Because the code is highly minimized and optimized, Closure Compiler implicitly obfuscates JavaScript to a state that’s nearly impossible to decode. This helps protecting your code, especially when using “Advanced code optimizations”. Nevertheless, care is required when using these options as it can create buggy code.

Summing up, the Closure Compiler helps you find syntax errors during development (rather than during testing) and identify potentially buggy code patterns. This makes it a very valuable tool in Phonegap/Cordova application. development. In addition, the Closure Compiler meets or exceeds the compression factors of other code minification tools. This improves startup and running times of your applications, making your code highly efficient.