title: How to handle implementing different APIs for older and newer Android versions?
body: <p>I am writing an app, which uses the older camera API for older android versions and the new camera2/torch API for API 23 and up.
  Basically I determine once if the device is running a new enough Android version, and then switch between the implementations according to that. It boils down to this:</p>
  
  <pre><code>private static final int SDK_VERSION = Build.VERSION.SDK_INT;
  
  private boolean isSdkVersionGreaterThanOrEqualTo(final int sdk) {
      return SDK_VERSION &gt;= sdk;
  }
  
  if (marshmallowOrNewer) { //isSdkVersionGreaterThanOrEqualTo(23);
                              try {
                                  cameraManager.setTorchMode(cameraManager.getCameraIdList()[0], true);
                              } catch (final CameraAccessException e) {
                                  e.printStackTrace();
                              }
                          } else {
                              cameraParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                              camera.setParameters(cameraParameters);
                              camera.startPreview();
                          }
  </code></pre>
  
  <p>Upon running on Gingerbread I get a force close: FATAL EXCEPTION: main
                                                   java.lang.VerifyError
  in this line:</p>
  
  <pre><code> toggleButton.setOnClickListener(new View.OnClickListener() {
  </code></pre>
  
  <p>which opens up the OnClickListener which contains above code and indicates that an API method is not supported in this version of android, which makes sense, but I'm not actually calling it here. How should I handle this situation properly, implementing different APIs for different Android versions, or how do I bypass this veryfication?</p>
  
  <p>Stacktrace:</p>
  
  <pre><code>04-02 13:42:54.413 3815-3815/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   java.lang.VerifyError: t.t.t.MainActivity$1
                                                       at t.t.t.MainActivity.onCreate(MainActivity.java:61)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
                                                       at android.app.ActivityThread.access$1500(ActivityThread.java:123)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
                                                       at android.os.Handler.dispatchMessage(Handler.java:99)
                                                       at android.os.Looper.loop(Looper.java:130)
                                                       at android.app.ActivityThread.main(ActivityThread.java:3835)
                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                       at java.lang.reflect.Method.invoke(Method.java:507)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
                                                       at dalvik.system.NativeStart.main(Native Method)
  </code></pre>