/2012/08/22/Using-Proguard-for-Android-and-Libgdx/
{"item":{"status":"visible","url":"\/2012\/08\/22\/Using-Proguard-for-Android-and-Libgdx\/","trash":"","name":"Using-Proguard-for-Android-and-Libgdx","parent":"","title":"Using Proguard for Android and Libgdx","date":"2012-08-22 17:32:24","filepath":"posts\/20120822_Using-Proguard-for-Android-and-Libgdx.xml.json","type":"post","content":"<p><img style=\"float: left; margin-left: 10px; margin-right: 10px;\" src=\"http:\/\/www.8bitrobot.com\/media\/uploads\/2012\/01\/gear.png\" alt=\"\" width=\"60\" height=\"64\" \/>I decided to finally bite the bullet and get <a href=\"http:\/\/proguard.sourceforge.net\/\">ProGuard<\/a> working for some <a href=\"https:\/\/play.google.com\/store\/apps\/developer?id=Zygmy\">Android apps<\/a>. It was hell getting everything working because of the shear number of moving parts: Android SDK, Eclipse, Libgdx, AdMob\/Google Ads, apk signing, and the shitstorm ProGuard imposes on any project.<\/p>\n<p>This is going to be another Impatient Guide, because I'd rather not relive configuration problems. The amount of time wasted getting technologies configured and working together is a time sink that gets in the way of making <strong>real<\/strong> progress and doing <strong>real<\/strong> work.<\/p>\n<p><strong>Step one:<\/strong> Turn on ProGuard for your Android project.<\/p>\n<ul>\n<li>Navigate to your project. If you are using LibGdx and the standard configuration it will be named something like \"<YourProjectName>-Android\".<\/li>\n<li>Open <em>default.properties<\/em> for editing<\/li>\n<li>Add the line: <em>proguard.config=proguard.cfg<\/em><\/li>\n<li>Save and reopen Eclipse.<\/li>\n<\/ul>\n<p><strong>Step two:<\/strong> Update the Android SDK Location in Eclipse<\/p>\n<p>Did you jump the gun and try to created a signed apk? Did you get this error?<\/p>\n<p style=\"padding-left: 30px;\"><em>\"C:\\Program' is not recognized as an internal or external command, and executable program, or command file.\"<\/em><\/p>\n<p>Well, Proguard doesn't like spaces in paths, so update the Android SDK location to a short path form.<\/p>\n<ul>\n<li>Open a command prompt: WIN+R -> cmd.exe<\/li>\n<li>cd to the Android SDK location... e.g. \"<em>c:\\Program Files (x86)\\Android\\android-sdk<\/em>\"<\/li>\n<li>type: <em>for \/d %I in (*) do @echo %~sI<\/em><\/li>\n<li>Copy the path. e.g. \"<em>C:\\PROGRA~2\\Android\\ANDROI~1<\/em>\"<\/li>\n<li>Paste the short form path in Eclipse: <em>Window -> Preferences -> Android -> SDK Location<\/em><\/li>\n<li>Hit apply or OK<\/li>\n<\/ul>\n<p><strong>Step Three:<\/strong> Tell ProGuard about dependent libraries<\/p>\n<p>Did you try to export an application package again? Did you get errors like this?<\/p>\n<p style=\"padding-left: 30px;\">\"<em>Warning: com.badlogic.gdx.scenes.scene2d.ui.utils.DesktopClipboard: can't find superclass or interface java.awt.datatransfer.ClipboardOwner<\/em>\"<\/p>\n<p>or<\/p>\n<p style=\"padding-left: 30px;\">\"<em>Warning: there were 28 unresolved references to classes or interfaces.<\/em>\"<\/p>\n<p>Well, you have to tell ProGuard dependent jar files, so it won't blow up. Add something like the following to the <em>proguard.cfg<\/em> file in your project directory. Your configuration will depend upon how your project is set up and make sure to use <strong>absolute paths<\/strong>.<\/p>\n<pre>-libraryjars 'C:\\Program Files\\Java\\jre6\\lib\\rt.jar'\n-libraryjars 'C:\\source...\\<YourProjectName>-Android\\libs'\n-libraryjars 'C:\\source...\\<YourProjectName>\\libs'\n-libraryjars 'C:\\source...\\<YourProjectName>\\libs\\GoogleAdMobAdsSdkAndroid-4.1.1'<\/pre>\n<p>That last one is only valid if you use the AdMob SDK.<\/p>\n<p><strong>Step Four: <\/strong>Fix runtime errors resulting from an overzealous ProGuard shrink<\/p>\n<p>Did you export an .apk with joy, but become totally crushed when your application crashed? Did the crashes happen when you clicked on widgets?<\/p>\n<p>It turns out that ProGuard is removing any code that appears to not be referenced\/used. So, any OnClick* handlers specified in your Android layout xml files will be removed. Seriously. Take a look at <em><YourProjectName>-Android\/proguard\/usage.txt.<\/em> That is stuff that was torn out from your code to \"speed\" up performance. Well, it crashes faster. That's for sure.<\/p>\n<p>But you can't blame ProGuard. It can't read every file in your project that is SDK dependent and make the connection. ProGuard is nice, but it's no mindreader.<\/p>\n<p>To fix this, open up <em>proguard.cfg<\/em> again and tell ProGuard to not touch any OnClick handlers:<\/p>\n<pre>-keepclasseswithmembers class * {\n void onClick*(...);\n}<\/pre>\n<p>If you want to make sure your advertisements from Google show up (com.google.ads*), add the following to your config file too:<\/p>\n<pre>-keep public class com.google.ads.** {*;}<\/pre>\n<p>Save it.<\/p>\n<p><strong>Step Five: <\/strong>Export the .apk and test it<\/p>\n<p>You've probably already done this, but for reference:<\/p>\n<ul>\n<li>Right Click on <YourProjectName>-Android -> Android Tools -> Export Signed Application Package...<\/li>\n<li>Complete the form and generate the apk (e.g. <em>yourproject.apk<\/em>)<\/li>\n<li>Make sure the app isn't already installed on your phone<\/li>\n<li>Open a console: <em>apk.exe install yourproject.apk<\/em><\/li>\n<\/ul>\n<p><strong>Conclusion:<\/strong><\/p>\n<p>This article was built upon a previous article called <a href=\"http:\/\/www.8bitrobot.com\/2012\/07\/07\/How-to-obfuscate-and-package-a-libgdx-app-for-distribution\/\">How to Obfuscate and Package a LibGdx App for Distribution<\/a>.<\/p>\n<p>Working with ProGuard is painful, but hopefully your project is now up and running.<\/p>\n<p> <\/p>"}}