4

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:

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic I/dalvikvm: Could not find method android.hardware.camera2.CameraDevice.getId, referenced from method com.example.android.camera2basic.CameraActivity.thisIsNeverRun

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic W/dalvikvm: VFY: unable to resolve virtual method 689: Landroid/hardware/camera2/CameraDevice;.getId ()Ljava/lang/String; 11-12 20:46:21.336 5875-5875/com.example.android.camera2basic W/dalvikvm: VFY:  rejecting opcode 0x6e at 0x0007

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic W/dalvikvm: VFY:  rejected Lcom/example/android/camera2basic/CameraActivity;.thisIsNeverRun ()V

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic W/dalvikvm: Verifier rejected class Lcom/example/android/camera2basic/CameraActivity;

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic W/dalvikvm: Class init failed in newInstance call (Lcom/example/android/camera2basic/CameraActivity;)

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic D/AndroidRuntime: Shutting down VM

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41c7bdb8)

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic E/AndroidRuntime: FATAL EXCEPTION: main

11-12 20:46:21.336 5875-5875/com.example.android.camera2basic E/AndroidRuntime: Process: com.example.android.camera2basic, PID: 5875

11-12 20:46:21.336 5875-5875/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 and devices > 21 in separate fragments but I would really like to know why this is not working as it is.

Ananth
  • 2,597
  • 1
  • 29
  • 39
Droidio
  • 41
  • 2

1 Answers1

0

Camera2 API works only on devices with Android 5.0 and above. For devices with Android 4.4 you will have to use the deprecated Camera API. You can find more information here: https://developer.android.com/guide/topics/media/camera.html#considerations