Flag Count

free counters

Sunday, August 7, 2011

Absorbing 3rd party code (Zxing) in your Android Project

In this text ZXING or ZEBRA CROSSING is the 3rd party library.

Technologies required:

1. Android SDK Tools v10
2. Android platform 2.2( v8)
3. Zebra Crossing Library 1.6
4. Ant v1.8
5. Eclipse (optional)

Why Zebra Crossing 1.6 and not the latest 1.7?

To integrate the Zebra crossing library we need to use Zebra Crossing 1.6. The latest version is 1.7 but its target platform is 9. Target platform for Mobile Print Solution is 8. Hence ZXing 1.6 is the most suitable version for integration.

ZXing code base location?

ZXing 1.6 and ZXing 1.7 are checked in externs section of the repository. Now we should use 1.6. When we upgrade our apps to target platform 9, we can link 1.7 with Mobile Print apps.

Linking ZXing with Mobile Print Apps

Create a svn external link to the zxing/android directory from the Mobile Print Directory. Additionally, core.jar file placed under libs of ZXing extern should be linked with libs section of Mobile Print apps.

How to build the integrated code?

To build the integrated code first we need to link the ZXing as a class project with Mobile Print apps. This will require adding a line in default.properties file of <Your Project>:
This has been done already for the code checked in Zxing.
From command prompt:
To do so go to the location where <Your Project> apps directory is located and run the following command:
android update project --path ./<Your Project> --library /Zing/android
From eclipse:
Please note that if you update the project from command prompt Eclipse will not be able to identify the library project. Therefore for development environment in Eclipse where you need to debug the QrCode you need to manually add the Zxing project in <Your Project>.
Step 1: Import <Your Project> and ZXing projects in your workspace.
Step 2: Right Click on ZXing in project explorer->Properties->Android->Check Is Library(Screen 1)

Screen 1

Step 3: Then,
Right Click on <Your Project> in project explorer->Properties->Android->Click on Add button(screen 2).
Screen 2
Step 4: Select ZXing library project(Screen 3).

Screen 3

Step 5: Click Ok(Screen 4)
Screen 4
This should add a similar tag in build.properties. Additionally eclipse will also update .classpath file and .project file adding some thing like below
<classpathentry kind="src" path="ZXing_src"/>

In the .project file a linkedresouces tag will be created and should have a hardcoded path. To avoid hardcoded path in this file.
Include the core.jar file under libs (<Your Project>) in the build path if it is not already included.
Right Click on <Your Project> in project explorer->Properties->Java Build Path->Source->Select ZXing->Click on Edit(Screen 5).

Screen 5
Select Variables(Screen 6).

Screen 6

Select PROJECT_LOC(Screen 7).

Screen 7

Click on Extend(Screen 8) and set the correct directory. This will remove the hard coded path.

Screen 8

Please note that this linking should be done only for development environment. Even if these updated config files of Eclipse are put in subversion, only checking them out will not link the entire code base properly. It has been tried and didn’t work out for Eclipse. This is a limitation of Eclipse IDE.

Build Errors from ZXing:

If we try to build the source code(v1.6) downloaded from ZXing website there will be some build errors for String.xml files under ZXing. A string namedpreferences_custom_product_search_summary is defined in strings.xml which contains %s. The compiler error that will be thrown is “Multiple Substitutions specified in non-positional format”. To fix this one add an attribute “formatted= "false" ” inside the definition tag. It has already been done in externs section of the repository. This fix is already checked in subversion. Please note that this bug is fixed in ZXing 1.7.

Manifest File Updates and invoking ZXing activity:

To invoke the CaptureActivity? of ZXing we need to add the following in Android manifest file:
<activity android:name= "com.google.zxing.client.android.CaptureActivity"
android:screenOrientation= "landscape"
android:configChanges= "orientation|keyboardHidden"
android:theme= "@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode= "stateAlwaysHidden" >
<action android:name= "com.google.zxing.client.android.SCAN" />
<category android:name= "android.intent.category.DEFAULT" />
Additionally add the following permission:
<uses-permission android:name ="android.permission.CAMERA" />
Please make sure that <Your Activity> from component section is present in
Now if the apps is built from command prompt using build.xml or from Eclipse the apps will run fine and Camera will be invoked.

Building Zebra Crossing as a separate component:

Please note that building the zebra crossing separately is not required for using it in <Your Project>. However, for testing purpose it can be done.
You can check out all the files under Zxing in externs section. Alternatively you can create a folder Zxing and inside it download android directory, core directory, and all the configuration related files.
To build it from command prompt, perform the following steps:
Edit build.properties at the top level of the project, and change the android-home property to point to the SDK install location
Download Proguard, version 4.4 minimum, and install it where you like.
Edit build.properties and set proguard-jar to the full path (including the filename) of the proguard library.
Build core; Note that core must be built without debug symbols to avoid an incompatibility between proguard and the Android tool chain:
cd core
ant clean build-no-debug

Build android:
cd ../android

Please note that the build.xml file provided by ZXing open source will throw some errors as some of the tags used in it are depreciated for later versions of ant, android and proguard.
Copy the core.jar file inside android under a new directory core_jar. This is optional and is done to use the same directory to build the ZXing with <Your Project> from command line as well as eclipse.
From Eclipse: 

First create the core.jar file from command prompt. Place the jar file inside core_jar directory in android. Then import the android project in Eclipse. Remove the “Is Library” tag if it is displayed and trigger the build. This will create the apk file.

Flickr Enjoy photos on http://www.flickr.com/photos/mithunkundu/


  1. Hai Mithun, i am beginer in java programing, your post help me so much, and myproject running well, but i still confusing to load or access some code in zxing, can you explain how to load camera like in barcode.apk (this apk has i append to my phone and running well) Thanks.

    Asep Rohimat - Indonesi