Notes: 03. Build An In App Review Module
After adding a new module, I deleted the kotlin_version property from the Gradle file.
This is because we already have that defined, but adding a new module creates a duplicate which will cause build errors.
To start implementing the In App Review feature, you’ll create a new feature module that will let you completely decouple the code from the rest of the app.
This is a good practice, as you can easily separate the code and reuse it in different parts of your app, even in a different project if you decide to publish it as a small library! Let’s see how to do so!
Start off by clicking File > New > New Module and choose Android Library module naming it inappreview. Now that you’ve created the module, add the following line of code to the app module’s build.gradle file:
implementation project(':inappreview')
This line will let you use inappreview code within the app module. It loads up the module within this one, allowing you to create objects, use functions and types you’ve defined elsewhere. Just like you’d add a standard Gradle dependency for a published library, you can load local modules this way.
Now that you’ve connected these two modules, open the inappreview module’s build.gradle file and change the plugins to the following:
plugins {
id 'com.android.library'
id 'kotlin-android'
id 'kotlin-kapt'
id 'dagger.hilt.android.plugin'
}
You’ll use the kotlin-kapt and dagger.hilt plugins to process the dependencies later in the course and provide them to the entire project. Now change the following part to make sure all the versions are the same as in other parts of the project:
defaultConfig {
minSdkVersion versions.minSdk
targetSdkVersion versions.targetSdk
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
This is to keep all versions the same, so that you don’t have dependency versioning issues.
Finally, you need to add certain dependencies to build this feature. The first part of dependencies you need to add is the Kotlin dependency and other modules you need, such as styles and preferences. Add the following code to do that:
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kotlin}"
implementation project(':styles')
implementation project(':preferences')
...
}
Again, by using implementation project() you add local modules to this module. This means you’ll be able to use general styles and preferences in this module. Next is the general UI code that you need to add to your project. Do that the following way:
implementation "androidx.appcompat:appcompat:${versions.appCompat}"
implementation "androidx.constraintlayout:constraintlayout:${versions.constraintLayout}"
implementation "androidx.core:core-ktx:${versions.coreKtx}"
implementation "androidx.cardview:cardview:${versions.cardView}"
All of these dependencies will help you deal with UI parts of the feature, where you’ll show a small dialog to the user, to ask them to review the app. We’ll go over that a bit more later, when you connect that code to the rest of the app. These dependencies allow you to use different parts of UI code, such as ConstraintLayout, the CardView and similar.
The next two dependencies are the most important ones. They will let you use the Play Core API to start the review process from within the app. Add the following code:
// Play core
api "com.google.android.play:core:${versions.playCore}"
api "com.google.android.play:core-ktx:${versions.playCoreKtx}"
The play:core dependency lets you use many Android features such as the ReviewManager. The play:core-ktx dependency provides many useful shorthand functions to use such an API. It makes it easier to work with the API.
Finally, let’s add Hilt to the project, that will let you setup dependency injection and share your code across the project:
// Hilt (Dagger)
// https://developer.android.com/training/dependency-injection/hilt-android
implementation "com.google.dagger:hilt-android:${versions.hilt}"
kapt "com.google.dagger:hilt-android-compiler:${versions.hilt}"
Awesome! Those are all the dependencies you need to make this feature work. There are only a few of them, as this will be a small feature. Now you have everything ready to start working on the project! Build & run the app, just to make sure that everything’s working fine!