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.
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.
| 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_DONT_TREAT_AS_SYSTEMAPP
System Apps ignore PREFER_ANDROID_LIBS_DIRECTORY.
|
static int |
SOLOADER_ENABLE_EXOPACKAGE
Enable the exopackage SoSource.
|
static int |
SOLOADER_LOOK_IN_ZIP |
static int |
SOLOADER_SKIP_MERGED_JNI_ONLOAD
Skip calling JNI_OnLoad if the library is merged.
|
| 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 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 |
getSoSourcesVersion() |
static void |
init(Context context,
boolean nativeExopackage)
Backward compatibility
|
static void |
init(Context context,
int flags) |
static void |
init(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(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 java.lang.String |
makeNonZipPath(java.lang.String localLdLibraryPath) |
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.
|
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
public static final int SOLOADER_DONT_TREAT_AS_SYSTEMAPP
public static void init(Context context,
int flags)
throws java.io.IOException
java.io.IOExceptionpublic static void init(Context context,
int flags,
SoFileLoader soFileLoader)
throws java.io.IOException
init(Context, int) is called. This method is idempotent: calls after the first are
ignored.context - application context.flags - Zero or more of the SOLOADER_* flagssoFileLoader - java.io.IOExceptionpublic static void init(Context context,
boolean nativeExopackage)
public 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.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 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 libNamepublic 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 java.lang.String makeNonZipPath(java.lang.String localLdLibraryPath)
public static boolean isInitialized()
public static int getSoSourcesVersion()
public static void prependSoSource(SoSource extraSoSource) throws java.io.IOException
extraSoSource - The SoSource to installjava.io.IOExceptionpublic static java.lang.String makeLdLibraryPath()
public static boolean areSoSourcesAbisSupported()