Skip to content

Commit

Permalink
feat: use jnigen for android (#53)
Browse files Browse the repository at this point in the history
## Overview
- [x] generate bindings using jnigen
- [x] integrate into the MapController
- [x] replace some pigeon functions with jni
- [x] fix web compilation errors
- [x] fix proguard for release builds
- [x] bump min flutter version to 3.24.0 (min dart version to 3.5.0)
because meta ^0.15.0 is required by jni.

## Issue
- #52
  • Loading branch information
josxha authored Oct 4, 2024
1 parent 3886761 commit 880dbce
Show file tree
Hide file tree
Showing 206 changed files with 109,438 additions and 52 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ jobs:
strategy:
fail-fast: false
matrix:
sdk: [ '3.22.3', '' ]
sdk: [ '3.24.3', '' ]
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
Expand Down Expand Up @@ -162,7 +162,7 @@ jobs:
# strategy:
# fail-fast: false
# matrix:
# sdk: [ '3.22.3', '' ]
# sdk: [ '3.24.3', '' ]
# steps:
# - uses: actions/checkout@v4
# - uses: subosito/flutter-action@v2
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
strategy:
fail-fast: false
matrix:
sdk: [ '3.22.3', '' ]
sdk: [ '3.24.3', '' ]
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
Expand Down
21 changes: 19 additions & 2 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ apply plugin: "com.android.library"
apply plugin: "kotlin-android"

android {
// Keeping the classes from being removed by proguard.
defaultConfig {
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
}
}

if (project.android.hasProperty("namespace")) {
namespace = "com.github.josxha.maplibre"
}
Expand All @@ -50,10 +60,17 @@ android {
}

dependencies {
implementation 'org.maplibre.gl:android-sdk:11.5.0'
implementation project(":MapLibreAndroid")
implementation 'com.google.code.gson:gson:2.11.0'
// implementation 'org.maplibre.gl:android-sdk:11.5.0'
implementation 'org.maplibre.gl:maplibre-android-gestures:0.0.3'
implementation 'org.maplibre.gl:android-sdk-geojson:6.0.1'
implementation 'org.maplibre.gl:android-sdk-turf:6.0.1'
implementation 'com.jakewharton.timber:timber:5.0.1'
implementation 'androidx.annotation:annotation:1.8.2'
// implementation 'org.maplibre.gl:android-plugin-annotation-v9:3.0.0'
// implementation 'org.maplibre.gl:android-plugin-offline-v9:3.0.0'
// implementation 'com.squareup.okhttp3:okhttp:4.12.0'
implementation 'com.squareup.okhttp3:okhttp:4.12.0'
testImplementation("org.jetbrains.kotlin:kotlin-test")
testImplementation("org.mockito:mockito-core:5.14.1")
}
Expand Down
18 changes: 18 additions & 0 deletions android/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
-keep class org.maplibre.android.attribution.** { public *; }
-keep class org.maplibre.android.camera.** { public *; }
-keep class org.maplibre.android.constants.** { public *; }
-keep class org.maplibre.android.exceptions.** { public *; }
-keep class org.maplibre.android.geometry.** { public *; }
-keep class org.maplibre.android.http.** { public *; }
-keep class org.maplibre.android.location.** { public *; }
-keep class org.maplibre.android.log.** { public *; }
-keep class org.maplibre.android.maps.** { public *; }
-keep class org.maplibre.android.offline.** { public *; }
-keep class org.maplibre.android.storage.** { public *; }
-keep class org.maplibre.android.style.layers.** { public *; }
-keep class org.maplibre.android.style.light.** { public *; }
-keep class org.maplibre.android.style.sources.** { public *; }
-keep class org.maplibre.android.style.types.** { public *; }
-keep class org.maplibre.android.text.** { public *; }
-keep class org.maplibre.android.util.** { public *; }
-keep class org.maplibre.android.utils.** { public *; }
3 changes: 3 additions & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
rootProject.name = 'maplibre'

include ':MapLibreAndroid'
project(':MapLibreAndroid').projectDir = new File('../example/android/MapLibreAndroid')
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import kotlin.coroutines.cancellation.CancellationException


class MapLibreMapController(
viewId: Int,
private val viewId: Int,
private val context: Context,
private val lifecycleProvider: LifecycleProvider,
binaryMessenger: BinaryMessenger
Expand Down Expand Up @@ -112,6 +112,7 @@ class MapLibreMapController(

override fun onMapReady(mapLibreMap: MapLibreMap) {
this.mapLibreMap = mapLibreMap
MapLibreRegistry.addMap(viewId, mapLibreMap)
if (mapOptions.listensOnClick) {
this.mapLibreMap.addOnMapClickListener { latLng ->
flutterApi.onClick(LngLat(latLng.longitude, latLng.latitude)) { }
Expand Down Expand Up @@ -646,4 +647,4 @@ class MapLibreMapController(
mapLibreMap.uiSettings.isTiltGesturesEnabled = options.gestures.tilt
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,4 @@ class MapLibreMapFactory(

interface LifecycleProvider {
fun getLifecycle(): Lifecycle?
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github.josxha.maplibre

import androidx.annotation.Keep
import org.maplibre.android.maps.MapLibreMap

@Keep
object MapLibreRegistry {
private val mapRegistry = HashMap<Int, MapLibreMap>()

@Suppress("RedundantVisibilityModifier")
@Keep
public fun getMap(viewId: Int): MapLibreMap? = mapRegistry[viewId]

fun addMap(viewId: Int, map: MapLibreMap) {
mapRegistry[viewId] = map
}
}
1 change: 1 addition & 0 deletions example/android/MapLibreAndroid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.gradle/
Binary file not shown.
2 changes: 2 additions & 0 deletions example/android/MapLibreAndroid/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configurations.maybeCreate("default")
artifacts.add("default", file('MapLibreAndroid-10.5.0.aar'))
4 changes: 4 additions & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ android {
signingConfig = signingConfigs.debug
}
}

dependencies {
implementation project(":MapLibreAndroid")
}
}

flutter {
Expand Down
1 change: 1 addition & 0 deletions example/android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ plugins {
}

include ":app"
include ':MapLibreAndroid'
10 changes: 4 additions & 6 deletions example/lib/controller_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,11 @@ class _ControllerPageState extends State<ControllerPage> {
bearing: -50,
pitch: 60,
);
debugPrint('animateTo end');
} catch (error) {
final e = error as PlatformException;
debugPrint('flyTo end');
} on PlatformException catch (error) {
debugPrint(
'animateTo cancelled: code: '
'"${e.code}", message: "${e.message}"',
'flyTo cancelled: code: '
'"${error.code}", message: "${error.message}"',
);
}
},
Expand Down Expand Up @@ -149,7 +148,6 @@ pitch: ${camera.pitch}'''),
OutlinedButton(
onPressed: () async {
final region = await _controller.getVisibleRegion();
debugPrint(region.toString());
if (context.mounted) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
Expand Down
1 change: 1 addition & 0 deletions example/linux/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
jni
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: 'none'
version: 1.0.0+1

environment:
sdk: '>=3.4.0 <4.0.0'
sdk: '>=3.5.0 <4.0.0'

dependencies:
flutter:
Expand Down
1 change: 1 addition & 0 deletions example/windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ list(APPEND FLUTTER_PLUGIN_LIST
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
jni
)

set(PLUGIN_BUNDLED_LIBRARIES)
Expand Down
47 changes: 47 additions & 0 deletions jnigen.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
preamble: |
// Autogenerated by jnigen. DO NOT EDIT!
// ignore_for_file: public_member_api_docs
// ignore_for_file: require_trailing_commas
// ignore_for_file: unnecessary_raw_strings
// ignore_for_file: avoid_field_initializers_in_const_classes
// ignore_for_file: avoid_equals_and_hash_code_on_mutable_classes
// ignore_for_file: sort_constructors_first
// ignore_for_file: avoid_positional_boolean_parameters
// ignore_for_file: sort_unnamed_constructors_first
// ignore_for_file: always_use_package_imports
// ignore_for_file: use_late_for_private_fields_and_variables
// ignore_for_file: one_member_abstracts
// ignore_for_file: always_put_required_named_parameters_first
android_sdk_config:
add_gradle_deps: true
android_example: 'example/'
suspend_fun_to_async: true
output:
dart:
path: 'lib/src/jni/'
source_path:
- 'android/src/main'
classes:
# https://github.com/maplibre/maplibre-native/tree/main/platform/android/MapLibreAndroid/src/main/java/org/maplibre/android
- 'android.app.Activity'
- 'android.graphics.PointF'
- 'com.github.josxha.maplibre.MapLibreRegistry'
- 'org.maplibre.android.attribution'
- 'org.maplibre.android.camera'
- 'org.maplibre.android.constants'
- 'org.maplibre.android.exceptions'
- 'org.maplibre.android.geometry'
- 'org.maplibre.android.http'
- 'org.maplibre.android.location'
- 'org.maplibre.android.log'
- 'org.maplibre.android.maps'
- 'org.maplibre.android.offline'
- 'org.maplibre.android.storage'
- 'org.maplibre.android.style.layers'
- 'org.maplibre.android.style.light'
- 'org.maplibre.android.style.sources'
- 'org.maplibre.android.style.types'
- 'org.maplibre.android.text'
- 'org.maplibre.android.util'
- 'org.maplibre.android.utils'
Loading

0 comments on commit 880dbce

Please sign in to comment.