javaandroidkotlinfusedlocationproviderapifusedlocationproviderclient

Kotlin: getFusedLocationProviderClient crashes


I am following a tutorial from Raywenderlich on Google Maps with Kotlin. I have the following code that crashes for reasons I cannot fathom:

MapActivity.kt

import com.google.android.gms.location.LocationServices
...
private lateinit var map: GoogleMap
private lateinit var fusedLocationClient: FusedLocationProviderClient

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_maps)
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    val mapFragment = supportFragmentManager
        .findFragmentById(R.id.map) as SupportMapFragment
    mapFragment.getMapAsync(this)

    fusedLocationClient = LocationServices.getFusedLocationProviderClient(this)
}

build.gradle

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'com.google.android.gms:play-services-maps:16.1.0'
    implementation 'com.google.android.gms:play-services-location:11.8.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}

I have commented out getFusedLocationProviderClient() and the app runs fine. Once that is uncommented, the app crashes. Is there something I am doing wrong? Is it possibly the reference to gms:play-services-location:11.8.0 version that could be the issue. If so how do I check what version I have installed?

Update Here is the clash log from Logcat

--------- beginning of crash
2019-12-23 09:37:02.698 14916-14916/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.johndoe.cityguide, PID: 14916
    java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/common/api/Api$zzf;
        at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0)
        at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0)
        at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.common.api.Api$zzf" on path: DexPathList[[zip file "/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/base.apk"],nativeLibraryDirectories=[/data/app/com.johndoe.cityguide-jR7TeGEs6Oxa46o3Ce5Zcw==/lib/x86, /system/lib, /vendor/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source:0) 
        at com.google.android.gms.location.LocationServices.getFusedLocationProviderClient(Unknown Source:0) 
        at com.johndoe.cityguide.MapsActivity.onCreate(MapsActivity.kt:29) 
        at android.app.Activity.performCreate(Activity.java:7009) 
        at android.app.Activity.performCreate(Activity.java:7000) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
        at android.app.ActivityThread.-wrap11(Unknown Source:0) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:164) 
        at android.app.ActivityThread.main(ActivityThread.java:6494) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Solution

  • I think I ran into a similar issue once upon a time; it was due to versioning conflicts between Play Services dependencies. I currently use the following to utilize the FusedLocationProviderClient:

    dependencies {
        def play_services_version = '17.0.0'
    
        ...
    
        implementation "com.google.android.gms:play-services-maps:$play_services_version"
        implementation "com.google.android.gms:play-services-location:$play_services_version"
    }
    

    I hope this helps!