Search code examples
androidcamera2

Only one APK with Camera2 and old camera API


I have an app that uses camera2 API and also the old camera version. The problem is that I need to generate a single APK with minSdkVersion 16 and camera2 does not compile together, for obvious reasons.

I gave a quick fix to the problem by making an exclusive photo app for Camera2. I detect the user's Android version and, if greater than or equal to 5.0 (21), trigger an Intent for this particular app. I could not get an elegant solution to this problem.

So... how can I generate only APK in this case?


Solution

  • Option A: Using Both APIs

    Step #1: Set your compileSdkVersion to 21 or higher. Ideally, set it to 25, for the latest version of Android.

    Step #2: Write code for both APIs.

    Step #3: Only call the code that uses the android.hardware.camera2 classes when the device is running Android 5.0+:

    if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.LOLLIPOP) {
      // use camera2
    }
    else {
      // use Camera
    }
    

    Option B: Only Use the Classic Camera API

    android.hardware.Camera works fine on Android 7.1 and older devices. Until such time as android.hardware.Camera is removed from the Android SDK, just use it. You will not be able to take advantage of any new features offered by the android.hardware.camera2 classes, though.