use old copy code on older versions

This commit is contained in:
qimiko committed 2024-01-04 04:08:20 -07:00
1 parent 16ac6cbc0b
commit c421e37e1a
1 file changed
+34 -3
@@ -124,12 +124,43 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
}
private fun loadLibraryFromAssets(libraryName: String) {
// for split apks, loads a library stored inside of the apk
// maybe todo: is there really no way to load these libraries without weird workarounds?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
loadLibraryFromAssetsNative(libraryName)
} else {
loadLibraryFromAssetsCopy(libraryName)
}
}
@SuppressLint("UnsafeDynamicallyLoadedCode")
private fun loadLibraryFromAssetsCopy(libraryName: String) {
// loads a library loaded in assets
// this copies the library to a non-compressed directory
val arch = LaunchUtils.getApplicationArchitecture()
val libraryFd = try {
assets.openNonAssetFd("lib/$arch/lib$libraryName.so")
} catch (_: Exception) {
throw UnsatisfiedLinkError("Could not find library lib$libraryName.so for abi $arch")
}
// copy the library to a path we can access
// there doesn't seem to be a way to load a library from a file descriptor
val libraryCopy = File(cacheDir, "lib$libraryName.so")
val libraryOutput = libraryCopy.outputStream()
copyFile(libraryFd.createInputStream(), libraryOutput)
System.load(libraryCopy.path)
return
}
private fun loadLibraryFromAssetsNative(libraryName: String) {
// loads a library loaded in assets (which points to the apk + an offset)
// these libraries are available to the application after merging
// find the first instance of the library in preferred abi order
val arch = LaunchUtils.getApplicationArchitecture()
val libraryFd = try {
assets.openNonAssetFd("lib/$arch/lib$libraryName.so")
} catch (_: Exception) {
@@ -144,7 +175,7 @@ class GeometryDashActivity : AppCompatActivity(), Cocos2dxHelper.Cocos2dxHelperL
}
// after the library is opened in native code, it should be available for loading
// you can read more about the behavior:
// you can read more about the behavior (added in android 11):
// https://android.googlesource.com/platform/libcore/+/7f3eb2e0ac87bdea471bc577380cf50025aebde5/ojluni/src/main/java/java/lang/Runtime.java#1060
System.loadLibrary(libraryName)
}