I want to load custom .so
dynamic for the NaticityActivity
, but get error when NativeActivity.onCreate()
call classLoader.findLibrary("UE4");
this is party of NativeActivity.onCreate()
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() && file.isFile() && 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");