Skip to content

Commit

Permalink
Allow Soloader to work with system apps
Browse files Browse the repository at this point in the history
Summary:
Fix to allow SoLoader to find .so deps when app is packaged as Prebuilt APK with Android AOSP Source
When an apk has .so files as part of the APK, the Android make system will generate the APK so as to allow .so files to be loaded directly.

Another option is to include the .so in system libs by updating the Android make file but that means that the files are shared across the OS and can cause conflicts with
applications that may want their own version of the .so

Possible fix for issue
#28

How to use:
  SoLoader.init(mContext, SOLOADER_DISABLE_BACKUP_SOSOURCE);
  SoLoader.setSystemLoadLibraryWrapper(
        new SystemLoadLibraryWrapper() {
          Override
          public void loadLibrary(String libName) {
            System.loadLibrary(libName);
          }
        });

Reviewed By: joelmccall

Differential Revision: D15082630

fbshipit-source-id: 68db389bc105929c6cba7a92285c5a3df947f51c
  • Loading branch information
Bhavin Modi authored and facebook-github-bot committed May 1, 2019
1 parent 263af31 commit c1a8c67
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ public class SoLoader {
@GuardedBy("sSoSourcesLock")
private static int sFlags;

private static boolean isSystemApp;

static {
boolean shouldSystrace = false;
try {
Expand Down Expand Up @@ -177,6 +179,7 @@ private static void init(Context context, int flags, @Nullable SoFileLoader soFi
throws IOException {
StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskWrites();
try {
isSystemApp = checkIfSystemApp(context);
initSoLoader(soFileLoader);
initSoSources(context, flags, soFileLoader);
} finally {
Expand Down Expand Up @@ -239,12 +242,8 @@ private static void initSoSources(Context context, int flags, @Nullable SoFileLo
Log.d(TAG, "adding exo package source: " + SO_STORE_NAME_MAIN);
soSources.add(0, new ExoSoSource(context, SO_STORE_NAME_MAIN));
} else {
ApplicationInfo applicationInfo = context.getApplicationInfo();
boolean isSystemApplication =
(applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0
&& (applicationInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 0;
int apkSoSourceFlags;
if (isSystemApplication) {
if (isSystemApp) {
apkSoSourceFlags = 0;
} else {
apkSoSourceFlags = ApkSoSource.PREFER_ANDROID_LIBS_DIRECTORY;
Expand Down Expand Up @@ -429,6 +428,13 @@ private static Method getNativeLoadRuntimeMethod() {
}
}

private static boolean checkIfSystemApp(Context context) {
return (context != null)
&& (context.getApplicationInfo().flags
& (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP))
!= 0;
}

/** Turn shared-library loading into a no-op. Useful in special circumstances. */
public static void setInTestMode() {
setSoSources(new SoSource[] {new NoopSoSource()});
Expand Down Expand Up @@ -545,6 +551,13 @@ public static boolean loadLibrary(String shortName, int loadFlags) throws Unsati
sSoSourcesLock.readLock().unlock();
}

// This is to account for the fact that we want to load .so files from the apk itself when it is
// a system app.
if (isSystemApp && sSystemLoadLibraryWrapper != null) {
sSystemLoadLibraryWrapper.loadLibrary(shortName);
return true;
}

String mergedLibName = MergedSoMapping.mapLibName(shortName);

String soName = mergedLibName != null ? mergedLibName : shortName;
Expand Down

0 comments on commit c1a8c67

Please sign in to comment.