@ThreadSafe
public class SoLoader
extends java.lang.Object
To load a native library, call the static method loadLibrary(java.lang.String) from the static
initializer of the Java class declaring the native methods. The argument should be the library's
short name.
Note: SoLoader is enabled in the source code by default but disabled for the OSS package via meta-data config. The application could define com.facebook.soloader.enabled metadata entry to override the default behavior.
For example, if the native code is in libmy_jni_methods.so:
class MyClass {
static {
SoLoader.loadLibrary("my_jni_methods");
}
}
Before any library can be loaded SoLoader needs to be initialized. The application using SoLoader should do that by calling SoLoader.init early on app initialization path. The call must happen before any class using SoLoader in its static initializer is loaded.
An example of the meta data entry to enable SoLoader:
<application ...>
<meta-data
tools:replace="android:value"
android:name="com.facebook.soloader.enabled"
android:value="true" />
</application>
| Modifier and Type | Class and Description |
|---|---|
static class |
SoLoader.WrongAbiError |
| Modifier and Type | Field and Description |
|---|---|
static int |
SOLOADER_ALLOW_ASYNC_INIT
Allow deferring some initialization work to asynchronous background threads.
|
static int |
SOLOADER_DISABLE_BACKUP_SOSOURCE
In some contexts, using a backup so source in case of so corruption is not feasible e.g.
|
static int |
SOLOADER_DISABLE_FS_SYNC_JOB
Experiment ONLY: disable the fsync job in soSource
|
static int |
SOLOADER_DONT_TREAT_AS_SYSTEMAPP
Deprecated.
|
static int |
SOLOADER_ENABLE_DIRECT_SOSOURCE
Deprecated.
|
static int |
SOLOADER_ENABLE_EXOPACKAGE
Enable the exopackage SoSource.
|
static int |
SOLOADER_ENABLE_SYSTEMLOAD_WRAPPER_SOSOURCE
Experiment ONLY: use the
SystemLoadWrapperSoSource to instead of the
directApk/Application soSource. |
static int |
SOLOADER_EXPLICITLY_ENABLE_BACKUP_SOSOURCE
For compatibility, we need explicitly enable the backup soSource.
|
static int |
SOLOADER_LOOK_IN_ZIP |
static int |
SOLOADER_SKIP_MERGED_JNI_ONLOAD
Skip calling JNI_OnLoad if the library is merged.
|
static java.lang.String |
TAG |
static java.lang.String |
VERSION |
| Constructor and Description |
|---|
SoLoader() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
areSoSourcesAbisSupported()
This function ensure that every SoSources Abi is supported for at least one abi in
SysUtil.getSupportedAbis
|
static SoSource[] |
cloneSoSources() |
static void |
deinitForTest()
Make shared-library loading delegate to the system.
|
static java.lang.String[] |
getLibraryDependencies(java.lang.String libName)
Gets the dependencies of a library.
|
static java.lang.String |
getLibraryPath(java.lang.String libName)
Gets the full path of a library.
|
static int |
getLoadedLibrariesCount()
Returns count of already loaded so libraries
|
static java.io.File |
getSoFile(java.lang.String shortName)
Returns the so file for the specified library.
|
static int |
getSoSourcesVersion() |
static void |
init(android.content.Context context,
boolean nativeExopackage)
Backward compatibility.
|
static void |
init(android.content.Context context,
int flags) |
static void |
init(android.content.Context context,
int flags,
SoFileLoader soFileLoader)
Initializes native code loading for this app; this class's other static facilities cannot be
used until this
init(android.content.Context, int) is called. |
static boolean |
isInitialized() |
static boolean |
loadLibrary(java.lang.String shortName) |
static boolean |
loadLibrary(java.lang.String shortName,
int loadFlags)
Load a shared library, initializing any JNI binding it contains.
|
static java.lang.String |
makeLdLibraryPath()
Retrieve an LD_LIBRARY_PATH value suitable for using the native linker to resolve our shared
libraries.
|
static void |
prependSoSource(SoSource extraSoSource)
Add a new source of native libraries.
|
static void |
setInTestMode()
Turn shared-library loading into a no-op.
|
static void |
setSystemLoadLibraryWrapper(SystemLoadLibraryWrapper wrapper)
Provide a wrapper object for calling
System.loadLibrary(java.lang.String). |
static java.io.File |
unpackLibraryAndDependencies(java.lang.String shortName)
Unpack library and its dependencies, returning the location of the unpacked library file.
|
static boolean |
useDepsFile(android.content.Context context,
boolean async,
boolean extractToDisk)
Enables the use of a deps file to fetch the native library dependencies to avoid reading them
from the ELF files.
|
public static final java.lang.String TAG
public static java.lang.String VERSION
public static final int SOLOADER_ENABLE_EXOPACKAGE
public static final int SOLOADER_ALLOW_ASYNC_INIT
public static final int SOLOADER_LOOK_IN_ZIP
public static final int SOLOADER_DISABLE_BACKUP_SOSOURCE
public static final int SOLOADER_SKIP_MERGED_JNI_ONLOAD
@Deprecated public static final int SOLOADER_DONT_TREAT_AS_SYSTEMAPP
@Deprecated public static final int SOLOADER_ENABLE_DIRECT_SOSOURCE
public static final int SOLOADER_EXPLICITLY_ENABLE_BACKUP_SOSOURCE
SOLOADER_DISABLE_BACKUP_SOSOURCE, you should only set one of them or none.public static final int SOLOADER_DISABLE_FS_SYNC_JOB
public static final int SOLOADER_ENABLE_SYSTEMLOAD_WRAPPER_SOSOURCE
SystemLoadWrapperSoSource to instead of the
directApk/Application soSource. This could work on the apps w/o superpack or any other so file
compression.public static void init(android.content.Context context,
int flags)
throws java.io.IOException
java.io.IOExceptionpublic static void init(android.content.Context context,
int flags,
@Nullable
SoFileLoader soFileLoader)
throws java.io.IOException
init(android.content.Context, int) is called. This method is idempotent: calls after the first are
ignored.context - application contextflags - Zero or more of the SOLOADER_* flagssoFileLoader - the custom SoFileLoader, you can implement your own loaderjava.io.IOException - IOExceptionpublic static void init(android.content.Context context,
boolean nativeExopackage)
context - application contextnativeExopackage - pass SOLOADER_ENABLE_EXOPACKAGE as flags if truepublic static void setInTestMode()
public static void deinitForTest()
public static void setSystemLoadLibraryWrapper(SystemLoadLibraryWrapper wrapper)
System.loadLibrary(java.lang.String). This is useful for controlling
which ClassLoader libraries are loaded into.wrapper - the wrapper you wanna set@Nullable
public static java.lang.String getLibraryPath(java.lang.String libName)
throws java.io.IOException
libName - the library file name, including the prefix and extension.java.io.IOException - if there is an error calculating the canonical path of libNamepublic static SoSource[] cloneSoSources()
@Nullable
public static java.lang.String[] getLibraryDependencies(java.lang.String libName)
throws java.io.IOException
libName - the library file name, including the prefix and extension.java.io.IOException - if there is an error reading libName@Nullable public static java.io.File getSoFile(java.lang.String shortName)
shortName - Name of library to find, without "lib" prefix or ".so" suffixpublic static boolean loadLibrary(java.lang.String shortName)
public static boolean loadLibrary(java.lang.String shortName,
int loadFlags)
throws java.lang.UnsatisfiedLinkError
shortName - Name of library to find, without "lib" prefix or ".so" suffixloadFlags - Control flags for the loading behavior. See available flags under SoSource (LOAD_FLAG_XXX).java.lang.UnsatisfiedLinkErrorpublic static java.io.File unpackLibraryAndDependencies(java.lang.String shortName)
throws java.lang.UnsatisfiedLinkError
shortName - Name of library to find, without "lib" prefix or ".so" suffixjava.lang.UnsatisfiedLinkErrorpublic static boolean isInitialized()
public static int getSoSourcesVersion()
public static void prependSoSource(SoSource extraSoSource) throws java.io.IOException
extraSoSource - The SoSource to installjava.io.IOException - IOExceptionpublic static java.lang.String makeLdLibraryPath()
public static boolean areSoSourcesAbisSupported()
public static boolean useDepsFile(android.content.Context context,
boolean async,
boolean extractToDisk)
context - - Application context, used to find native deps file in APKasync - - If true, initialization will occur in a background thread and we library loading
will wait for initialization to complete.extractToDisk - - If true, the native deps file will be extract from the APK and written
to disk. This can be useful when the file is compressed in the APK, since we can prevent
decompressing the file every time.public static int getLoadedLibrariesCount()