Problems Supporting Android Camera Api And Camera2 Api

I’m helping to develop an app that needs to support camera usage across all android versions without using anything deprecated. This obviously means I need to use the new Camera2 API for Android 5.0+ (Api Level 21).

Today I converted all our Original Camera Code to support the Camera2 API, checking Build.VERSION.SDK_INT to decide when to use the original code and when to use the new code. My current problem arose when I went back to test on android 4.x to make sure nothing was broken, I was getting java.lang.VerifyError and rejected opcode errors – things I have never seen before.

I have boiled my problem down to not understanding why the following code crashes when run on Android 4.4:

public class CameraActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera);
} void thisIsNeverRun()
{ if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // set to null so we can compile and run easily. // Doesn’t Matter as the code is never actually executed CameraDevice d = null; // calling getId as an example, anything will break it d.getId(); }
}
}
Notice that thisIsNeverRun() is never called. When run on an android 4.4 Device a crash occurs and logcat produces this output: :46:21. /com.example.android.camera2basic I/dalvikvm: Could not find method android.hardware.camera2.CameraDevice.getId, referenced from method com.example.android.camera2basic.CameraActivity.thisIsNeverRun :46:21. /com.example.android.camera2basic W/dalvikvm: VFY: unable to resolve virtual method 689: Landroid/hardware/camera2/CameraDevice;.getId ()Ljava/lang/String; :46:21. /com.example.android.camera2basic W/dalvikvm: VFY: rejecting opcode 0x6e at 0x :46:21. /com.example.android.camera2basic W/dalvikvm: VFY: rejected Lcom/example/android/camera2basic/CameraActivity;.thisIsNeverRun ()V :46:21. /com.example.android.camera2basic W/dalvikvm: Verifier rejected class Lcom/example/android/camera2basic/CameraActivity; :46:21. /com.example.android.camera2basic W/dalvikvm: Class init failed in newInstance call (Lcom/example/android/camera2basic/CameraActivity;) :46:21. /com.example.android.camera2basic D/AndroidRuntime: Shutting down VM :46:21. /com.example.android.camera2basic W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41c7bdb8) :46:21. /com.example.android.camera2basic E/AndroidRuntime: FATAL EXCEPTION: main :46:21. /com.example.android.camera2basic E/AndroidRuntime: Process: com.example.android.camera2basic, PID: :46:21. /com.example.android.camera2basic E/AndroidRuntime: java.lang.VerifyError: com/example/android/camera2basic/CameraActivity
This is very confusing to me as I have never seen these types of errors before. Ive tested this on 2 different android 4.4 devices. I am admittedly new to supporting deprecated together with new API’s in the same app but it seems fairly straightforward. Maybe I’m doing something obviously wrong?

At this point I’m pretty frustrated as any Activity or fragment containing Camera2 code crashes on Android 4.4 (and I assume other versions) regardless of whether or not the code is actually run or not. I think I can make it work by keeping camera code for devices 21 in separate fragments but I would really like to know why this is not working as it is.