How to Use Google Play Install Referrer API in Android using Jetpack Compose?
Last Updated :
03 Aug, 2022
Google Play Install Referrer is the API that is used in most of the applications but it is not seen in the application. This functionality works under the hood and is used to check the sources from where the app is getting most of the downloads. Google Play Install Referrer API tells us that from where the application has to go and install the sources. This will help us in improving the presence of our apps on different platforms. In this article, we will take a look at How to Use Google Play Install Referrer API in Android using Jetpack Compose.
Note: If you are seeking Java code for Jetpack Compose, please note that Jetpack Compose is only available in Kotlin. It uses features such as coroutines, and the handling of @Composable annotations is handled by a Kotlin compiler. There is no method for Java to access these. Therefore, you cannot use Jetpack Compose if your project does not support Kotlin.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Adding a new color in the Color.kt file
Navigate to app > java > your app’s package name > ui.theme > Color.kt file and add the below code to it.
Kotlin
import androidx.compose.ui.graphics.Color
val Purple200 = Color(0xFF0F9D58)
val Purple500 = Color(0xFF0F9D58)
val Purple700 = Color(0xFF3700B3)
val Teal200 = Color(0xFF03DAC5)
// in the below line, we are adding different colors.
val greenColor = Color(0xFF0F9D58)
Step 3 : Adding Dependency for using Google Play Install Referrer API.
Navigate to Gradle Scripts > build.gradle and add below dependency to it in dependencies section.
implementation "com.android.installreferrer:installreferrer:2.2"
After adding this dependency simply sync your project to install it.
Step 4: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
import android.content.Context
import android.os.Bundle
import android.os.RemoteException
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.*
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.*
import com.android.installreferrer.api.InstallReferrerClient
import com.android.installreferrer.api.InstallReferrerStateListener
import com.android.installreferrer.api.ReferrerDetails
import com.example.newcanaryproject.ui.theme.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
NewCanaryProjectTheme {
// in the below line, we are specifying background color for our application
Surface(
// in the below line, we are specifying modifier and color for our app
modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background
) {
// in the below line, we are specifying theme as scaffold.
Scaffold(
// in scaffold we are specifying top bar.
topBar = {
// inside top bar we are specifying background color.
TopAppBar(backgroundColor = greenColor,
// along with that we are specifying title for our top bar.
title = {
// in the top bar we are specifying tile as a text
Text(
// in the below line, we are specifying text to display in top app bar.
text = "Google Play Install Referrer",
// in the below line, we are specifying modifier to fill max width.
modifier = Modifier.fillMaxWidth(),
// in the below line, we are specifying text alignment.
textAlign = TextAlign.Center,
// in the below line, we are specifying color for our text.
color = Color.White
)
})
}) {
// in the below line, we are calling pop window dialog method to display ui.
GooglePlayInstallReferrer(LocalContext.current)
}
}
}
}
}
}
@Composable
fun GooglePlayInstallReferrer(ctx: Context) {
// in the below line, we are creating a variable for referrer
val referrer = remember {
mutableStateOf("")
}
// in the below line, we are building our install referrer client and building it.
var referrerClient: InstallReferrerClient = InstallReferrerClient.newBuilder(ctx).build();
// in the below line, we are starting its connection.
referrerClient.startConnection(object : InstallReferrerStateListener {
override fun onInstallReferrerSetupFinished(responseCode: Int) {
// this method is called when install referrer setup is finished.
when (responseCode) {
InstallReferrerClient.InstallReferrerResponse.OK -> {
// this case is called when the status is OK and
var response: ReferrerDetails? = null
try {
// in the below line, we are getting referrer details
// by calling get install referrer.
response = referrerClient.installReferrer
// in the below line, we are getting referrer url.
val referrerUrl = response.installReferrer
// in the below line, we are getting referrer click time.
val referrerClickTime = response.referrerClickTimestampSeconds
// in the below line, we are getting app install time
val appInstallTime = response.installBeginTimestampSeconds
// in the below line, we are getting our time when
// user has used our apps instant experience.
val instantExperienceLaunched = response.googlePlayInstantParam
// in the below line, we are getting our
// apps install referrer.
var refrer = response.installReferrer
// in the below line, we are setting all detail to our text view.
referrer.value =
"Referrer is : \n$referrerUrl\nReferrer Click Time is : $referrerClickTime\nApp Install Time : $appInstallTime"
} catch (e: RemoteException) {
// handling error case.
e.printStackTrace()
}
}
InstallReferrerClient.InstallReferrerResponse.FEATURE_NOT_SUPPORTED ->
// API not available on the current Play Store app.
Toast.makeText(
ctx,
"Feature not supported..",
Toast.LENGTH_SHORT
).show()
InstallReferrerClient.InstallReferrerResponse.SERVICE_UNAVAILABLE ->
// Connection couldn't be established.
Toast.makeText(
ctx,
"Fail to establish connection",
Toast.LENGTH_SHORT
).show()
}
}
override fun onInstallReferrerServiceDisconnected() {
// Try to restart the connection on the next request to
// Google Play by calling the startConnection() method.
Toast.makeText(ctx, "Service disconnected..", Toast.LENGTH_SHORT)
.show()
}
})
// in the below line, we are creating a column.
Column(
// in this column we are specifying
// modifier to add padding and fill
// max size
modifier = Modifier
.fillMaxSize()
.padding(horizontal = 20.dp),
// in the below line, line we are adding horizontal alignment
// and vertical arrangement
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// in the below line, line we are creating a text view.
Text(
text = "Google Play Install Referrer",
color = greenColor,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
// in the below line, we are adding a spacer.
Spacer(modifier = Modifier.height(20.dp))
// in the below line, we are creating a simple text
Text(
text = referrer.value,
color = Color.Black,
fontSize = 18.sp,
textAlign = TextAlign.Center
)
}
}
Now run your application to see the output of it.
Similar Reads
How to Play Audio From URL in Android using Jetpack Compose? Many apps require the feature to add the audio feature in their application and there are so many audio files that we have to play inside our application. If we will store so many audio files inside our application, then it will increase the size of the app and this may reduce the user base due to t
7 min read
How to Update Data in API using Retrofit in Android using Jetpack Compose? Android applications use APIs within Android Applications to access data from databases. We can perform several operations using APIs such as Reading, Writing, and Updating our Data in the Database. In this article, we will take a look at How to Update Data in API using Retrofit in Android using Jet
9 min read
How to Post Data to API using Retrofit in Android using Jetpack Compose? APIs are used within Android Applications to interact with databases to perform various CRUD operations on data within the database such as adding new data, reading the existing data, and updating and deleting existing data. In this article, we will take a look at How to Post Data to API using Retro
5 min read
How to Use Different Types of Google Maps in Android using Jetpack Compose? When we use the default application of Google Maps we will get to see different types of Maps present inside this application. We will get to see satellite maps, terrain maps, and many more. We have seen adding Google Maps in the Android application. In this article, we will take a look at the imple
7 min read
How to Add Custom Marker to Google Maps in Android using Jetpack Compose? Many android applications use maps to display the location. We can get to see they use custom marker icons like a delivery boy image or an icon to display a marker on the map. In this article, we will take a look at How to use a custom icon as a marker on google maps in our android application. Ste
6 min read
How to Add Multiple Markers to Google Maps in Android using Jetpack Compose? Google Maps is used in most apps which are used to represent many locations and markers on Google Maps. We have seen markers on Google maps for multiple locations. In this article, we will take a look at the implementation of Multiple Markers on Google Maps in Android using Jetpack Compose. Step By
6 min read