diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2019-02-22 11:10:40 +0100 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2019-02-25 16:58:15 +0100 |
commit | f66848efaf1efe2ce4088bf56ae7af3a765b9d57 (patch) | |
tree | d7e8234f83ccc36a9ef490a9e62b7f2cbbeb1eb8 /platform/android | |
parent | 5416872b744628341b92046cb57efd6dffb3a377 (diff) | |
download | qtlocation-mapboxgl-f66848efaf1efe2ce4088bf56ae7af3a765b9d57.tar.gz |
[android] - make ReLinker default library loader, create library loader provider for modularisation
Diffstat (limited to 'platform/android')
8 files changed, 95 insertions, 21 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/build.gradle b/platform/android/MapboxGLAndroidSDK/build.gradle index 265bcf81ff..71a9a061ad 100644 --- a/platform/android/MapboxGLAndroidSDK/build.gradle +++ b/platform/android/MapboxGLAndroidSDK/build.gradle @@ -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 diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java index 66047ee934..ee9f4b3b65 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoader.java @@ -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); diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoaderProvider.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoaderProvider.java new file mode 100644 index 0000000000..c9d98bf765 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/LibraryLoaderProvider.java @@ -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(); + +} diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProvider.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProvider.java index 12a10cd86e..9749290eb3 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProvider.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProvider.java @@ -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(); + } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProviderImpl.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProviderImpl.java index c64e91767c..2de111c97a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProviderImpl.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/ModuleProviderImpl.java @@ -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(); + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/loader/LibraryLoaderProviderImpl.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/loader/LibraryLoaderProviderImpl.java new file mode 100644 index 0000000000..fa97a49bb5 --- /dev/null +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/module/loader/LibraryLoaderProviderImpl.java @@ -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); + } + } + } +} diff --git a/platform/android/MapboxGLAndroidSDKTestApp/build.gradle b/platform/android/MapboxGLAndroidSDKTestApp/build.gradle index d20758fad4..22222b0f50 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/build.gradle +++ b/platform/android/MapboxGLAndroidSDKTestApp/build.gradle @@ -65,7 +65,6 @@ dependencies { implementation dependenciesList.supportConstraintLayout implementation dependenciesList.gmsLocation - implementation dependenciesList.reLinker implementation dependenciesList.timber debugImplementation dependenciesList.leakCanaryDebug releaseImplementation dependenciesList.leakCanaryRelease diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java index ab083ddc13..9ade97f91e 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/MapboxApplication.java @@ -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) { |