Skip to content

Commit

Permalink
Centralize the Injector logic to load the native runtime
Browse files Browse the repository at this point in the history
Longer-term, as the native runtime expands, we can always load it as part of AndroidTestEnvironment startup process. However, currently, because it takes
~300ms to load, it's better to keep loading it lazily.

PiperOrigin-RevId: 438318530
  • Loading branch information
hoisie authored and Copybara-Service committed Apr 1, 2022
1 parent 2755a2c commit 1645fef
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
Expand Up @@ -17,9 +17,6 @@
package org.robolectric.nativeruntime;

import android.database.CharArrayBuffer;
import android.database.CursorWindow;
import org.robolectric.pluginapi.NativeRuntimeLoader;
import org.robolectric.util.inject.Injector;

/**
* Native methods for CursorWindow JNI registration.
Expand All @@ -29,9 +26,7 @@
*/
public final class CursorWindowNatives {
static {
Injector injector = new Injector.Builder(CursorWindow.class.getClassLoader()).build();
NativeRuntimeLoader loader = injector.getInstance(NativeRuntimeLoader.class);
loader.ensureLoaded();
DefaultNativeRuntimeLoader.injectAndLoad();
}

private CursorWindowNatives() {}
Expand Down
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.StandardSystemProperty.OS_ARCH;
import static com.google.common.base.StandardSystemProperty.OS_NAME;

import android.database.CursorWindow;
import com.google.auto.service.AutoService;
import com.google.common.io.Files;
import com.google.common.io.Resources;
Expand All @@ -14,13 +15,20 @@
import javax.annotation.Priority;
import org.robolectric.pluginapi.NativeRuntimeLoader;
import org.robolectric.util.PerfStatsCollector;
import org.robolectric.util.inject.Injector;

/** Loads the Robolectric native runtime. */
@AutoService(NativeRuntimeLoader.class)
@Priority(Integer.MIN_VALUE)
public final class DefaultNativeRuntimeLoader implements NativeRuntimeLoader {
private static final AtomicBoolean loaded = new AtomicBoolean(false);

static void injectAndLoad() {
Injector injector = new Injector.Builder(CursorWindow.class.getClassLoader()).build();
NativeRuntimeLoader loader = injector.getInstance(NativeRuntimeLoader.class);
loader.ensureLoaded();
}

@Override
public void ensureLoaded() {
if (!isSupported()) {
Expand Down
Expand Up @@ -16,11 +16,8 @@

package org.robolectric.nativeruntime;

import android.database.sqlite.SQLiteConnection;
import java.util.function.BinaryOperator;
import java.util.function.UnaryOperator;
import org.robolectric.pluginapi.NativeRuntimeLoader;
import org.robolectric.util.inject.Injector;

/**
* Native methods for SQLiteConnection JNI registration.
Expand All @@ -31,9 +28,7 @@
public class SQLiteConnectionNatives {

static {
Injector injector = new Injector.Builder(SQLiteConnection.class.getClassLoader()).build();
NativeRuntimeLoader loader = injector.getInstance(NativeRuntimeLoader.class);
loader.ensureLoaded();
DefaultNativeRuntimeLoader.injectAndLoad();
}

private SQLiteConnectionNatives() {}
Expand Down

0 comments on commit 1645fef

Please sign in to comment.