π± Google Play's App Integrity API implementing for offline Android mobile app
- Get link
- X
- Other Apps
If your Android app is offline and doesn't have a backend, you can still use Google Play Integrity API to check whether the app is installed from the Play Store and running on a genuine device. However, since there's no server to verify the integrity token, you will only be able to check basic integrity locally.
β Steps to Use Play Integrity API Without a Backend (Offline Mode)
1οΈβ£ Enable Play Integrity API in Google Play Console
- Go to Google Play Console.
- Select your app β Navigate to Setup β App Integrity.
- Enable the Play Integrity API.
- Save the License Key (you wonβt need it for offline checks, but it's useful for future reference).
2οΈβ£ Add Dependencies
In your app-level build.gradle
, add the Play Integrity dependency:
dependencies { implementation 'com.google.android.play:integrity:1.3.0' }
Sync the project after adding the dependency.
3οΈβ£ Implement Play Integrity API Locally in Android App
Since you donβt have a backend, you can directly verify the response inside the app.
π Example Code for Offline Play Integrity Check
Modify your MainActivity.java
or MainActivity.kt
:
Java Code:
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.play.core.integrity.IntegrityManager;
import com.google.android.play.core.integrity.IntegrityManagerFactory;
import com.google.android.play.core.integrity.IntegrityTokenRequest;
import com.google.android.play.core.integrity.StandardIntegrityManager;
import com.google.android.play.core.integrity.StandardIntegrityManagerFactory;
import com.google.android.play.core.integrity.StandardIntegrityResponse;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "PlayIntegrity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkAppIntegrity();
}
private void checkAppIntegrity() {
StandardIntegrityManager integrityManager = StandardIntegrityManagerFactory.create(this);
ExecutorService executorService = Executors.newSingleThreadExecutor();
integrityManager.request(StandardIntegrityManager.standardIntegrityRequest())
.addOnSuccessListener(executorService, response -> handleIntegrityResponse(response))
.addOnFailureListener(executorService, e -> Log.e(TAG, "Integrity check failed", e));
}
private void handleIntegrityResponse(StandardIntegrityResponse response) {
if (response.isPlayLicensed()) {
Log.d(TAG, "App is installed from Google Play Store β
");
} else {
Log.e(TAG, "App is NOT from Google Play Store β");
}
if (response.isBasicIntegrityMet()) {
Log.d(TAG, "Device integrity is valid β
");
} else {
Log.e(TAG, "Device integrity check failed β");
}
if (response.isStrongIntegrityMet()) {
Log.d(TAG, "Device meets strong integrity standards β
");
} else {
Log.e(TAG, "Device may be rooted or running an emulator β");
}
}
}
π Kotlin Version:
kotlinimport android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import com.google.android.play.core.integrity.StandardIntegrityManager
import com.google.android.play.core.integrity.StandardIntegrityManagerFactory
import com.google.android.play.core.integrity.StandardIntegrityResponse
import java.util.concurrent.Executors
class MainActivity : AppCompatActivity() {
private val TAG = "PlayIntegrity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
checkAppIntegrity()
}
private fun checkAppIntegrity() {
val integrityManager: StandardIntegrityManager = StandardIntegrityManagerFactory.create(this)
val executorService = Executors.newSingleThreadExecutor()
integrityManager.request(StandardIntegrityManager.standardIntegrityRequest())
.addOnSuccessListener(executorService) { response: StandardIntegrityResponse ->
handleIntegrityResponse(response)
}
.addOnFailureListener(executorService) { e: Exception ->
Log.e(TAG, "Integrity check failed", e)
}
}
private fun handleIntegrityResponse(response: StandardIntegrityResponse) {
if (response.isPlayLicensed) {
Log.d(TAG, "App is installed from Google Play Store β
")
} else {
Log.e(TAG, "App is NOT from Google Play Store β")
}
if (response.isBasicIntegrityMet) {
Log.d(TAG, "Device integrity is valid β
")
} else {
Log.e(TAG, "Device integrity check failed β")
}
if (response.isStrongIntegrityMet) {
Log.d(TAG, "Device meets strong integrity standards β
")
} else {
Log.e(TAG, "Device may be rooted or running an emulator β")
}
}
}
β 4οΈβ£ How It Works (Offline Mode)
The Play Integrity API will check:
β If the app is installed from Google Play (Play License Check)
β If the device is secure (Basic Integrity)
β If the device is unmodified (Strong Integrity)
You can use this information to restrict app features if the integrity check fails.
π What Can You Do Without a Backend?
πΉ Allow full access only if the app passes all checks
πΉ Show warnings if the app is not from Play Store
πΉ Disable features for rooted/emulated devices
β οΈ Limitations of Offline Play Integrity Check
β No server-side verification (less secure)
β Cannot detect advanced fraud (like app repackaging)
β Integrity tokens cannot be stored securely
For better security, it's recommended to implement a backend to validate the integrity token.
π― Conclusion
β If you want a simple offline Play Integrity check, use StandardIntegrityManager
.
β This method helps prevent app piracy and detect rooted devices.
β For better security, use a backend to verify the token properly.
This is how you can use Play Integrity API for an offline Android app. π Let me know if you have any doubts! π
- Get link
- X
- Other Apps
Comments
Post a Comment