josxha / flutter-maplibre

Unofficial MapLibre bindings for Flutter
https://flutter-maplibre.pages.dev
Other
2 stars 1 forks source link

[FEATURE] Make use of jnigen #52

Open josxha opened 3 days ago

josxha commented 3 days ago

Feature Description

JNI allows interoperability between Dart and Java/Kotlin. The package jnigen generates Dart bindings for Java or Kotlin. Currently Dart types of our public API are mapped to pigeon generated types that are mapped to MapLibre SDK types. Using JNI, the public API could be directly mapped to the generated type. No additional type mapping inside Kotlin would be needed. Another big advantage is the possibiliy to make synchonous calls so that a lot of cheap look ups could become sync.

Current limiations are:

Describe alternatives you've considered

Additional context

josxha commented 2 days ago

Update: After way to many hours of struggle I was able to get jnigen working. It's currently pushed to the feat/jnigen branch (especially https://github.com/josxha/flutter-maplibre/tree/038ccc99c9012d80b08a6b5b2628098fded01998 succeeded the code generation). jnigen would be possible to read directly from maven but requires a jar archive. maplibre android (and I think in general android libraries?) only provides only an aar archive. If the library contains no resources the internet says it should be possible to extract and use the classes.jar file form inside the aar archive but that's unfortunally not possible here. The issue https://github.com/dart-lang/native/issues/595 on the jnigen repository hinted a workaround. It's possible to import the aar archive as a module to the /android project. I had no success with the tutorial linked in the issue (https://www.dynamsoft.com/codepool/flutter-programming-android-aar-file.html) but had success using the "new approach" described in https://rommansabbir.com/import-aarjar-in-android-studio. I had some issues with having successful gradle syncs in the buth /android project and /example/android project with linking to the module but the current configuration seems to work. Now, to have successful builds of the example app, we need to add all the previously transitive dependencies to the /android project.

        implementation 'com.google.code.gson:gson:2.11.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 'com.squareup.okhttp3:okhttp:4.12.0'
        testImplementation("org.jetbrains.kotlin:kotlin-test")
        testImplementation("org.mockito:mockito-core:5.13.0")

After that the example app build and jnigen code generation suceeded. Note, that there is no need to use the maven_downloads property.