1

I am new to android programming. I am creating a project that needs to calculate the route between two geo points. I have both points and my google map correctly but when I call GeoApiContext my program crashes. This is the error:

D/DockFinderActivity: calculateDirections: calculating directions.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.anchor.demo, PID: 21685
java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
    at okhttp3.internal.Util.<clinit>(Util.java:87)
    at okhttp3.internal.Util.immutableList(Util.java:234)
    at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124)
    at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.java:449)
    at com.google.maps.OkHttpRequestHandler$Builder.<init>(OkHttpRequestHandler.java:106)
    at com.google.maps.GeoApiContext$Builder.<init>(GeoApiContext.java:318)
    at com.example.anchor404.DockFinderActivity.calculateDirections(DockFinderActivity.java:497)
    at com.example.anchor404.DockFinderActivity.access$900(DockFinderActivity.java:65)
    at com.example.anchor404.DockFinderActivity$8.onClick(DockFinderActivity.java:473)
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.ClassCastException: Bootstrap method returned null
    at okhttp3.internal.Util.<clinit>(Util.java:87) 
    at okhttp3.internal.Util.immutableList(Util.java:234) 
    at okhttp3.OkHttpClient.<clinit>(OkHttpClient.java:124) 
    at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.java:449) 
    at com.google.maps.OkHttpRequestHandler$Builder.<init>(OkHttpRequestHandler.java:106) 
    at com.google.maps.GeoApiContext$Builder.<init>(GeoApiContext.java:318) 
    at com.example.anchor404.DockFinderActivity.calculateDirections(DockFinderActivity.java:497) 
    at com.example.anchor404.DockFinderActivity.access$900(DockFinderActivity.java:65) 
    at com.example.anchor404.DockFinderActivity$8.onClick(DockFinderActivity.java:473) 
    at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:172) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
   I/Process: Sending signal. PID: 21685 SIG: 9
   Process 21685 terminated.

I am calling geoApiContext like this:

if(mGeoApiContext == null){
        mGeoApiContext = new GeoApiContext.Builder()
                .apiKey(getString(R.string.google_map_api_key))
                .build();
    }

mGeoApiContext is a global:

private GeoApiContext mGeoApiContext = null;

Inside my gradle:

// Google Maps Services (needed for directions)
implementation 'com.google.maps:google-maps-services:0.10.1'
implementation 'org.slf4j:slf4j-simple:1.7.25'

My manifest has these permissions:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Am I missing a permission? Am I calling GeoApiContext wrong or could it be an issue with my API key?

evan
  • 5,443
  • 2
  • 11
  • 20
ConCon19
  • 13
  • 5
  • Can you post the entire stack trace? – Dinesh Oct 24 '19 at 04:11
  • @Dinesh I just edited the question, if that's not what you meant by full stack trace please explain more. – ConCon19 Oct 24 '19 at 13:12
  • What API key are you using to geocode? Is it the same one you use to load your Android map, and if so, is it Android restricted? Note that you need to use different API keys as you're using the Geocoding web service. See https://stackoverflow.com/questions/57196465/how-to-use-google-directions-android-library-with-a-restricted-api-key – evan Oct 30 '19 at 09:27

2 Answers2

1

I ran into the same error when I tested your code so after a lot of research I found this issue. You need to set both source compatibility and target compatibility to 1.8 (Java 8).

build.gradle

android {
    compileOptions {
        targetCompatibility = 1.8
        sourceCompatibility = 1.8
    }
}

Or if you use Android Studio, go to File > Project Structure > Modules > Properties tab.

After making the above change I was able to use GeoApiContext in my Android app without problem so I hope this helps you too!

evan
  • 5,443
  • 2
  • 11
  • 20
0

I dont know if its any help but just talked to the guys from google and they said that GeoApiContext is not ment to be used directly on android phones since if you try to restrict the key and limit it to your app it will not work and you will not get authorization, and without restriction you run into risk of potential abuse of your key, so the api is ment to be used mainly by web services

anyway it may change in future so mark the date of my post

mariubog
  • 1,498
  • 15
  • 16