0

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?

Mateus
  • 389
  • 3
  • 20

1 Answers1

0

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Dear, my question is about how can i put camera2 in the same package (APK) using minSdk 16 to avoid another APK with the implementation of camera2 that requires minSdk as 21. . compileSdkVersion is 25. I was implemented the Step #3 as you said. – Mateus Dec 14 '16 at 15:09
  • 1
    @Mateus: "about how can i put camera2 in the same package (APK) using minSdk 16 to avoid another APK" -- that is Option A. My point with Option B is that what you are trying to do may not be worth the effort. "I was implemented the Step #3 as you said" -- then why did you ask the question, since you already implemented it? – CommonsWare Dec 14 '16 at 15:18
  • 1
    @Mateus: For example, I have been using Option A in [this library](https://github.com/commonsguy/cwac-cam2) and its associated apps for over a year. I have tested it on 50+ devices, and the approach works fine. – CommonsWare Dec 14 '16 at 15:25