Integrare le recensioni in-app (Kotlin o Java)

Questa guida descrive come integrare le recensioni in-app nella tua app utilizzando Kotlin o Java. Se utilizzi il codice nativo o Unity, sono disponibili guide all'integrazione separate.

Configura l'ambiente di sviluppo

La libreria recensione in-app di Google Play fa parte delle librerie di base di Google Play. Includi la seguente dipendenza Gradle per integrare la libreria di recensioni in-app di Google Play.

trendy

// In your app’s build.gradle file:
...
dependencies {
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation 'com.google.android.play:review:2.0.1'

    // For Kotlin users also add the Kotlin extensions library for Play In-App Review:
    implementation 'com.google.android.play:review-ktx:2.0.1'
    ...
}

Kotlin

// In your app’s build.gradle.kts file:
...
dependencies {
    // This dependency is downloaded from the Google’s Maven repository.
    // So, make sure you also include that repository in your project's build.gradle file.
    implementation("com.google.android.play:review:2.0.1")

    // For Kotlin users also import the Kotlin extensions library for Play In-App Review:
    implementation("com.google.android.play:review-ktx:2.0.1")
    ...
}

Crea ReviewManager

ReviewManager è l'interfaccia che consente alla tua app di avviare un flusso di revisione in-app. Per ottenerlo, crea un'istanza utilizzando ReviewManagerFactory.

Kotlin

val manager = ReviewManagerFactory.create(context)

Java

ReviewManager manager = ReviewManagerFactory.create(context)

Richiedi un oggetto ReviewInfo

Segui le indicazioni su quando richiedere le revisioni in-app per stabilire i punti positivi nel flusso utente della tua app per richiedere una recensione (ad esempio, quando l'utente completa un livello di un gioco). Quando l'app raggiunge uno di questi punti, utilizza l'istanza ReviewManager per creare un'attività di richiesta. Se l'esito è positivo, l'API restituisce l'oggetto ReviewInfo necessario per avviare il flusso di revisione in-app.

Kotlin

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 ReviewException).errorCode
    }
}

Java

ReviewManager manager = ReviewManagerFactory.create(this);
Task<ReviewInfo> request = manager.requestReviewFlow();
request.addOnCompleteListener(task -> {
    if (task.isSuccessful()) {
        // We can get the ReviewInfo object
        ReviewInfo reviewInfo = task.getResult();
    } else {
        // There was some problem, log or handle the error code.
        @ReviewErrorCode int reviewErrorCode = ((ReviewException) task.getException()).getErrorCode();
    }
});

Avvia il flusso di revisione in-app

Utilizza l'istanza ReviewInfo per avviare il flusso di revisione in-app. Attendi che l'utente abbia completato il flusso di revisione in-app prima che l'app continui il normale flusso (ad esempio, passare al livello successivo).

Kotlin

val flow = manager.launchReviewFlow(activity, reviewInfo)
flow.addOnCompleteListener { _ ->
    // The flow has finished. The API does not indicate whether the user
    // reviewed or not, or even whether the review dialog was shown. Thus, no
    // matter the result, we continue our app flow.
}

Java

Task<Void> flow = manager.launchReviewFlow(activity, reviewInfo);
flow.addOnCompleteListener(task -> {
    // The flow has finished. The API does not indicate whether the user
    // reviewed or not, or even whether the review dialog was shown. Thus, no
    // matter the result, we continue our app flow.
});

Passaggi successivi

Testa il flusso di revisione in-app della tua app per verificare che l'integrazione funzioni correttamente.