[android] - make ReLinker default library loader, create library loader provider for modularisation

This commit is contained in:
tobrun
2019-02-22 11:10:40 +01:00
committed by Łukasz Paczos
parent 5416872b74
commit f66848efaf
8 changed files with 95 additions and 21 deletions

View File

@@ -12,6 +12,7 @@ dependencies {
implementation dependenciesList.supportAnnotations
implementation dependenciesList.supportFragmentV4
implementation dependenciesList.okhttp3
implementation dependenciesList.reLinker
testImplementation dependenciesList.junit
testImplementation dependenciesList.mockito
testImplementation dependenciesList.mockk

View File

@@ -13,12 +13,9 @@ public abstract class LibraryLoader {
private static final String TAG = "Mbgl-LibraryLoader";
private static final LibraryLoader DEFAULT = new LibraryLoader() {
@Override
public void load(String name) {
System.loadLibrary(name);
}
};
private static final LibraryLoader DEFAULT = Mapbox.getModuleProvider()
.createLibraryLoaderProvider()
.getDefaultLibraryLoader();
private static volatile LibraryLoader loader = DEFAULT;
@@ -42,10 +39,11 @@ public abstract class LibraryLoader {
public static void load() {
try {
if (!loaded) {
loaded = true;
loader.load("mapbox-gl");
}
loaded = true;
} catch (UnsatisfiedLinkError error) {
loaded = false;
String message = "Failed to load native shared library.";
Logger.e(TAG, message, error);
MapStrictMode.strictModeViolation(message, error);

View File

@@ -0,0 +1,15 @@
package com.mapbox.mapboxsdk;
/**
* Injects the default library loader.
*/
public interface LibraryLoaderProvider {
/**
* Creates and returns a the default Library Loader.
*
* @return the default library loader
*/
LibraryLoader getDefaultLibraryLoader();
}

View File

@@ -26,4 +26,13 @@ public interface ModuleProvider {
@Nullable
TelemetryDefinition obtainTelemetry();
/**
* Get the concrete implementation of LibraryLoaderProvider
*
* @return a new instance of LibraryLoaderProvider
*/
@NonNull
LibraryLoaderProvider createLibraryLoaderProvider();
}

View File

@@ -5,6 +5,7 @@ import android.support.annotation.Nullable;
import com.mapbox.mapboxsdk.http.HttpRequest;
import com.mapbox.mapboxsdk.maps.TelemetryDefinition;
import com.mapbox.mapboxsdk.module.http.HttpRequestImpl;
import com.mapbox.mapboxsdk.module.loader.LibraryLoaderProviderImpl;
import com.mapbox.mapboxsdk.module.telemetry.TelemetryImpl;
public class ModuleProviderImpl implements ModuleProvider {
@@ -20,4 +21,10 @@ public class ModuleProviderImpl implements ModuleProvider {
public TelemetryDefinition obtainTelemetry() {
return new TelemetryImpl();
}
@NonNull
@Override
public LibraryLoaderProvider createLibraryLoaderProvider() {
return new LibraryLoaderProviderImpl();
}
}

View File

@@ -0,0 +1,58 @@
package com.mapbox.mapboxsdk.module.loader;
import android.content.Context;
import com.getkeepsafe.relinker.ReLinker;
import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.LibraryLoaderProvider;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.exceptions.MapboxConfigurationException;
import com.mapbox.mapboxsdk.log.Logger;
/**
* Concrete implementation of a native library loader.
* <p>
* Uses ReLinker from https://github.com/KeepSafe/ReLinker.
* </p>
*/
public class LibraryLoaderProviderImpl implements LibraryLoaderProvider {
/**
* Creates and returns a the default Library Loader.
*
* @return the default library loader
*/
@Override
public LibraryLoader getDefaultLibraryLoader() {
return new ReLinkerLibraryLoader();
}
/**
* Concrete implementation of a LibraryLoader using ReLinker.
*/
private static class ReLinkerLibraryLoader extends LibraryLoader {
@Override
public void load(String name) {
try {
Context context = Mapbox.getApplicationContext();
ReLinker.log(new LibraryLogger()).loadLibrary(context, name);
} catch (MapboxConfigurationException exception) {
Logger.e(LibraryLogger.TAG, "Couldn't load so file with relinker, application context missing, "
+ "call Mapbox.getInstance(Context context, String accessToken) first");
}
}
/**
* Relinker library loader logger.
*/
private static class LibraryLogger implements ReLinker.Logger {
private static final String TAG = "Mbgl-LibraryLoader";
@Override
public void log(String message) {
Logger.d(TAG, message);
}
}
}
}

View File

@@ -65,7 +65,6 @@ dependencies {
implementation dependenciesList.supportConstraintLayout
implementation dependenciesList.gmsLocation
implementation dependenciesList.reLinker
implementation dependenciesList.timber
debugImplementation dependenciesList.leakCanaryDebug
releaseImplementation dependenciesList.leakCanaryRelease

View File

@@ -4,8 +4,6 @@ import android.app.Application;
import android.os.StrictMode;
import android.text.TextUtils;
import com.getkeepsafe.relinker.ReLinker;
import com.mapbox.mapboxsdk.LibraryLoader;
import com.mapbox.mapboxsdk.MapStrictMode;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.log.Logger;
@@ -25,7 +23,6 @@ import static timber.log.Timber.DebugTree;
*/
public class MapboxApplication extends Application {
private static final String TAG = "MapboxApplication";
private static final String DEFAULT_MAPBOX_ACCESS_TOKEN = "YOUR_MAPBOX_ACCESS_TOKEN_GOES_HERE";
private static final String ACCESS_TOKEN_NOT_SET_MESSAGE = "In order to run the Test App you need to set a valid "
+ "access token. During development, you can set the MAPBOX_ACCESS_TOKEN environment variable for the SDK to "
@@ -38,7 +35,6 @@ public class MapboxApplication extends Application {
if (!initializeLeakCanary()) {
return;
}
initializeLibraryLoader();
initializeLogger();
initializeStrictMode();
initializeMapbox();
@@ -54,15 +50,6 @@ public class MapboxApplication extends Application {
return true;
}
private void initializeLibraryLoader() {
LibraryLoader.setLibraryLoader(new LibraryLoader() {
@Override
public void load(String name) {
ReLinker.log(message -> Logger.v(TAG, message)).loadLibrary(MapboxApplication.this, name);
}
});
}
private void initializeLogger() {
Logger.setLoggerDefinition(new TimberLogger());
if (BuildConfig.DEBUG) {