{"id":9,"date":"2010-02-24T03:24:29","date_gmt":"2010-02-24T08:24:29","guid":{"rendered":"https:\/\/www.falatic.com\/?p=9"},"modified":"2010-02-26T03:46:57","modified_gmt":"2010-02-26T08:46:57","slug":"building-zxing-for-android-part-1","status":"publish","type":"post","link":"https:\/\/www.falatic.com\/index.php\/9\/building-zxing-for-android-part-1","title":{"rendered":"Building ZXing for Android &#8211; lessons learned"},"content":{"rendered":"<p><em><strong>Updated based on feedback from the ZXing team.<\/strong><\/em><\/p>\n<p>Tonight I decided to try my hand again at building an Android app &#8211; this time, one that should in theory be easy and yet feature-rich enough to learn from.\u00a0 It was far from the simple experiment it ought to have been, so I&#8217;m sharing my experience to hopefully save you the reader some time if you try this yourself!<\/p>\n<p><strong><a title=\"ZXing code page\" href=\"http:\/\/code.google.com\/p\/zxing\/\" target=\"_blank\">ZXing<\/a> <\/strong>is the heart of the <strong>Barcode Scanner<\/strong> app for Android.\u00a0 It&#8217;s nifty though it doesn&#8217;t take full advantage of my Droid&#8217;s camera resolution and there are some other tricks I&#8217;d like to see it do (such as activate the flash for improved lighting, replacing the constant autofocus with a forced focus, and perhaps adding some decoding protocols that aren&#8217;t supported in the Droid build of this, at least as of Barcode Scanner v3.1).\u00a0 My main goal was to simply rebuild the app for the Android target via a Windows host, then play around with the code to learn more about Android coding.<\/p>\n<p><strong><a title=\"ZXing code page\" href=\"http:\/\/code.google.com\/p\/zxing\/\" target=\"_blank\"><!--more--><\/a><\/strong><\/p>\n<p>I&#8217;d recently moved some install folders around so I decided to install the necessary parts from scratch.\u00a0 Note that I first spent an hour or two trying to use Eclipse 3.5.1 to build this but haven&#8217;t gotten it to work (yet).\u00a0 When I decided to try doing this via &#8220;basic principles&#8221; it quickly became apparent that this would take a while.<\/p>\n<p>I ran into several issues I won&#8217;t bore you with here.\u00a0 Suffice to say the answers are scattered around Google, either in the ZXing comments, discussions, or its bugs area, and sometimes completely elsewhere.\u00a0 It became clear that while Windows is supported as a host it&#8217;s probably not the primary host they test builds from, so their <a href=\"http:\/\/code.google.com\/p\/zxing\/wiki\/GettingStarted\" target=\"_blank\">instructions<\/a> <em><span style=\"text-decoration: line-through;\">aren&#8217;t very current or coherent for it<\/span><\/em> needed some tweaks, which they&#8217;ve made.\u00a0 (Windows isn&#8217;t the most common way to build for Android but it&#8217;s quite do-able just the same.)<\/p>\n<p><strong>Here, then, is the distillation of what I learned in order to successfully build the Barcode Scanner app for Android via Windows.<\/strong> This should apply to other Windows versions as well, at least back to XP.\u00a0 If you are using an x86-based Windows then use x86 installers instead of x64 installers (where there&#8217;s a difference).<\/p>\n<pre><strong># This is the setup for a Windows 7 host (64-bit on an Intel CPU)\r\n<\/strong>\r\n<strong># Install Sun JDK (jdk-6u18-windows-x##):<\/strong>\r\n - Source: <a href=\"http:\/\/java.sun.com\/javase\/downloads\/index.jsp\" target=\"_blank\">http:\/\/java.sun.com\/javase\/downloads\/index.jsp<\/a>\r\n    - Select the JDK appropriate to your Windows host (x64 or x86)\r\n - My root folder: \"C:\\Program Files\\Java\\jdk1.6.0_18\"\r\n\r\n<span style=\"text-decoration: line-through;\"><strong># Install Sun WTK 2.5.2 (sun_java_wireless_toolkit-2.5.2_01-win.exe)<\/strong>:\r\n - Yes, you need this too!\r\n - Source: <a href=\"http:\/\/java.sun.com\/products\/sjwtoolkit\/\" target=\"_blank\">http:\/\/java.sun.com\/products\/sjwtoolkit\/<\/a>\r\n    - Select Sun Java Wireless Toolkit 2.5.2_01 for CLDC\r\n - My root folder: \"C:\\WTK2.5.2_01\"<\/span>\r\n\r\n<strong># WTK is NOT required for the Android target, but part of its folder structure IS, as follows.<\/strong>\r\n\r\n<strong># Install ProGuard Java Optimizer (proguard4.4.zip):<\/strong>\r\n - Source: <a href=\"http:\/\/sourceforge.net\/projects\/proguard\/\" target=\"_blank\">http:\/\/sourceforge.net\/projects\/proguard\/<\/a>\r\n - My root folder: \"D:\\Programming\\proguard4.4\"\r\n\r\n<strong># Copy the proguard.jar file to the expected location in WTK<\/strong>:\r\n copy D:\\Programming\\proguard4.4\\lib\\proguard.jar C:\\WTK2.5.2_01\\bin\r\n - If the \"C:\\WTK2.5.2_01\\bin\" tree doesn't exist, simply create it and populate it as above\r\n    (again, WTK itself is not required for the Android target)\r\n\r\n<strong># Install Apache Ant (apache-ant-1.8.0-bin):<\/strong>\r\n - Source: <a href=\"http:\/\/ant.apache.org\/bindownload.cgi\" target=\"_blank\">http:\/\/ant.apache.org\/bindownload.cgi<\/a>\r\n - My root folder: \"D:\\Programming\\apache-ant-1.8.0\"\r\n\r\n<strong># Install Google Android SDK (android-sdk_r04-windows)<\/strong>:\r\n - Source: <a href=\"http:\/\/developer.android.com\/sdk\/index.html\" target=\"_blank\">http:\/\/developer.android.com\/sdk\/index.html<\/a>\r\n - My root folder: \"D:\\Programming\\android-sdk-windows\"\r\n\r\n<strong># Run \"android-sdk-windows\/SDK Setup\":<\/strong>\r\n - Update all packages!\r\n\r\n<strong># Fix \"dx.bat\" for Android SDK pre-2.0 (it's broken in those)<\/strong>:\r\n - More info in <a href=\"http:\/\/groups.google.com\/group\/zxing\/browse_thread\/thread\/ca00c576c14dae04\/cd52c5b64a\" target=\"_blank\">this thread<\/a>\r\n cd \/d D:\\Programming\\android-sdk-windows\r\n copy \/y platforms\\android-1.1\\tools\\dx.bat platforms\\android-1.1\\tools\\dx.bat.ORIG\r\n copy \/y platforms\\android-2.0\\tools\\dx.bat platforms\\android-1.1\\tools\r\n copy \/y platforms\\android-1.5\\tools\\dx.bat platforms\\android-1.5\\tools\\dx.bat.ORIG\r\n copy \/y platforms\\android-2.0\\tools\\dx.bat platforms\\android-1.5\\tools\r\n copy \/y platforms\\android-1.6\\tools\\dx.bat platforms\\android-1.6\\tools\\dx.bat.ORIG\r\n copy \/y platforms\\android-2.0\\tools\\dx.bat platforms\\android-1.6\\tools\r\n\r\n<strong># Install the ZXing codebase (ZXing-1.4.zip):<\/strong>\r\n - Source: <a href=\"http:\/\/code.google.com\/p\/zxing\/downloads\/list\" target=\"_blank\">http:\/\/code.google.com\/p\/zxing\/downloads\/list<\/a>\r\n - My root folder: \"D:\\Test\\zxing-1.4\"\r\n\r\n<strong># Edit \"zxing-1.4\\build.properties\":<\/strong>\r\n - Note double-backslashes!\r\n - Update: WTK-home=C:\\\\WTK2.5.2_01\r\n - Update: android-home=T:\\\\Programming\\\\android-sdk-windows\r\n<\/pre>\n<p><strong>Now, if all that went well you should be ready to build it!<\/strong><\/p>\n<pre><strong># Open a \"cmd.exe\" window to work in.<\/strong>\r\n\r\n<strong># Set appropriate environment variables for the tools:<\/strong>\r\n set JAVA_HOME=C:\\Program Files\\Java\\jdk1.6.0_18\r\n set path=%PATH%;D:\\Programming\\android-sdk-windows\\tools\r\n set path=%PATH%;D:\\Programming\\apache-ant-1.8.0\\bin\r\n set path=%PATH%;%JAVA_HOME%\\bin\r\n\r\n<strong># Set the environment variable for the root of your project:<\/strong>\r\n set MY_PROJ_ROOT=D:\\Programming\\workspace\\zxing-1.4\r\n\r\n<strong># Running the build (repeat as necessary):<\/strong>\r\n REM I added this top-level clean step to my process -\r\n REM It helps when you have tried building from the wrong level!\r\n cd \/d %MY_PROJ_ROOT%\u00a0 &amp;&amp;\u00a0 ant clean\r\n\r\n REM Standard build step (see note in android\/build.xml)\r\n cd \/d %MY_PROJ_ROOT%\\core\u00a0 &amp;&amp;\u00a0 ant clean build-no-debug\r\n\r\n REM Debug is the default target - I prefer to name it explicitly\r\n cd \/d %MY_PROJ_ROOT%\\android\u00a0 &amp;&amp;\u00a0 ant clean debug\r\n\r\n REM OPTIONAL: You can instead build the release version but it's not signed\r\n cd \/d %MY_PROJ_ROOT%\\android\u00a0 &amp;&amp;\u00a0 ant clean release\r\n\r\n<strong># Inspect and use the build output (the APK file is produced if successful)<\/strong>:\r\n - %MY_PROJ_ROOT%\\android\\bin\r\n<\/pre>\n<p><strong>At this point you should have successfully built the Barcode Scanner app for Android!<\/strong><\/p>\n<p>Load it upon your emulator or phone and see how it works. To try it on your phone, you will probably need to uninstall the current version.\u00a0 Then you will need to check the <em>&#8220;Settings -&gt; Applications -&gt; Unknown Sources&#8221;<\/em> box on your phone to install a debug binary like this (As always, be careful when testing in this mode!!)\u00a0 Copy the APK to your phone and install it.\u00a0 Voil\u00e0!\u00a0 It worked for me.<\/p>\n<p><strong>Next steps:<\/strong><\/p>\n<ul>\n<li>Seeing if I can do this from within Eclipse.\u00a0 A <a href=\"http:\/\/knol.google.com\/k\/alec-go\/modifying-the-android-barcode-scanner\/2vd5zn6va2fqd\/2#\" target=\"_blank\">somewhat outdated Knol<\/a> suggests this is possible, but I had no luck with it. <a href=\"https:\/\/www.falatic.com\/index.php\/12\/building-zxing-for-android-part-3-using-eclipse\" target=\"_blank\"><strong><em>Done!<\/em><\/strong><\/a><\/li>\n<li>Install a Subversion client to get the latest code and see how that goes (my expertise is with Clearcase and such so this should be interesting). <strong><a title=\"Subversion and ZXing on Windows\" href=\"https:\/\/www.falatic.com\/index.php\/11\/building-zxing-for-android-part-2-subversion\" target=\"_blank\"><em>Done!<\/em><\/a><\/strong><\/li>\n<li>Modifying some code! <strong><a title=\"Subversion and ZXing on Windows\" href=\"https:\/\/www.falatic.com\/index.php\/11\/building-zxing-for-android-part-2-subversion\" target=\"_blank\"><em>Done!<\/em><\/a><\/strong><\/li>\n<\/ul>\n<p><strong><span style=\"text-decoration: underline;\">Disclaimer:<\/span> I&#8217;m NOT attached to the ZXing project at all, I&#8217;m just putting this out there in the hopes that it&#8217;ll help others have an easier time at this than I did.\u00a0 I probably cannot help you with any issues you have (especially for other host or target platforms).<br \/>\n<\/strong><\/p>\n<!-- wpsso rrssb get buttons: buttons on archive option not enabled -->\n","protected":false},"excerpt":{"rendered":"<p>Updated based on feedback from the ZXing team. Tonight I decided to try my hand again at building an Android app &#8211; this time, one that should in theory be <a href=\"https:\/\/www.falatic.com\/index.php\/9\/building-zxing-for-android-part-1\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[1],"tags":[116,23,21,26,24,27,117,25],"class_list":["entry","author-marty","has-more-link","post-9","post","type-post","status-publish","format-standard","category-uncategorized","tag-android","tag-ant","tag-barcode-scanner","tag-building","tag-droid","tag-success","tag-windows","tag-zxing"],"_links":{"self":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}