Ionic Capacitor 3 with @capacitor-community/background-geolocation fails to run in android

I have upgraded to Latest version of ionic 6.16.1 and created a new project with Capacitor 3.

I just added the plugin @capacitor-community/background-geolocation and the sample code in their npm page

When i try to build the Android Application i am getting the following error

[capacitor] FAILURE: Build failed with an exception.
[capacitor]
[capacitor] * Where:
[capacitor] Build file ‘E:\Projects\ionic\background_location\node_modules@capacitor-community\background-geolocation\android\build.gradle’ line: 54
[capacitor]
[capacitor] * What went wrong:
[capacitor] A problem occurred evaluating project ‘:capacitor-community-background-geolocation’.
[capacitor] > Could not get unknown property ‘androidxLocalbroadcastmanagerVersion’ for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
[capacitor]
[capacitor] * Try:
[capacitor] Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
[capacitor]
[capacitor] * Get more help at https://help.gradle.org
[capacitor]
[capacitor] Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
[capacitor] Use ‘–warning-mode all’ to show the individual deprecation warnings.
[capacitor] See Command-Line Interface
[capacitor]
[capacitor] BUILD FAILED in 11m 12s

Can anyone provide some solution for this?

1 Like

This can be fixed by adding these two lines in variales.gradle file & sync now

androidxAppCompatVersion = ‘1.2.0’
androidxLocalbroadcastmanagerVersion=‘1.0.0’

But Still my background geolocation is not working, it’s adding watcher & removing it

Hi,

Thanks for your reply.

For me it got compiled after adding the following lines in variables.gradle

androidxLocalbroadcastmanagerVersion = ‘1.0.0’
playServicesLocationVersion = ‘17.0.0’

androidxAppCompatVersion = ‘1.2.0’ was already there in the generated vairables.gradle file.

But the App automatically closes when opened. When opening the second time it crashes with following error

java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:593)
at com.getcapacitor.-$$Lambda$Bridge$25SFHybyAQk7zS27hTVXh2p8tmw.run(Unknown Source:8)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:224)
at android.os.HandlerThread.run(HandlerThread.java:67)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at com.getcapacitor.PluginHandle.invoke(PluginHandle.java:121)
at com.getcapacitor.Bridge.lambda$callPluginMethod$0$Bridge(Bridge.java:584)
… 5 more
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method ‘void com.equimaps.capacitor_background_geolocation.BackgroundGeolocationService$LocalBinder.removeWatcher(java.lang.String)’ on a null object reference
at com.equimaps.capacitor_background_geolocation.BackgroundGeolocation.removeWatcher(BackgroundGeolocation.java:168)
… 8 more

I suspect that it might be due to Android Studio JDK getting updated from JDK 8 to JDK 11 recently and the changes corresponding to that is not yet handled in the plugin…

Any guidance on this would be very much helpful.

2 Likes

@spbhaskar Just a random guess, but if you update JDK to version 15, does it work? You can download it from here.

I can confirm the plugin works for me with JDK version 15, after adding

androidxLocalbroadcastmanagerVersion = '1.0.0'
playServicesLocationVersion = '17.0.0'

…to variables.gradle.

@priyansh360 Is it possible that you are using the demo code which adds and instantly removes the watcher?

BackgroundGeolocation.addWatcher(
    {
        ...
    },
    function callback(location, error) {
        ...
    }
).then(function after_the_watcher_has_been_added(watcher_id) {
    // When a watcher is no longer needed, it should be removed by calling
    // 'removeWatcher' with an object containing its ID.
    BackgroundGeolocation.removeWatcher({      👈 WATCHER IS REMOVED HERE
                                                   RIGHT AFTER IT WAS ADDED. MAYBE
                                                   COMMENT OUT THIS?
        id: watcher_id
    });
});
1 Like

could you solve it guys?