/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

package org.tensorflow.lite;

import org.checkerframework.checker.nullness.qual.Nullable;

/** Static utility methods loading the TensorFlowLite runtime. */
public final class TensorFlowLite {

  private static final String LIBNAME = "tensorflowlite_jni";
  private static final String ALTERNATE_LIBNAME = "tensorflowlite_jni_stable";

  private static final @Nullable Throwable LOAD_LIBRARY_EXCEPTION;
  private static volatile boolean isInit = false;

  static {
    // Attempt to load the default native libraries. If unavailable, cache the exceptions;
    // the client may choose to link the native deps into their own custom native library.
    Throwable loadLibraryException = null;
    try {
      System.loadLibrary(LIBNAME);
    } catch (UnsatisfiedLinkError e1) {
      try {
        System.loadLibrary(ALTERNATE_LIBNAME);
      } catch (UnsatisfiedLinkError e2) {
        // Don't both saving the second UnsatisfiedLinkError e2;
        // it will be almost identical to the first one (e1).
        loadLibraryException = e1;
      }
    }
    LOAD_LIBRARY_EXCEPTION = loadLibraryException;
  }

  private TensorFlowLite() {}

  /**
   * Returns the version of the underlying TensorFlowLite model schema.
   *
   * @deprecated Prefer using {@link #runtimeVersion() or #schemaVersion()}.
   */
  @Deprecated
  public static String version() {
    return schemaVersion();
  }

  /** Returns the version of the underlying TensorFlowLite runtime. */
  public static String runtimeVersion() {
    init();
    return nativeRuntimeVersion();
  }

  /** Returns the version of the underlying TensorFlowLite model schema. */
  public static String schemaVersion() {
    init();
    return nativeSchemaVersion();
  }

  /**
   * Ensure the TensorFlowLite native library has been loaded.
   *
   * <p>If unsuccessful, throws an UnsatisfiedLinkError with the appropriate error message.
   */
  public static void init() {
    if (isInit) {
      return;
    }

    try {
      // Try to invoke a native method (which itself does nothing) to ensure that native libs are
      // available.
      nativeDoNothing();
      isInit = true;
    } catch (UnsatisfiedLinkError e) {
      // Prefer logging the original library loading exception if native methods are unavailable.
      Throwable exceptionToLog = LOAD_LIBRARY_EXCEPTION != null ? LOAD_LIBRARY_EXCEPTION : e;
      UnsatisfiedLinkError exceptionToThrow =
          new UnsatisfiedLinkError(
              "Failed to load native TensorFlow Lite methods. Check that the correct native"
                  + " libraries are present, and, if using a custom native library, have been"
                  + " properly loaded via System.loadLibrary():\n"
                  + "  "
                  + exceptionToLog);
      exceptionToThrow.initCause(e);
      throw exceptionToThrow;
    }
  }

  private static native void nativeDoNothing();

  private static native String nativeRuntimeVersion();

  private static native String nativeSchemaVersion();
}

@@ -890,118 +890,471 @@
 ring
- 
+%5B%5D%5B%5D TFLITE_RUNTIME_
 LIBNAME
+S
  =
- %22tensorflowlite_jni%22;%0A  private static final String ALTERNATE_LIBNAME = %22tensorflowlite_jni_stable%22
+%0A      new String%5B%5D%5B%5D %7B%0A        // We load the first library that we find in each group.%0A        new String%5B%5D %7B%0A          // Regular TF Lite.%0A          %22tensorflowlite_jni%22, // Full library, including experimental features.%0A          %22tensorflowlite_jni_stable%22, // Subset excluding experimental features.%0A        %7D,%0A        new String%5B%5D %7B%0A          // TF Lite from system.%0A          %22tensorflowlite_jni_gms_client%22%0A        %7D%0A      %7D
 ;%0A%0A 
@@ -1512,33 +1512,83 @@
 the 
-default native libraries.
+TF Lite runtime's JNI library, trying each alternative name in turn.%0A    //
  If 
@@ -1602,18 +1602,27 @@
 able, ca
+t
 ch
+ and sav
 e the ex
@@ -1632,17 +1632,12 @@
 tion
-s;%0A    //
+(s);
  the
@@ -1673,16 +1673,23 @@
 e native
+%0A    //
  deps in
@@ -1722,16 +1722,90 @@
  library
+, so it's not an error if the default library names%0A    // can't be loaded
 .%0A    Th
@@ -1849,85 +1849,90 @@
     
-try %7B%0A      System.loadLibrary(
+for (String%5B%5D group : TFLITE_RUNTIME_
 LIBNAME
-);
+S) %7B
 %0A    
-%7D catch (UnsatisfiedLinkError e1
+  for (String libName : group
 ) %7B%0A
@@ -1933,30 +1933,34 @@
 up) %7B%0A      
+ 
+ 
 try %7B%0A
+  
         Syst
@@ -1978,28 +1978,37 @@
 ary(
-ALTERNATE_LIBNAME);%0A
+libName);%0A          break;%0A  
     
@@ -2040,17 +2040,16 @@
 kError e
-2
 ) %7B%0A    
@@ -2056,63 +2056,82 @@
     
-// Don't both saving the second UnsatisfiedLinkError
+  if (loadLibraryException == null) %7B%0A            loadLibraryException =
  e
-2
 ;%0A  
@@ -2140,96 +2140,91 @@
     
-// it will be almost identical to the first one (e1).%0A        loadLibraryException = e1;
+  %7D else %7B%0A            loadLibraryException.addSuppressed(e);%0A          %7D%0A        %7D
 %0A   

Change TensorFlowLite so that if loading of "tensorflowlite_jni" and "tensorflowlite_jni_stable" doesn't succeed, it will try loading in "tensorflowlite_jni_gms_client".