Saifur Rahman Mohsin
Saifur Rahman Mohsin

Reputation: 1011

Google’s DirectionsApi doesnt appear to work in Kotlin

I'm trying to implement DirectionsApi to draw polylines on Google maps in my Android app using Kotlin. I added the required library using:

implementation 'com.google.maps:google-maps-services:0.9.0'

And then included the following function in my MapFragment:

private fun getGeoContext() : GeoApiContext {
    val geoApiContext = GeoApiContext.Builder()
            .apiKey(getString(R.string.directions_apikey))
            .build()
    return geoApiContext
}

And finally in my onMapReady I'm trying to fetch the directions result using:

val origin = "Hotel Hillview Munnar, State Highway 16, Moolakadai, Munnar, Kerala, India"
val dest = "Echo Point, Kannan Devan Hills, Kerala, India"

val apiRequest = DirectionsApi.newRequest(getGeoContext())
        apiRequest.origin(origin)
        apiRequest.destination(dest)
        apiRequest.mode(TravelMode.DRIVING)
        apiRequest.setCallback(object : com.google.maps.PendingResult.Callback<DirectionsResult> {
            override fun onResult(result:DirectionsResult) {
                Timber.i("Call Success")
                val routes = result.routes
            }
            override fun onFailure(e:Throwable) {
            }
        })

But it keeps failing. I checked to Logcat logs and found this as the error:

10-01 17:22:41.248 4112-4193/com.acme.internal E/AndroidRuntime: FATAL EXCEPTION: Rate Limited Dispatcher
    Process: com.acme.internal, PID: 4112
    java.lang.NoClassDefFoundError: Failed resolution of: Ljava/time/LocalDateTime;
        at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241)
        at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "java.time.LocalDateTime" on path: DexPathList[[zip file "/data/app/com.acme.internal-1/base.apk"],nativeLibraryDirectories=[/data/app/com.acme.internal-1/lib/arm64, /vendor/lib64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at com.google.maps.internal.OkHttpPendingResult.parseResponse(OkHttpPendingResult.java:241) 
        at com.google.maps.internal.OkHttpPendingResult.onResponse(OkHttpPendingResult.java:207) 
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:153) 
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
        at java.lang.Thread.run(Thread.java:818) 
      Suppressed: java.lang.ClassNotFoundException: java.time.LocalDateTime
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 8 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available
10-01 17:22:41.248 4112-4193/com.acme.internal E/AbstractTracker: Can't create handler inside thread that has not called Looper.prepare()

I'm guessing this has to do with some issue with the Google maps library using the LocalDateTime class but since I'm using kotlin it's not able to access said class? Is there some work around for this issue?

Upvotes: 2

Views: 1541

Answers (1)

Saifur Rahman Mohsin
Saifur Rahman Mohsin

Reputation: 1011

I figured out a solution on my own for now. It's not the best fix but solves the issue at hand. Written it here so it may help anyone else looking for a solution to the same issue.

Switch the gradle dependency from:

implementation 'com.google.maps:google-maps-services:0.9.0'

to

implementation 'com.google.maps:google-maps-services:0.2.11'

And it should fix it.

Upvotes: 3

Related Questions