I am trying to add Google Places AutoComplete API to my Android Instant Apps project.
I followed this sample to implement the API.
My PlacesAdapter
class:
package net.epictimes.uvindex.autocomplete
import android.content.Context
import android.graphics.Typeface
import android.text.style.StyleSpan
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Filter
import android.widget.Filterable
import android.widget.TextView
import com.google.android.gms.common.data.DataBufferUtils
import com.google.android.gms.location.places.AutocompletePrediction
import com.google.android.gms.location.places.GeoDataClient
import com.google.android.gms.maps.model.LatLng
import com.google.android.gms.maps.model.LatLngBounds
import com.google.android.gms.tasks.RuntimeExecutionException
import com.google.android.gms.tasks.Tasks
import timber.log.Timber
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
class PlacesAdapter(context: Context, private val geoDataClient: GeoDataClient)
: ArrayAdapter<AutocompletePrediction>(context, android.R.layout.simple_expandable_list_item_2, android.R.id.text1), Filterable {
private val resultList = mutableListOf<AutocompletePrediction>()
private val styleBold = StyleSpan(Typeface.BOLD)
override fun getCount(): Int = resultList.size
override fun getItem(position: Int): AutocompletePrediction = resultList[position]
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val row = super.getView(position, convertView, parent)
// Sets the primary and secondary text for a row.
// Note that getPrimaryText() and getSecondaryText() return a CharSequence that may contain
// styling based on the given CharacterStyle.
val item = getItem(position)
val textView1 = row.findViewById<View>(android.R.id.text1) as TextView
val textView2 = row.findViewById<View>(android.R.id.text2) as TextView
textView1.text = item.getPrimaryText(styleBold)
textView2.text = item.getSecondaryText(styleBold)
return row
}
override fun getFilter(): Filter {
return object : Filter() {
override fun performFiltering(constraint: CharSequence?): FilterResults {
val results = FilterResults()
// We need a separate list to store the results, since
// this is run asynchronously.
var filterData: ArrayList<AutocompletePrediction>? = ArrayList()
// Skip the autocomplete query if no constraints are given.
if (constraint != null) {
// Query the autocomplete API for the (constraint) search string.
filterData = getAutocomplete(constraint)
}
results.values = filterData
if (filterData != null) {
results.count = filterData.size
} else {
results.count = 0
}
return results
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
if (results != null && results.count > 0) {
// The API returned at least one result, update the data.
resultList.clear()
resultList.addAll(results.values as ArrayList<AutocompletePrediction>)
}
notifyDataSetChanged()
}
}
}
/**
* Submits an autocomplete query to the Places Geo Data Autocomplete API.
* Results are returned as frozen AutocompletePrediction objects, ready to be cached.
* Returns an empty list if no results were found.
* Returns null if the API client is not available or the query did not complete
* successfully.
* This method MUST be called off the main UI thread, as it will block until data is returned
* from the API, which may include a network request.
*
* @param constraint Autocomplete query string
* @return Results from the autocomplete API or null if the query was not successful.
* @see GeoDataClient.getAutocompletePredictions
* @see AutocompletePrediction.freeze
*/
private fun getAutocomplete(constraint: CharSequence): ArrayList<AutocompletePrediction>? {
Timber.d("Starting autocomplete query for: " + constraint)
val latLngBounds = LatLngBounds(LatLng(-34.041458, 150.790100), LatLng(-33.682247, 151.383362))
// Submit the query to the autocomplete API and retrieve a PendingResult that will
// contain the results when the query completes.
val results = geoDataClient.getAutocompletePredictions(constraint.toString(), latLngBounds, null)
// This method should have been called off the main UI thread. Block and wait for at most
// 60s for a result from the API.
try {
Tasks.await(results, 60, TimeUnit.SECONDS)
} catch (e: ExecutionException) {
Timber.e(e)
} catch (e: InterruptedException) {
Timber.e(e)
} catch (e: TimeoutException) {
Timber.e(e)
}
Timber.d("Query completed. Received " + results.result.count + " predictions.")
return try {
// Freeze the results immutable representation that can be stored safely.
DataBufferUtils.freezeAndClose<AutocompletePrediction, AutocompletePrediction>(results.result)
} catch (e: RuntimeExecutionException) {
// If the query did not complete successfully return null
Timber.e("Error getting autocomplete prediction API call", e)
null
}
}
}
When I run the project in app
configuration it works fine.
But when I run it in instantapp
configuration it throws this:
D/PlacesAdapter: Starting autocomplete query for: is
E/GmsClient: unable to connect to service: com.google.android.gms.location.places.GeoDataApi on com.google.android.gms
E/PlacesAdapter: java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED
at com.google.android.gms.tasks.Tasks.zzc(Unknown Source:17)
at com.google.android.gms.tasks.Tasks.await(Unknown Source:53)
at net.epictimes.uvindex.autocomplete.PlacesAdapter.getAutocomplete(PlacesAdapter.kt:116)
at net.epictimes.uvindex.autocomplete.PlacesAdapter.access$getAutocomplete(PlacesAdapter.kt:25)
at net.epictimes.uvindex.autocomplete.PlacesAdapter$getFilter$1.performFiltering(PlacesAdapter.kt:64)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED
at com.google.android.gms.common.internal.zzb.zzz(Unknown Source:14)
at com.google.android.gms.common.internal.zzbk.zzaa(Unknown Source:0)
at com.google.android.gms.common.internal.zzbl.zzs(Unknown Source:32)
at com.google.android.gms.common.api.internal.zzs.zzc(Unknown Source:46)
at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source:42)
at com.google.android.gms.common.api.internal.zzm.zzv(Unknown Source:17)
at com.google.android.gms.common.api.internal.zzc.zzt(Unknown Source:2)
at com.google.android.gms.common.api.internal.zzbr.zzx(Unknown Source:27)
at com.google.android.gms.common.api.internal.zzbp.handleMessage(Unknown Source:386)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
W/Filter: An exception occured during performFiltering()!
com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED
at com.google.android.gms.tasks.zzn.getResult(Unknown Source:14)
at net.epictimes.uvindex.autocomplete.PlacesAdapter.getAutocomplete(PlacesAdapter.kt:123)
at net.epictimes.uvindex.autocomplete.PlacesAdapter.access$getAutocomplete(PlacesAdapter.kt:25)
at net.epictimes.uvindex.autocomplete.PlacesAdapter$getFilter$1.performFiltering(PlacesAdapter.kt:64)
at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
Caused by: com.google.android.gms.common.api.ApiException: 17: API_NOT_CONNECTED
at com.google.android.gms.common.internal.zzb.zzz(Unknown Source:14)
at com.google.android.gms.common.internal.zzbk.zzaa(Unknown Source:0)
at com.google.android.gms.common.internal.zzbl.zzs(Unknown Source:32)
at com.google.android.gms.common.api.internal.zzs.zzc(Unknown Source:46)
at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source:42)
at com.google.android.gms.common.api.internal.zzm.zzv(Unknown Source:17)
at com.google.android.gms.common.api.internal.zzc.zzt(Unknown Source:2)
at com.google.android.gms.common.api.internal.zzbr.zzx(Unknown Source:27)
at com.google.android.gms.common.api.internal.zzbp.handleMessage(Unknown Source:386)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
API_NOT_CONNECTED
error code's explation in the docs:
The client attempted to call a method from an API that failed to connect. Possible reasons include:
- The API previously failed to connect with a resolvable error, but the user declined the resolution.
- The device does not support GmsCore.
- The specific API cannot connect on this device.
I updated the Google Play Services on the emulator (it's 11.7.46 (470-175121617) BTW) but it didn't changed anyting.
I put my Google Play Services dependencies into the base
module. I checked the merged manifest files, they all have the Google Play API keys.
In the Android Developers site they listed Google Places API in the supported API list.
I am using Google Location API in the other feature module and it is working in both app
and instantapp
configurations.
So what am I doing wrong here?