How to add Google Play In-app Review Dialog?
Tags: android, kotlin, beginners
Published: 2023-06-03 00:00:00

**A step-by-step guide how you can let users review your app using Google Play In-app review API and test it out using Internal App Sharing**

This is just a quick guide and a simple example of how to add the Google Play In-app review dialog in your app. This is what it looks like after I implemented this in [RSS feed reader app](https://vtsen.hashnode.dev/simple-rss-feed-reader-jetpack-compose).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683438263244/94cfaa3d-852c-4927-9972-1e058d3bb15e.png)

## 1\. Add Review API Libraries

This example of build.gradle.kts ([Kotlin script/KTS](https://vtsen.hashnode.dev/how-to-convert-android-gradle-groovy-to-kts#heading-what-is-kotlin)) in your app module.

```kotlin
dependencies {
    /*...*/
    implementation("com.google.android.play:review:2.0.1")
    implementation("com.google.android.play:review-ktx:2.0.1")
    /*...*/
}
```

## 2\. Create ReviewManager in the Activity

It is better to **implement the** `ReviewManger` **in activity** rather than a composable function because its APIs needs an activity. Well, you can technically do it in composable function (through `LocalContext`), but it is not a good practice because composable functions shouldn't need to know about the activity.

This creates the `ReviewManager` using `ReviewManagerFactory.create()` API in your activity. Of course, only when it is accessed because of the [lazy delegate](https://vtsen.hashnode.dev/understand-by-delegated-properties-in-kotlin#heading-by-lazy) is used here.

```kotlin
class MainActivity : ComponentActivity() {
    /*...*/
    private val reviewManager by lazy {
        ReviewManagerFactory.create(this)
    }
    /*...*/
}
```

## 3\. Add showReviewDialog() Function

Let's create a function that requests the review flow and launch the review dialog.

```kotlin
class MainActivity : ComponentActivity() {
    /*...*/
    private fun showReviewDialog() {
        val request = reviewManager.requestReviewFlow()
        request.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val reviewInfo = task.result
                reviewManager.launchReviewFlow(this, reviewInfo)
            }
        }
    }
    /*...*/
}
```

Then, set this function to be a callback from your composable function.

```kotlin
override fun onCreate(savedInstanceState: Bundle?) {
    /*...*/
    setContent {
        /*...*/
        MainScreen(showReviewDialog = ::showReviewDialog)
    }
}
```

What I did is simply add a drop-down "Rate Me" menu to the app.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683438532400/9d46a111-b369-4f5e-82f4-af035e840893.png)

Well, this is for educational purposes and **is not recommended** to implement this way because there are **no callbacks to indicate whether Google Play review dialog is shown or not**. If the dialog is not shown (for the reasons below), this appears to be a broken user flow, which we want to avoid!

There are **quotas** on **how many times this API can be called**. Clicking the "Rate Me" the second time, won't show the Google Play review dialog anymore. I also noticed if you have already reviewed the app, call the `ReviewManager.launchReviewFlow()` API does nothing too.

For more information, you can refer to the official document [here](https://developer.android.com/guide/playcore/in-app-review#quotas).

## 4\. Test In-app Reviews

The issue is you can't test it in your emulator directly because it requires your app to be published in Play Store.

There are a few ways you can test it out (as you can read from the documentation [here](https://developer.android.com/guide/playcore/in-app-review/test)) but the easiest way in my opinion is **internal app sharing**.

Here are the steps to upload your app for internal app sharing:

1. In **Google Play Console**, go to **Setup** -&gt; **Internal App Sharing**
    
2. Create an Internal Testers email lists. Separate the emails with a comma.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683441690920/3ce836b2-fb47-4003-aba6-ca5338f70523.png)
    
3. In **Manage testers**, select **Restrict access to email lists**
    
4. In **Manage uploaders,** click this link, `https://play.google.com/console/internal-app-sharing`. After that, you can upload your app bundle or APK here.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683441764452/a95b0bff-6630-4064-90a4-1fa7091160b2.png)
    
5. Copy the link and sent it to your testers (it could be yourself). The tester needs to open the link on his/her phone and it looks like this. Click **OPEN IN PLAY STORE APP.**
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683442411931/b89bee22-808d-436d-aec7-d9423f103bd3.png)
    
6. You get this if your Google Play has not enabled Internal app sharing. Click the **Google Play Settings**.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683442538024/b1f7a142-7ca3-45f8-900d-489d2d95cc8f.png)
    
7. In Google Play Settings, go to **About**, and click **Play Store Version** 7 times. This enables the developer options.
    
8. Go to **General** -&gt; **Developer options**, and enable the **Internal app sharing**. You get this prompt, so just click **Turn on**.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683442878769/f2d0ce61-5ca9-41c8-8a59-3f7c37b0a1be.png)
    
9. Go back to step 5, and click **OPEN IN PLAY STORE APP** again. It brings you to Google Play Store, to install the app.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683443141745/702cc76e-574e-42be-b0ee-21e91607ed5b.png)
    
    > If the app has already been installed previously, it asks you to uninstall it first.
    
10. Now, you should be able to test the Google Play In-App Review Dialog.
    

In this **testing mode**, the **quotas do not apply** here. You can call this Google Play In-App review API as many times as you wish. The only thing is the **Submit button is disabled**.

## Conclusion

Implementing the Google Play in-app review dialog is quite simple, but testing is a bit tricky. It will be nice if we can test it out directly from the emulator.

Deciding when to trigger the in-app review is also a bit tricky for a very simple app like this one. Unlike games after the player finished a level, you can trigger in-app reviews.

Maybe I can do something like prompting the in-app review after the user installed the app after a certain period? Yeah, I will do that next.

> **\[Updated - July 09, 20 23\]:** This is an example of getting the duration after the app was first installed using `PackageInfo.firstInstallTime` from `PackageManager`.

```kotlin
class MainActivity : ComponentActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        showReviewDialog()
        super.onCreate(savedInstanceState)
        /*...*/
    }

    private fun showReviewDialog() {        
        val packageInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
            packageManager.getPackageInfo(packageName, PackageManager.PackageInfoFlags.of(0))
        } else {
            packageManager.getPackageInfo(packageName, 0)
        }

        val installTime = packageInfo.firstInstallTime
        val currentTime = System.currentTimeMillis()
        val durationInMillis = currentTime - installTime
        val durationInDays = durationInMillis / (1000 * 60 * 60 * 24)
        /*...*/
    }
}
```

## Example

GitHub Repository: [AndroidNews](https://github.com/vinchamp77/AndroidNews)

---

*Originally published at [https://vtsen.hashnode.dev](https://vtsen.hashnode.dev/how-to-add-google-play-in-app-review-dialog)*.