Search code examples
androidjavafxgpsgluon

Trying to access GPS with Gluon, but app shuts down unexpectedly


I had been trying to know if my GPS state is enabled, and if not it will enable and give me the location coordinates. But whenever I launch the app and click on the button to get coordinates, the app shuts down unexpectedly.

My code is as below:

button.setOnAction(e->{
        PositionService positionService = Services.get(PositionService.class).orElseThrow(() -> new RuntimeException("PositionService not available."));
        positionService.positionProperty().addListener((obs, ov, nv) -> MobileApplication.getInstance().showMessage("Latest known GPS coordinates from device: " + nv.getLatitude() + ", " + nv.getLongitude()));
    }); 

I found the code in Gluon Mobile's documentation.

Here is my build.gradle file.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.javafxports:jfxmobile-plugin:1.3.10'
    }
}

apply plugin: 'org.javafxports.jfxmobile'

repositories {
    jcenter()
    maven {
        url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
    }
}

mainClassName = 'com.gluonapplication.test.GluonTestApplication'

dependencies {
    compile 'com.gluonhq:charm:4.4.1'
    compile 'com.gluonhq:charm-down-common:2.0.0';
    desktopRuntime 'com.gluonhq:charm-down-desktop:2.0.0';
    androidRuntime 'com.gluonhq:charm-down-android:2.0.0';
    iosRuntime 'com.gluonhq:charm-down-ios:2.0.0';
    compile group: 'com.gluonhq', name: 'charm-down-plugin-position', 
    version: '3.7.0'
}

jfxmobile {
    downConfig {
        version = '3.7.0'
        // Do not edit the line below. Use Gluon Mobile Settings in your 
project context menu instead
        plugins 'display', 'lifecycle', 'statusbar', 'storage', 'position'
    }
    android {
        manifest = 'src/android/AndroidManifest.xml'
        androidSdk = 'C:/Users/Mainul/AppData/Local/Android/Sdk'
    }
    ios {
        infoPList = file('src/ios/Default-Info.plist')
        forceLinkClasses = [
                'com.gluonhq.**.*',
                'javax.annotations.**.*',
                'javax.inject.**.*',
                'javax.json.**.*',
                'org.glassfish.json.**.*'
        ]
    }
}

Note: I have also added the Position permission in AndroidManifest.xml file.

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Solution

  • You are mixing up old and new dependencies for Charm Down.

    dependencies {
        compile 'com.gluonhq:charm:4.4.1'
    
        // (1)
        compile 'com.gluonhq:charm-down-common:2.0.0';
        desktopRuntime 'com.gluonhq:charm-down-desktop:2.0.0';
        androidRuntime 'com.gluonhq:charm-down-android:2.0.0';
        iosRuntime 'com.gluonhq:charm-down-ios:2.0.0';
    
        // (2)
        compile group: 'com.gluonhq', name: 'charm-down-plugin-position', version: '3.7.0'
    }
    

    For starters, the latest Charm Down version is 3.7.0, so all the "2.0.0" dependencies in (1) are not required anymore.

    Also, downConfig manages to add the required Charm Down dependencies, so you don't need to include any of them inside dependencies {}, (2).

    Just remove those from (1) and (2):

    dependencies {
        compile 'com.gluonhq:charm:4.4.1'
    } 
    

    Clean and deploy again.