If you want to restrict usage of your app in the rooted device then you have to think of 2 points:
1) Restrict downloading of the app in the rooted device.
2) Restrict sideloading of the app in the rooted device.
Follow below steps to restrict downloading from the play store:
1) Go to play store console.
2) On the left side menu, go to release management.
3) In that, go to device catalog.
4) Then you will get 3 tab options, go to Excluded devices.
5) You will get an option to specify exclusion rules. click on manage exclusion rules.
6) You can see a chooser for SafetyNet exclusion. choose the option: exclude devices that don't pass basic integrity, as well as devices that are uncertified by google.
Follow below steps to restrict the sideloading of the app:
1) Obtain an API key using: https://developer.android.com/training/safetynet/attestation.html#obtain-api-key
2) Add safetynet dependency in your gradle file.
implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
3) I have put below code in my BaseActivity which my other activities extend, so If a hacker with the rooted device tries to sideload and tries to enter at any activity then below code executes.
private void ifGooglePlayServicesValid() {
if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(getApplicationContext())
== ConnectionResult.SUCCESS) {
// The SafetyNet Attestation API is available.
callSafetyNetAttentationApi();
} else {
// Prompt user to update Google Play services.
}
}
private void callSafetyNetAttentationApi() {
SafetyNet.getClient(this).attest(generateNonce(), SAFETY_NET_CHECK_API_KEY)
.addOnSuccessListener(this,
response -> {
// Use response.getJwsResult() to get the result data.
String jwsResponse = decodeJws(response.getJwsResult());
try {
JSONObject attestationResponse = new JSONObject(jwsResponse);
boolean ctsProfileMatch = attestationResponse.getBoolean("ctsProfileMatch");
boolean basicIntegrity = attestationResponse.getBoolean("basicIntegrity");
if (!ctsProfileMatch || !basicIntegrity) {
// this indicates it's rooted/tampered device
}
} catch (JSONException e) {
// json exception
}
})
.addOnFailureListener(this, e -> {
// An error occurred while communicating with the service.
});
}
public String decodeJws(String jwsResult) {
if (jwsResult == null) {
return null;
}
final String[] jwtParts = jwsResult.split("\\.");
if (jwtParts.length == 3) {
return new String(Base64.decode(jwtParts[1], Base64.DEFAULT));
} else {
return null;
}
}
private byte[] generateNonce() {
byte[] nonce = new byte[16];
new SecureRandom().nextBytes(nonce);
return nonce;
}
SAFETY_NET_CHECK_API_KEY is the key obtained in the 1st step.
The Attestation API returns a JWS response which looks like:
{
"timestampMs": 9860437986543,
"nonce": "R2Rra24fVm5xa2Mg",
"apkPackageName": "com.package.name.of.requesting.app",
"apkCertificateDigestSha256": ["base64 encoded, SHA-256 hash of the
certificate used to sign requesting app"],
"ctsProfileMatch": true,
"basicIntegrity": true,
}
The JWS response contains ctsProfileMatch and basicIntegrity that indicate the device status.
Reference: https://developer.android.com/training/safetynet/attestation.html