Play Install Referrer Library

You can use the Google Play Store's Install Referrer API to securely retrieve
referral content from Google Play. The Play Install Referrer API Client Library
is written in Java and is a wrapper for the Android Interface Definition
Language (AIDL) file that defines the interface to the Install Referrer service.
You can use the Play Install Referrer API Client Library to simplify your
development process.

This guide covers the basics of retrieving referral information from Google
Play using the Play Install Referrer Library.

Note: If you are using a language other than Java, or you want to interact directly
with the AIDL file, use the
Play Install Referrer API.

Updating your app's dependencies

Add the following line to the dependencies section of the build.gradle file
for your app:

Override the onInstallReferrerSetupFinished()
method to handle lost connections to Google Play. For
example, the Play Install Referrer Library client may lose connection if the
Play Store service is updating in the background. The library client must call
the
startConnection()
method to restart the connection before making further requests.

The following code demonstrates how to start and test a connection to the
Play Store app:

Java

Caution: The install referrer information will be
available for 90 days and won't change unless the application is
reinstalled. To avoid unecessary API calls in your app, you should invoke the
API only once during the first execution after install. Your app can listen
to the system broadcast
Intent.ACTION_PACKAGE_FIRST_LAUNCH to identify the app's first
execution. For details about how to handle a broadcast, check the
Receiving broadcasts section.

Closing service connection

After getting referrer information, call the endConnection()
method on
InstallReferrerClient
instance to close the connection. Closing the connection will help you avoid
leaks and performance problems.