{"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>. &nbsp;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. &nbsp;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&nbsp;<strong>real<\/strong> work.<\/p>\n<p><strong>Step one:<\/strong> &nbsp;Turn on ProGuard for your Android project.<\/p>\n<ul>\n<li>Navigate to your project. &nbsp;If you are using LibGdx and the standard configuration it will be named something like \"&lt;YourProjectName&gt;-Android\".<\/li>\n<li>Open&nbsp;<em>default.properties<\/em> for editing<\/li>\n<li>Add the line:&nbsp;<em>proguard.config=proguard.cfg<\/em><\/li>\n<li>Save and reopen Eclipse.<\/li>\n<\/ul>\n<p><strong>Step two:<\/strong> &nbsp;Update the Android SDK Location in Eclipse<\/p>\n<p>Did you jump the gun and try to created a signed apk? &nbsp;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 -&gt; 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:&nbsp;<em>for \/d %I in (*) do @echo %~sI<\/em><\/li>\n<li>Copy the path. &nbsp;e.g. \"<em>C:\\PROGRA~2\\Android\\ANDROI~1<\/em>\"<\/li>\n<li>Paste the short form path in Eclipse: <em>Window -&gt; Preferences -&gt; Android -&gt; 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? &nbsp;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. &nbsp;Add something like the following to the&nbsp;<em>proguard.cfg<\/em> file in your project directory. &nbsp;Your configuration will depend upon how your project is set up and make sure to use&nbsp;<strong>absolute paths<\/strong>.<\/p>\n<pre>-libraryjars 'C:\\Program Files\\Java\\jre6\\lib\\rt.jar'\n-libraryjars 'C:\\source...\\&lt;YourProjectName&gt;-Android\\libs'\n-libraryjars 'C:\\source...\\&lt;YourProjectName&gt;\\libs'\n-libraryjars 'C:\\source...\\&lt;YourProjectName&gt;\\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:&nbsp;<\/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? &nbsp;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. &nbsp;So, any OnClick* handlers specified in your Android layout xml files will be removed. &nbsp;Seriously. &nbsp;Take a look at&nbsp;<em>&lt;YourProjectName&gt;-Android\/proguard\/usage.txt.<\/em> &nbsp;That is stuff that was torn out from your code to \"speed\" up performance. &nbsp;Well, it crashes faster. &nbsp;That's for sure.<\/p>\n<p>But you can't blame ProGuard. &nbsp;It can't read every file in your project that is SDK dependent and make the connection. &nbsp;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:&nbsp;<\/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 &lt;YourProjectName&gt;-Android -&gt; Android Tools -&gt; 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>&nbsp;<\/p>"}}
comments powered by Disqus

Lorem Ipsum

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

This page is best viewed with Firefox 3.5+, Chrome 5+, Safari 5+, Opera 10.6+, IE 9+
Copyright © 2014 8bitrobot.com