1

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) 
pnizzle
  • 6,243
  • 4
  • 52
  • 81
  • "the app crashes" -- if you mean you crash at runtime, check Logcat to see what the stack trace is. – CommonsWare Dec 22 '19 at 23:34
  • @CommonsWare I've added the crash log. Im pretty new to android dev so it might take me a bit to figure things out here. – pnizzle Dec 22 '19 at 23:44

1 Answers1

1

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!

buggily
  • 418
  • 2
  • 12
  • How do you know to use '17.0.0' ? How do you know that that is the version you have installed. Where do you check that? – pnizzle Dec 23 '19 at 00:13
  • Well, heck! This worked. But I really need to know why it did? – pnizzle Dec 23 '19 at 00:24
  • Hi again, I am glad this helped! To check versions for Android dependencies, I rely on https://mvnrepository.com which lists stable version histories as well as alpha and beta channels. For instance, https://mvnrepository.com/artifact/com.google.android.gms/play-services-location would be where I would lookup the latest version for play-services-location. Like in this case, similar libraries from the same organization will often match in version. To check which version(s) you have installed, you can always reference the app/build.gradle. – buggily Dec 24 '19 at 04:46
  • The app/build.gradle in Android Studio will often warn you if have an outdated version but may recommend an alpha, beta, or release track. If you would rather use a stable release, search for the library in Maven as I suggested. Another similar resource is https://maven.google.com/web/index.html which lists versions specific to Android libraries supported by Google. It's a good idea to default to the latest versions unless you have a specific reason for not doing so. – buggily Dec 24 '19 at 04:56
  • As you have found, old versions can have conflicting library dependencies as things get patched or become deprecated. I am sure that version combination you listed worked well at one point but sometimes bugs like this come about with time. Cheers! – buggily Dec 24 '19 at 04:56