Issue
Trying to follow the google tutorial for in-app reviews and I'm currently stuck with the following piece of code:
val request = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
if (task.isSuccessful) {
// We got the ReviewInfo object
val reviewInfo = task.result
} else {
// There was some problem, log or handle the error code.
@ReviewErrorCode val reviewErrorCode = (task.getException() as TaskException).errorCode
}
}
When I copy this into my IDE, it doesn't seem to pick up the TaskException
. What could I rather use here as an alternative ?
I've added:
implementation 'com.google.android.play:core:1.10.0'
implementation 'com.google.android.play:core-ktx:1.8.1'
Solution
I think the documentation has a typo.
The correct exception cast should be this RuntimeExecutionException
I've opened a documentation issue about this
UPDATE: 11/01/2022 (dd/MM/yyyy)
Upgrading your play:core
dependency to at least version 1.10.1 will bring you a new custom ReviewException
which should replace the RuntimeExecutionException
(or at least this is what we think).
Without an official answer the current safest solution is probably the one below:
val errorCode = when (val exception = task.exception) {
is ReviewException -> {
exception.errorCode
}
is RuntimeExecutionException -> {
exception.errorCode
}
else -> {
9999
}
}
You can use any value in place of 9999 but it is probably better if the integer is not already defined as error code in https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes just to avoid confusion
Answered By - MatPag
Answer Checked By - Dawn Plyler (JavaFixing Volunteer)