This may seem to be a duplicate of many existing similar questions. But I've read almost all discussions and none is helping. My goal is to embed zxing library into my application, and scan QRCodes without needing to reference external application. Current platform I'm targeting is android 4.2.

I've completed the following steps over and over :

  1. Download zxing-2.3.0 source
  2. Compiled zxing-2.3.0/core using Maven - got the core-2.3.0.jar
  3. Created a new Project with zxing-2.3.0/android/ , added core-2.3.0.jar to /libs folder , changed switch statements to if{} clause , removed final from CaptureActivity, marked as library, build = captureactivity.jar
  4. Returned back to my original app. Added core-2.3.0.jar and captureactivity.jar to my /libs folder.
  5. Also added libs/captureactivity.jar as a library under Properties/Java Build Path/Libraries/Add JARS/.. (this makes no difference though, i'll get the same error even if i dont)
  6. I created a class called ScannerActivity extending CaptureActivity (Details Below)

When I run the application I see a lot of resource resolve problems from dalvikvm before finally getting the exception. To my understanding/testing zxing library cannot access is local Resource files or static variables when compiled as a library. It'll finally blow up with a NoClassDefFoundError trying to locate the RelativeLayout for CaptureActivity.

Here is a reduced list of error types I'm getting.

01-04 23:30:37.851: W/dalvikvm(9804): VFY: unable to resolve static field 1711 (decode_succeeded) in Lcom/google/zxing/client/android/R$id;
01-04 23:30:37.851: D/dalvikvm(9804): VFY: replacing opcode 0x60 at 0x0011
01-04 23:30:37.851: W/dalvikvm(9804): VFY: unable to resolve static field 1761 (app_name) in Lcom/google/zxing/client/android/R$string;
01-04 23:30:37.851: D/dalvikvm(9804): VFY: replacing opcode 0x60 at 0x0005
..................(more of similar)
01-04 23:30:37.875: I/dalvikvm(9804): DexOpt: unable to optimize static field ref 0x0702 at 0x0e in Lcom/google/zxing/client/android CaptureActivity;.displayFrameworkBugMessageAndExit
01-04 23:30:37.875: I/dalvikvm(9804): DexOpt: unable to optimize static field ref 0x06eb at 0x17 in Lcom/google/zxing/client/android/CaptureActivity;.displayFrameworkBugMessageAndExit
..................(more of similar)
01-04 23:30:43.617: E/AndroidRuntime(9804): FATAL EXCEPTION: main
01-04 23:30:43.617: E/AndroidRuntime(9804): java.lang.NoClassDefFoundError: com.google.zxing.client.android.R$layout
01-04 23:30:43.617: E/AndroidRuntime(9804):     at com.google.zxing.client.android.CaptureActivity.onCreate(CaptureActivity.java:134)
..................(rest of exception stack)


package my.android.application;

import my.android.ordertaker.R;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.widget.Toast;

import com.google.zxing.Result;
import com.google.zxing.client.android.CaptureActivity;

public class ScannerActivity extends CaptureActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanner);
    }

    @Override
    public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
        Toast.makeText(this.getApplicationContext(), "Scanned code " + rawResult.getText(), Toast.LENGTH_LONG);
    }
}

Here is AndroidManifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.android.ordertaker"
    android:versionCode="3"
    android:versionName="3.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- uses-permission android:name="android.permission.ACCESS_WIFI_STATE" / -->
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.VIBRATE" />

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="17" />
    <!-- android:theme="@style/AppTheme" -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomTheme" >

        <activity
            android:name="my.android.application.ScannerActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <!-- service android:name="com.commonsware.cwac.updater.UpdateService"/ -->
        <!-- receiver android:name="com.commonsware.cwac.updater.WakefulReceiver"/ -->

        <receiver android:name="my.android.io.NetworkStatusListener" >
            <intent-filter>
                <action android:name="android.net.conn.CONNECTIVITY_CHANGE" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

What else I've tried : I've played with zxing /android source (captureactivity.jar) by modifying it, tried to remove R.id references with direct values (hard coded), pulled out some of the classes to my own project, changed some source accessors to public, etc, and I had some success with that, I even got the camera working, but I couldn't manage it at the end, the scope is just too big to handle, so I reverted back.

From my experience the problem seems to arise from generated library(captureactivity.jar) not being able to reference it's internal resources, but i might be wrong. My problem seems to be more fundamental than technical. What am I doing wrong?

Related posts

Recent Viewed