title: How to dynamically load a native shared libraries (.so) for NativeActivity on Android N?
body: <p>I want to load custom <code>.so</code> dynamic for the <code>NaticityActivity</code>, but get error when <code>NativeActivity.onCreate()</code>  call <code>classLoader.findLibrary("UE4");</code></p>
  
  <p>this is party of <code>NativeActivity.onCreate()</code></p>
  
  <pre><code>    BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();
      String path = classLoader.findLibrary(libname);
  
      if (path == null) {
          throw new IllegalArgumentException("Unable to find native library " + libname +
                                             " using classloader: " + classLoader.toString());
      }
  
      byte[] nativeSavedState = savedInstanceState != null
              ? savedInstanceState.getByteArray(KEY_NATIVE_SAVED_STATE) : null;
  
      mNativeHandle = loadNativeCode(path, funcname, Looper.myQueue(),
              getAbsolutePath(getFilesDir()), getAbsolutePath(getObbDir()),
              getAbsolutePath(getExternalFilesDir(null)),
              Build.VERSION.SDK_INT, getAssets(), nativeSavedState,
              classLoader, classLoader.getLdLibraryPath());
  
      if (mNativeHandle == 0) {
          throw new UnsatisfiedLinkError(
                  "Unable to load native library \"" + path + "\": " + getDlError());
      }
      super.onCreate(savedInstanceState);
  
  
      //Hack classLoader nativeLibraryDirectories, add my .so file path
  
  
      UnrealHelper.RequestPermission(this);
  
      UnrealHelper.CopyFile(Environment.getExternalStorageDirectory().getPath() + "/libUE4.so", getFilesDir() + "/libUE4.so");
  
      String TestA = System.mapLibraryName("gnustl_shared");
      //libUE4.so
      String fileName = System.mapLibraryName("UE4");
  
      String TmpVal = "";
      BaseDexClassLoader classLoader = (BaseDexClassLoader) getClassLoader();
      try
      {
          Field pathListField = classLoader.getClass().getSuperclass().getDeclaredField("pathList");
          pathListField.setAccessible(true);
          Object pathListVal  = pathListField.get(classLoader);
          Field nativeLibPathField = pathListVal.getClass().getDeclaredField("nativeLibraryDirectories");
          nativeLibPathField.setAccessible(true);
          Object nativeLibPathVal = nativeLibPathField.get(pathListVal);
          ArrayList nativeLibraryDirectories = (ArrayList)nativeLibPathVal;
          //add my .so path to classLoader
          nativeLibraryDirectories.add(getFilesDir());
          //nativeLibPathField.set(pathListVal, nativeLibraryDirectories);
          //pathListField.set(classLoader, pathListVal);
  
          //ref: https://android.googlesource.com/platform/libcore-snapshot/+/ics-mr1/dalvik/src/main/java/dalvik/system/BaseDexClassLoader.java
          //ref: https://android.googlesource.com/platform/libcore-snapshot/+/ics-mr1/dalvik/src/main/java/dalvik/system/DexPathList.java
          for (Object directory : nativeLibraryDirectories) {
              File file = new File((File)directory, fileName);
              if (file.exists() &amp;&amp; file.isFile() &amp;&amp; file.canRead()) {
                  //is valid
                  TmpVal = file.getPath();
              }
          }
      }
      catch(Exception Exp)
      {
          String ErrorMsg = Exp.toString();
          System.out.print(ErrorMsg);
      }
  
      //test the path added, but got null
      String path = classLoader.findLibrary("UE4");
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/PXrXT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PXrXT.png" alt="enter image description here"></a></p>