Skip to content

Commit

Permalink
Support loading native libraries in tests that use Robolectric *and* …
Browse files Browse the repository at this point in the history
…PowerMock

Summary: See the big comment

Reviewed By: strulovich

Differential Revision: D3806708

fbshipit-source-id: 78cd1994851ee0d9473a3c98d3e21bf7e9ce92f0
  • Loading branch information
dreiss authored and Pascal Hartig committed Jan 9, 2018
1 parent 090897a commit 279ec70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
19 changes: 18 additions & 1 deletion java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public class SoLoader {
*/
@Nullable private static StrictMode.ThreadPolicy sOldPolicy = null;

/**
* Wrapper for System.loadLlibrary.
*/
@Nullable private static SystemLoadLibraryWrapper sSystemLoadLibraryWrapper = null;

/**
* Name of the directory we use for extracted DSOs from built-in SO sources (APK, exopackage)
*/
Expand Down Expand Up @@ -239,6 +244,14 @@ public static void deinitForTest() {
sSoSources = null;
}

/**
* Provide a wrapper object for calling {@link System#loadLibrary}.
* This is useful for controlling which ClassLoader libraries are loaded into.
*/
public static void setSystemLoadLibraryWrapper(SystemLoadLibraryWrapper wrapper) {
sSystemLoadLibraryWrapper = wrapper;
}

public static final class WrongAbiError extends UnsatisfiedLinkError {
WrongAbiError(Throwable cause) {
super("APK was built for a different platform");
Expand Down Expand Up @@ -269,7 +282,11 @@ public static synchronized void loadLibrary(String shortName, int loadFlags)
assertInitialized();
} else {
// Not on an Android system. Ask the JVM to load for us.
System.loadLibrary(shortName);
if (sSystemLoadLibraryWrapper != null) {
sSystemLoadLibraryWrapper.loadLibrary(shortName);
} else {
System.loadLibrary(shortName);
}
return;
}
}
Expand Down
10 changes: 10 additions & 0 deletions java/com/facebook/soloader/SystemLoadLibraryWrapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright 2004-present Facebook. All Rights Reserved.

package com.facebook.soloader;

/**
* @see SoLoader#setSystemLoadLibraryWrapper
*/
public interface SystemLoadLibraryWrapper {
void loadLibrary(String libName);
}

0 comments on commit 279ec70

Please sign in to comment.