Android Question FusedLocationProvider Error

Status
Not open for further replies.

potman100

Active Member
Licensed User
Longtime User
Hi

I am trying to get this library working, but keep getting the following error when the application is installed on the device.

B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
main_activity_resume (java line: 360)
java.lang.IncompatibleClassChangeError: Found class com.google.android.gms.common.api.GoogleApiClient, but interface was expected (declaration of 'com.google.android.gms.common.api.GoogleApiClient' appears in /data/app/uk.co.martinpearman.b4a.fusedlocationproviderexample-1/base.apk)
    at uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProviderWrapper.Connect(FusedLocationProviderWrapper.java:147)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main._activity_resume(main.java:360)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main.afterFirstLayout(main.java:108)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main.access$000(main.java:17)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)

This is the code I am using as per the example provided with the library.

B4X:
Region  Project Attributes
    #ApplicationLabel: FusedLocationProvider example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    '#AdditionalRes: C:\Android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
    #additionaljar: com.android.support:support-v4
    #AdditionalJar: com.google.android.gms:play-services-location
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Private LastLocation As Location
End Sub

Sub Globals
    Private LastLocationLabel As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    **    IMPORTANT see manifest for required entries    **
    If FirstTime Then
        FusedLocationProvider1.Initialize("FusedLocationProvider1")
    End If
    
    Activity.LoadLayout("Main")

    If LastLocation.IsInitialized Then
        UpdateUI
    End If

End Sub

Sub Activity_Resume
    '    attempt to connect to the location services
    '    after calling Connect we are waiting for either ConnectionFailed or ConnectionSuccess events
    FusedLocationProvider1.Connect
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    FusedLocationProvider1.Disconnect
End Sub

Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
    Log("FusedLocationProvider1_ConnectionFailed")
    
    '    the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
    
    Select ConnectionResult1
        Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
            '    a network error has occurred, this is likely to be a recoverable error
            '    so try to connect again
            FusedLocationProvider1.Connect
        Case Else
            '    TODO handle other errors
    End Select
End Sub

Sub FusedLocationProvider1_ConnectionSuccess
    Log("FusedLocationProvider1_ConnectionSuccess")
    Dim LocationRequest1 As LocationRequest
    LocationRequest1.Initialize
    LocationRequest1.SetInterval(1000)    '    1000 milliseconds
    LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
    LocationRequest1.SetSmallestDisplacement(1)    '    1 meter
    FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End Sub

Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
    
    '    the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
    
    Select SuspendedCause1
        Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
            '    TODO take action
        Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            '    TODO take action
    End Select
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged")
    LastLocation=Location1
    UpdateUI
End Sub

Sub UpdateUI
    LastLocationLabel.Text=DateTime.Time(LastLocation.Time)&" ("&LastLocation.Latitude&", "&LastLocation.Longitude&")"
End Sub


Anyone can shed any light on this error please.

Thanks

Potman
 

potman100

Active Member
Licensed User
Longtime User
Hi Erel

Yes I ran SDK Manager and it offers no other installs after the initial install, I have included

B4X:
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)
    
'    you must add one of these two permissions
'    the permission you add will define the accuracy of location updates
AddPermission(android.permission.ACCESS_FINE_LOCATION)
AddPermission(android.permission.ACCESS_COARSE_LOCATION)

In the manifest file, now even with B4A process time out set to 120, it just hangs and times out when it hits Convert byte code, take nearly 100% of my cpus.
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
After some looking I found the new #MultiDex: True Attribute and changed the MaxRamForDex=2048, once these were set the application will now compile and install in both Release and Debug, but it crashes straight away with the following error

B4X:
java.lang.IncompatibleClassChangeError: Found class com.google.android.gms.common.api.GoogleApiClient, but interface was expected (declaration of 'com.google.android.gms.common.api.GoogleApiClient' appears in /data/app/uk.co.martinpearman.b4a.fusedlocationproviderexample-2/base.apk:classes2.dex)
    at uk.co.martinpearman.b4a.fusedlocationprovider.FusedLocationProviderWrapper.Connect(FusedLocationProviderWrapper.java:147)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main._activity_resume(main.java:427)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main.afterFirstLayout(main.java:108)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main.access$000(main.java:17)
    at uk.co.martinpearman.b4a.fusedlocationproviderexample.main$WaitForLayout.run(main.java:80)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6823)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1557)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1445)
SENTINEL_MSG_LIBCUTILS
SENTINEL_MSG_LIBLOG
** Activity (main) Pause, UserClosed = true **


B4X:
#Region  Project Attributes
    #ApplicationLabel: FusedLocationProvider example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    'AdditionalRes: C:\Android\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
    #additionaljar: com.android.support:support-v4
    #AdditionalJar: com.google.android.gms:play-services
    #MultiDex: True
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

    Sub Process_Globals
    Private FusedLocationProvider1 As FusedLocationProvider
    Private LastLocation As Location
End Sub

Sub Globals
    Private LastLocationLabel As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    '    **    IMPORTANT see manifest for required entries    **
    If FirstTime Then
        FusedLocationProvider1.Initialize("FusedLocationProvider1")
    End If
    
    Activity.LoadLayout("Main")

    If LastLocation.IsInitialized Then
        UpdateUI
    End If

End Sub

Sub Activity_Resume
    '    attempt to connect to the location services
    '    after calling Connect we are waiting for either ConnectionFailed or ConnectionSuccess events
    FusedLocationProvider1.Connect
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    FusedLocationProvider1.Disconnect
End Sub

Sub FusedLocationProvider1_ConnectionFailed(ConnectionResult1 As Int)
    Log("FusedLocationProvider1_ConnectionFailed")
    
    '    the FusedLocationProvider ConnectionResult object contains the various CoonectionResult constants
    
    Select ConnectionResult1
        Case FusedLocationProvider1.ConnectionResult.NETWORK_ERROR
            '    a network error has occurred, this is likely to be a recoverable error
            '    so try to connect again
            FusedLocationProvider1.Connect
        Case Else
            '    TODO handle other errors
    End Select
End Sub

Sub FusedLocationProvider1_ConnectionSuccess
    Log("FusedLocationProvider1_ConnectionSuccess")
    Dim LocationRequest1 As LocationRequest
    LocationRequest1.Initialize
    LocationRequest1.SetInterval(1000)    '    1000 milliseconds
    LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
    LocationRequest1.SetSmallestDisplacement(1)    '    1 meter
    FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End Sub

Sub FusedLocationProvider1_ConnectionSuspended(SuspendedCause1 As Int)
    Log("FusedLocationProvider1_ConnectionSuspended")
    
    '    the FusedLocationProvider SuspendedCause object contains the various SuspendedCause constants
    
    Select SuspendedCause1
        Case FusedLocationProvider1.SuspendedCause.CAUSE_NETWORK_LOST
            '    TODO take action
        Case FusedLocationProvider1.SuspendedCause.CAUSE_SERVICE_DISCONNECTED
            '    TODO take action
    End Select
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged")
    LastLocation=Location1
    UpdateUI
End Sub

Sub UpdateUI
    LastLocationLabel.Text=DateTime.Time(LastLocation.Time)&" ("&LastLocation.Latitude&", "&LastLocation.Longitude&")"
End Sub
Any ideas ?
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi Erel

No, it was a different error, I managed to find the answer, it was the google api url that was wrong, it should have been :

B4X:
Region  Project Attributes
    #ApplicationLabel: FusedLocationProvider example
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
    #additionaljar: com.android.support:support-v4
    #AdditionalJar: com.google.android.gms:play-services-location
    #MultiDex: True
#End Region

Once I changed this all works fine with the above code.

Thanks for you help.

Potman
 
Last edited:
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
hi at all, I have in the Main this code

upload_2018-5-31_12-12-25.png



upload_2018-5-31_12-11-3.png


but I have java 8 installed, but I can´t integrate com.google.android.gms.location....any idea how to make this integration ????

the best regards.
 
Upvote 0

westingenieria

Active Member
Licensed User
Longtime User
another questions...which versions of android I have to use, actually I am using platforms\android-16\android.jar, that it is correct ??? or I need to change to other platforms android version ????

thanks in advanced.
 
Upvote 0

potman100

Active Member
Licensed User
Longtime User
Hi

Sorry for the Delay in getting back to you, I use android-24, so it may be that android-16 does not have the required functionality ??

All the other code from above is the same as mine, I have

google-play-services.jar.properties
google-play-services.jar

in the library folder, and in the manifest I have

B4X:
AddApplicationText(<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />)
  
'    you must add one of these two permissions
'    the permission you add will define the accuracy of location updates
AddPermission(android.permission.ACCESS_FINE_LOCATION)


Hope this helps to track down your issue !

Potman
 
Upvote 0
Status
Not open for further replies.
Top