{"id":12,"date":"2010-02-26T03:42:25","date_gmt":"2010-02-26T08:42:25","guid":{"rendered":"https:\/\/www.falatic.com\/?p=12"},"modified":"2011-05-06T22:18:25","modified_gmt":"2011-05-07T03:18:25","slug":"building-zxing-for-android-part-3-using-eclipse","status":"publish","type":"post","link":"https:\/\/www.falatic.com\/index.php\/12\/building-zxing-for-android-part-3-using-eclipse","title":{"rendered":"Building ZXing for Android part 3 \u2013 Using Eclipse"},"content":{"rendered":"<p>Several iterations of updates below:<\/p>\n<p><!--more--><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Update 1:<\/span> This procedure works on the <\/strong><em>zxing-1.4.zip<\/em><strong> source base.\u00a0 I&#8217;m trying to figure out why it doesn&#8217;t want to work easily with the most current SVN code (e.g., rev 1217 or 1220).\u00a0 I thought it was just some preloading weirdness that caused the Java Stack Overflow errors, then I thought it was Subversion related, but now&#8230; now I&#8217;m not sure what&#8217;s going on but I&#8217;ll let you know if I figure it out.<\/strong><\/p>\n<hr \/>\n<p><strong><span style=\"text-decoration: underline;\">Update 2:<\/span> Eclipse does NOT like the new version of <\/strong><em>\/android\/default.properties<\/em><strong> at all.\u00a0 After you create the project folders but before you try creating the project in Eclipse, edit <\/strong><em>\/android\/default.properties<\/em><strong> and change<\/strong><\/p>\n<p>&nbsp;<\/p>\n<pre style=\"padding-left: 30px;\">target=<strong>android-4<\/strong><\/pre>\n<p><strong>to<\/strong><\/p>\n<pre style=\"padding-left: 30px;\">target=<strong>Google Inc.:Google APIs:4<\/strong><\/pre>\n<p><strong>That seems to prevent the whole Stack Overflow problem.\u00a0 I&#8217;m not sure why this changed or what the more correct thing to do it but there&#8217;s some apparently relevant discussion <a href=\"http:\/\/code.google.com\/p\/zxing\/issues\/detail?id=276\" target=\"_blank\">here<\/a>. Curiously, despite my reversion of this change I do not see any problem in my test builds here, whether in Eclipse or when using the command-line build as outlined previously.<\/strong><\/p>\n<hr \/>\n<p><strong><span style=\"text-decoration: underline;\">Update 3:<\/span> If you comment out the <\/strong>target=<strong> line in that file entirely (using &#8220;#&#8221;) the problem <em>also <\/em>goes away &#8211; be sure to select an SDK level &gt;= 4 though.\u00a0 Eclipse <em>still <\/em>forces you to select a Google APIs target, but if you double-click one of the &#8220;un-selectable&#8221; Android targets (click the text NOT the checkbox!) just right it&#8217;ll apparently take the setting and work OK.\u00a0 Who knows if that works in general &#8211; probably not. With an Android target launching the AVD also goes much more smoothly!<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Question is, what is it that makes Eclipse insist this is an APIs project when default.properties is not giving it any hints? <\/strong><strong><strong>Or i<\/strong>s it a quirk of the SDK? <\/strong><strong>What triggers this, and is it more readily resolved than by doing this?<\/strong><\/p>\n<hr \/>\n<p><strong><span style=\"text-decoration: underline;\">Update 4:<\/span> Last update for tonight. Once a project is in   Eclipse you can go to Properties -&gt; Android for that project and   select a suitable Android target.\u00a0 As long as you don&#8217;t have   dependencies on Google APIs it should be fine.\u00a0 If you <\/strong>\/android\/AndroidManifest.xml<strong> and remove the <\/strong>android:minSdkVersion<strong> setting that problem goes away as well (again, select Android 1.6 SDK level 4 to ensure proper operation).<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Either it&#8217;s Eclipse 3.5.1 or the ADT plugin that&#8217;s messing up here.\u00a0 Regardless, it doesn&#8217;t seem to be any flaw in ZXing.\u00a0 It&#8217;s rather annoying to see Eclipse acting like this&#8230; are other people seeing this sort of bug?<\/strong><\/p>\n<p><span style=\"color: #0000ff;\"><strong><span style=\"text-decoration: underline;\">To  summarize:<\/span> to avoid all these painful problems just plain remove <\/strong>default.properties<strong> and edit <\/strong>AndroidManifest.xml<strong> before you create a new Eclipse project based on the latest source.\u00a0  Ensure you select Android 1.6 SDK level 4 for best results.<\/strong><\/span><\/p>\n<p><strong><span style=\"color: #0000ff;\">Also, when you run the app it&#8217;s best to just start the AVD first, wiping user data, (or at least uninstalling the old version of your app) and being at the home screen before you &#8220;Run As&#8221;.\u00a0 Otherwise, the app&#8217;s display ends up rather funky.<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #0000ff;\">Finally, &#8220;Build Automatically&#8221; may be more trouble than it&#8217;s worth, at least for a larger project such as this.\u00a0 Consider this if your head starts making dents in the wall as you struggle to make sense of Eclipse&#8217;s random &#8220;out of memory&#8221; errors. \ud83d\ude42<br \/>\n<\/span><\/strong><\/p>\n<hr \/>\n<p>In my <a href=\"https:\/\/www.falatic.com\/index.php\/9\/building_zxing_for_android\" target=\"_blank\">previous post<\/a> I referred to a <a href=\"http:\/\/knol.google.com\/k\/alec-go\/modifying-the-android-barcode-scanner\/2vd5zn6va2fqd\/2#\" target=\"_blank\">Knol<\/a> that tells you how to build ZXing for Android (the Barcode Scanner) using the <strong>Eclipse<\/strong> IDE.\u00a0 There are pros and cons to doing this, given that ZXing is normally built from the command line using Ant (and during that build it is optimized using Proguard).<\/p>\n<p>&nbsp;<\/p>\n<p>However, my goal is to learn more about Android programming in general and to make it even more interesting, I&#8217;m doing it from Windows.\u00a0 Having the project working in an IDE is preferable for me when it comes to unraveling functional connections and learning how a (quite useful!) Android app is put together.<\/p>\n<p><!--more--><\/p>\n<p>First of all, that Knol isn&#8217;t all that bad.\u00a0 It helps if you read it carefully (I kept screwing up a particular step), however some of the steps are unclear apparently because the SDK has changed rather substantially since it was first written.\u00a0 I&#8217;ve tried to be ridiculously detailed here so as to eliminate any possible confusion for the novice.<\/p>\n<p>Second, it&#8217;s not clear that you need to install <em>anything<\/em> other than the <a href=\"http:\/\/www.eclipse.org\/downloads\/\" target=\"_blank\">Eclipse Classic 3.5.1<\/a> IDE and the <a href=\"http:\/\/developer.android.com\/sdk\/index.html\" target=\"_blank\">Android SDK<\/a> to perform this experiment.\u00a0 I have the JDK, Ant and Proguard as well but they aren&#8217;t in my system paths nor is JAVA_HOME set system-wide yet.\u00a0 Therefore I suspect none of that other stuff is getting referenced in this experiment.\u00a0 You should have the IDE installed, the Android SDK installed and up-to-date, and the ADT Plugin for Eclipse (found on the SDK page).<strong> <\/strong><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Initialize the project in Eclipse<\/strong><\/span><\/p>\n<ul>\n<li>Obtain the <strong>ZXing <\/strong>source tree via zip or SVN as described in the previous posts.\u00a0 We are only interested in the <em>\/android<\/em> and the <em>\/core\/src <\/em>trees.\u00a0 The rest of the paths and components may be ignored for the purpose of building the <strong>Barcode Scanner <\/strong>app for Android.<\/li>\n<li>Place these in a working area.\u00a0 For this example I used <em>D:\\workspace\\zxing<\/em><\/li>\n<li><span style=\"text-decoration: underline;\"><strong>An important note: <\/strong><\/span>the Android SDK Content loader doesn&#8217;t immediately start in Eclipse until you attempt to use or create an Adroid project, and it takes a while to load.\u00a0\u00a0 Therefore quickly creating a new Android project based on another Android project <em>while the SDK is loading <\/em>can have unwanted consequences &#8211; particularly, <strong>Stack Overflow<\/strong>s &#8211; which is apparently due to a race condition (if the Android SDK Content Loader is still processing when you pick the &#8220;based on&#8221; tree, bad, bad things happen).\u00a0 It was a very distracting problem until I realized what was going on.<\/li>\n<li>Start the Eclipse IDE\n<ul>\n<li>Close any open projects!<\/li>\n<li> Note that <em>Project -&gt; Build Automatically <\/em>is selected<\/li>\n<\/ul>\n<\/li>\n<li>From the menu select <em>File -&gt; New -&gt; Project&#8230;<\/em><\/li>\n<li>In the new window select <em>Android -&gt; Android Project<\/em><\/li>\n<li>If Build targets are NOT populated at all and there&#8217;s activity in the progress tab:\n<ul>\n<li>Close the <em>New Project <\/em>window.<\/li>\n<li>Wait for the Android SDK Content Loader activity to complete (watch the progress tab!)<\/li>\n<li>From the menu select <em>File -&gt; New -&gt; Project&#8230;<\/em> again<\/li>\n<li>In the new window select <em>Android -&gt; Android Project<\/em> again<\/li>\n<li>At this point the Build Targets should be properly populated and the progress tab should be idle\/empty.<\/li>\n<\/ul>\n<\/li>\n<li>Give the project a name.\u00a0 For this example I used &#8220;<strong>BarExample<\/strong>&#8221; and will refer to that going forward.<\/li>\n<li>Select <em>Create project from existing source<\/em><\/li>\n<li>Browse to where you put the <strong>zxing <\/strong>source tree (this will become your workspace!)<\/li>\n<li>Select the <em>\/android <\/em>subfolder and click OK<\/li>\n<li>The Build Target will automatically be selected as Google APIs 1.6\n<ul>\n<li>I&#8217;m not entirely sure why this happens &#8211; you cannot select Android alone either.\u00a0 I think this is why you are later warned when trying to launch it that there are no compatible devices.<\/li>\n<\/ul>\n<\/li>\n<li>The Application name will be auto-filled as <strong>CaptureActivity<\/strong><\/li>\n<li>Click <em>Finish<\/em><\/li>\n<\/ul>\n<p><strong>Allow it to prebuild.\u00a0 At this point you&#8217;ll have some 250+ errors but the next step should resolve them!<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Add the <em>\/core\/src <\/em>code to your project:<\/strong><\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><em><strong><span style=\"text-decoration: underline;\"><span style=\"text-decoration: line-through;\">Hard way #1:<\/span><\/span><\/strong><br \/>\n<\/em><\/span><\/p>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">In the <em>Package Exp <\/em>tab, right click on the project name (&#8220;BarExample&#8221;) and select <em>New -&gt; Source Folder<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Folder name: <strong>srccore<\/strong><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">&#8220;Update exclusion&#8221; is UNchecked<\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Click <em>OK<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Now, right click on srccore and <em>Import&#8230;<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Select <em>General -&gt; File System<\/em> and click <em>Next<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Select the <em>&#8220;core\\src&#8221; <\/em>folder in the zxing source tree and click <em>OK<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">You should see the From Directory as <em>D:\\workspace\\zxing\\core\\src<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Check the box to the left of the &#8220;src&#8221; folder<\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">&#8220;Into folder&#8221; should already be set to <strong><em>BarExample\/srccore<\/em><\/strong><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">In Options, select <em>Create selected folders only<\/em><\/span><\/span><\/li>\n<li style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">Click <em>Finish<\/em><\/span><\/span><\/li>\n<p style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><em><strong><span style=\"text-decoration: underline;\"><span style=\"text-decoration: line-through;\">Hard way #2:<\/span><\/span><\/strong><\/em><\/span><\/p>\n<p style=\"padding-left: 30px;\"><span style=\"color: #808080;\"><span style=\"text-decoration: line-through;\">A somewhat easier way to do this (avoiding the extra copy) is to put <em>\/core\/src<\/em> <span style=\"text-decoration: underline;\">under<\/span><em> <\/em><em>\/android<\/em> (thus creating <em>\/android\/core\/src<\/em>) <strong>before <\/strong>you initialize the project!\u00a0 Then, <strong>after <\/strong>you initialize the project you can pick the <em>src <\/em>folder under the <em>core <\/em>entry in the project items and select <em>Build Path -&gt; Use as a Source Folder<\/em> to have the desired effect (it&#8217;ll crete an entry called <em>core\/src<\/em>).\u00a0 I would expect a symlink, correctly applied, would also have the desired effect.\u00a0 As you can see, the <em>android <\/em>folder itself is really superfluous here.\u00a0 [I still think there&#8217;s got to be an even easier way to gather this all together, without moving ANYTHING around.]<\/span><\/span><\/p>\n<p><strong>The best way to add <em>\/core\/src <\/em>to your project!<\/strong> No symlinks required (and being Windows it&#8217;s possible but not easy and besides this is even more minimalist) and it should work seamlessly even if you&#8217;re doing thing with Subversion-controlled code.<\/p>\n<p>You&#8217;ve already created a project area called zxing (or whatever) and in it are the \/android tree and the \/core\/src tree. Rather than manually or automatically move ANY code, simply do this:<\/p>\n<ul>\n<li>In the <em>Package Exp <\/em>tab, right click on the project name  (&#8220;BarExample&#8221;) and select <em>Build Path -&gt; Link Source&#8230;<\/em><\/li>\n<li>Browse to and select the <strong><em>src <\/em><\/strong>folder in <strong><em>core<\/em><\/strong>.<\/li>\n<li>Change <em>Folder name<\/em> to <strong><em>srccore <\/em><\/strong>(or any other not-in-use folder name you prefer).<\/li>\n<li>Select <em>Finish<\/em><\/li>\n<li>Let it rebuild automagically<\/li>\n<\/ul>\n<p><strong>If you were successful, you should see a whole mess of warnings (I see over 170) &#8211; but no errors!<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>To install and test this on the Android Virtual Device:<\/strong><\/span><\/p>\n<ul style=\"padding-left: 30px;\">\n<li>Go to the Android SDK and AVD Manager and start your test device\n<ul>\n<li>I previously created a Droid-compatible device configured as Platform 2.0.1 API Level 6, with a WVGA854 display and a 32M SD card.<\/li>\n<li>Select the device and click<em> Start&#8230;<\/em><\/li>\n<li>To avoid weirdness I elected to run with a clean slate and so I selected the &#8220;Wipe user data&#8221; option<\/li>\n<li>The virtual device should start up.<\/li>\n<li>You can (and probably should) close the AVD Manager at this point. Leaving the manager on led to an odd &#8220;GC overhead limit exceeded&#8221; error.<\/li>\n<li>Wait patiently for the main screen to appear (it seems to take quite a while and a lot of CPU &#8211; is it just me?)<\/li>\n<li>You shouldn&#8217;t have any other versions of this Barcode app installed at this point.\u00a0 If you DO you should probably uninstall them first!<\/li>\n<\/ul>\n<\/li>\n<li>Now, back to Eclipse!\n<ul>\n<li>In your project, select <em>Run As -&gt; Android Application<\/em><\/li>\n<li>At this point I get an AVD error telling me &#8220;No compatible targets were found&#8221;.<\/li>\n<li>I selected &#8220;No&#8221; (I don&#8217;t want to create a new target &#8211; I already have one!)<\/li>\n<li>In the Device Chooser that follows I selected my running 2.0.1 emulator and clicked OK.<\/li>\n<li>(I don&#8217;t know why this happens &#8211; it&#8217;s probably the mismatch between my 2.0.1. AVD and the original APIs 1.6 target.)<\/li>\n<\/ul>\n<\/li>\n<li>The app should upload, install and execute on the virtual device.<\/li>\n<li>Be sure to rotate your local gravitational reference clockwise by 90\u00b0 for the  optimum viewing experience.\u00a0 See the January, 2153 quantum multicast of <em>Make:  The Virtual Magazine <\/em>for info on how to construct a portable  graviton generator for this purpose.<\/li>\n<li>It&#8217;s not terribly interesting at this point but should work and look normal (not wiping user data and\/or letting it launch the app before the screen is &#8220;unlocked&#8221; had something to do with the display looking rather weird &#8211; I&#8217;m not sure what was going wrong).<\/li>\n<li>(Really, why doesn&#8217;t the AVD have a rotate function?\u00a0 It seems  pretty fundamental.\u00a0 Likewise some way to fake camera input &#8211; do tell if  I&#8217;m missing a simple way to do either!)<\/li>\n<\/ul>\n<p><strong>Like I&#8217;ve mentioned, I got various odd little IDE errors as I made my way through all this&#8230; perhaps I&#8217;m doing something that&#8217;s not entirely right or the Eclipse IDE \/ Android SDK combination isn&#8217;t very stable.<br \/>\n<\/strong><\/p>\n<p>But <em>will it blend<\/em>?\u00a0 That is, will it work on a physical device?\u00a0 Heck yeah!<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>To install and test this on an Android physical device:<\/strong><\/span><\/p>\n<ul style=\"padding-left: 30px;\">\n<li>First, uninstall the current Barcode Scanner from your phone (you shouldn&#8217;t need to do it each time you cycle through your private test builds, but you DEFINITELY need to if you are switching between the officially signed to the unsigned or debug signed versions!\u00a0 In short: if you get an installation error uninstall the app and try intalling it again!<\/li>\n<li> <strong>Note:<\/strong> for some bizarre reason after installation the Barcode app may be out of order alphabetically in the Manage applications dialog!\u00a0 Closing the dialog and going back to it via Settings rectifies this.<\/li>\n<li>Copy the application&#8217;s APK from<em> \/android\/bin<\/em> to your phone<\/li>\n<li>Install the application<\/li>\n<li>Run the Barcode Scanner!<\/li>\n<\/ul>\n<p><strong>That&#8217;s about it!\u00a0 I&#8217;d expect this to work just as well from a Linux host but I haven&#8217;t tested it.<\/strong><\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Caveats:<\/strong><\/span><\/p>\n<ul style=\"padding-left: 30px;\">\n<li><strong>Proguard <\/strong>isn&#8217;t being used in this configuration AFAIK.\u00a0 Therefore, things aren&#8217;t optimized (I&#8217;m not yet clear on why that&#8217;s necessary &#8211; presumably for performance reasons though it seemed to work OK when I tried it).<\/li>\n<li>AFAIK this is NOT how the ZXing project is normally developed, so if you plan to submit changes to it or otherwise assist with the project you should defer to the project&#8217;s <a href=\"http:\/\/code.google.com\/p\/zxing\/wiki\/GettingStarted\" target=\"_blank\">standards and procedures<\/a>.<\/li>\n<li>This procedure worked fine for me but it may or may not work as well for you, and there may be unexpected side-effects (I haven&#8217;t seen any yet but you never know).<\/li>\n<\/ul>\n<!-- wpsso rrssb get buttons: buttons on archive option not enabled -->\n","protected":false},"excerpt":{"rendered":"<p>Several iterations of updates below:<\/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,30,28,27,117,25],"class_list":["entry","author-marty","has-more-link","post-12","post","type-post","status-publish","format-standard","category-uncategorized","tag-android","tag-ant","tag-barcode-scanner","tag-building","tag-droid","tag-eclipse","tag-subversion","tag-success","tag-windows","tag-zxing"],"_links":{"self":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/12","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=12"}],"version-history":[{"count":0,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/posts\/12\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/media?parent=12"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/categories?post=12"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.falatic.com\/index.php\/wp-json\/wp\/v2\/tags?post=12"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}