Updated based on feedback from the ZXing team.
Tonight I decided to try my hand again at building an Android app – this time, one that should in theory be easy and yet feature-rich enough to learn from. It was far from the simple experiment it ought to have been, so I’m sharing my experience to hopefully save you the reader some time if you try this yourself!
ZXing is the heart of the Barcode Scanner app for Android. It’s nifty though it doesn’t take full advantage of my Droid’s camera resolution and there are some other tricks I’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’t supported in the Droid build of this, at least as of Barcode Scanner v3.1). 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.
I’d recently moved some install folders around so I decided to install the necessary parts from scratch. Note that I first spent an hour or two trying to use Eclipse 3.5.1 to build this but haven’t gotten it to work (yet). When I decided to try doing this via “basic principles” it quickly became apparent that this would take a while.
I ran into several issues I won’t bore you with here. Suffice to say the answers are scattered around Google, either in the ZXing comments, discussions, or its bugs area, and sometimes completely elsewhere. It became clear that while Windows is supported as a host it’s probably not the primary host they test builds from, so their instructions aren’t very current or coherent for it needed some tweaks, which they’ve made. (Windows isn’t the most common way to build for Android but it’s quite do-able just the same.)
Here, then, is the distillation of what I learned in order to successfully build the Barcode Scanner app for Android via Windows. This should apply to other Windows versions as well, at least back to XP. If you are using an x86-based Windows then use x86 installers instead of x64 installers (where there’s a difference).
# This is the setup for a Windows 7 host (64-bit on an Intel CPU) # Install Sun JDK (jdk-6u18-windows-x##): - Source: http://java.sun.com/javase/downloads/index.jsp - Select the JDK appropriate to your Windows host (x64 or x86) - My root folder: "C:\Program Files\Java\jdk1.6.0_18" # Install Sun WTK 2.5.2 (sun_java_wireless_toolkit-2.5.2_01-win.exe): - Yes, you need this too! - Source: http://java.sun.com/products/sjwtoolkit/ - Select Sun Java Wireless Toolkit 2.5.2_01 for CLDC - My root folder: "C:\WTK2.5.2_01" # WTK is NOT required for the Android target, but part of its folder structure IS, as follows. # Install ProGuard Java Optimizer (proguard4.4.zip): - Source: http://sourceforge.net/projects/proguard/ - My root folder: "D:\Programming\proguard4.4" # Copy the proguard.jar file to the expected location in WTK: copy D:\Programming\proguard4.4\lib\proguard.jar C:\WTK2.5.2_01\bin - If the "C:\WTK2.5.2_01\bin" tree doesn't exist, simply create it and populate it as above (again, WTK itself is not required for the Android target) # Install Apache Ant (apache-ant-1.8.0-bin): - Source: http://ant.apache.org/bindownload.cgi - My root folder: "D:\Programming\apache-ant-1.8.0" # Install Google Android SDK (android-sdk_r04-windows): - Source: http://developer.android.com/sdk/index.html - My root folder: "D:\Programming\android-sdk-windows" # Run "android-sdk-windows/SDK Setup": - Update all packages! # Fix "dx.bat" for Android SDK pre-2.0 (it's broken in those): - More info in this thread cd /d D:\Programming\android-sdk-windows copy /y platforms\android-1.1\tools\dx.bat platforms\android-1.1\tools\dx.bat.ORIG copy /y platforms\android-2.0\tools\dx.bat platforms\android-1.1\tools copy /y platforms\android-1.5\tools\dx.bat platforms\android-1.5\tools\dx.bat.ORIG copy /y platforms\android-2.0\tools\dx.bat platforms\android-1.5\tools copy /y platforms\android-1.6\tools\dx.bat platforms\android-1.6\tools\dx.bat.ORIG copy /y platforms\android-2.0\tools\dx.bat platforms\android-1.6\tools # Install the ZXing codebase (ZXing-1.4.zip): - Source: http://code.google.com/p/zxing/downloads/list - My root folder: "D:\Test\zxing-1.4" # Edit "zxing-1.4\build.properties": - Note double-backslashes! - Update: WTK-home=C:\\WTK2.5.2_01 - Update: android-home=T:\\Programming\\android-sdk-windows
Now, if all that went well you should be ready to build it!
# Open a "cmd.exe" window to work in. # Set appropriate environment variables for the tools: set JAVA_HOME=C:\Program Files\Java\jdk1.6.0_18 set path=%PATH%;D:\Programming\android-sdk-windows\tools set path=%PATH%;D:\Programming\apache-ant-1.8.0\bin set path=%PATH%;%JAVA_HOME%\bin # Set the environment variable for the root of your project: set MY_PROJ_ROOT=D:\Programming\workspace\zxing-1.4 # Running the build (repeat as necessary): REM I added this top-level clean step to my process - REM It helps when you have tried building from the wrong level! cd /d %MY_PROJ_ROOT% && ant clean REM Standard build step (see note in android/build.xml) cd /d %MY_PROJ_ROOT%\core && ant clean build-no-debug REM Debug is the default target - I prefer to name it explicitly cd /d %MY_PROJ_ROOT%\android && ant clean debug REM OPTIONAL: You can instead build the release version but it's not signed cd /d %MY_PROJ_ROOT%\android && ant clean release # Inspect and use the build output (the APK file is produced if successful): - %MY_PROJ_ROOT%\android\bin
At this point you should have successfully built the Barcode Scanner app for Android!
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. Then you will need to check the “Settings -> Applications -> Unknown Sources” box on your phone to install a debug binary like this (As always, be careful when testing in this mode!!) Copy the APK to your phone and install it. Voilà! It worked for me.
- Seeing if I can do this from within Eclipse. A somewhat outdated Knol suggests this is possible, but I had no luck with it. Done!
- 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). Done!
- Modifying some code! Done!
Disclaimer: I’m NOT attached to the ZXing project at all, I’m just putting this out there in the hopes that it’ll help others have an easier time at this than I did. I probably cannot help you with any issues you have (especially for other host or target platforms).