I have an android app and I have some private information and when people run on emulators my android app they can see that information. Does anyone have a solution on how to block emulators from accessing my APK?
You can create class that extends android.app.Application. In that class, you can throw runtime exception to crash the app, if app is running on emulator.
Note that, there's no bulletproof solution as anyone can decompile your app with free tools and remove those checks easily.
import android.app.Application;
import android.os.Build;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
if (isEmulator()) throw new IllegalStateException();
}
private boolean isEmulator() {
return (Build.BRAND.startsWith("generic") && Build.DEVICE.startsWith("generic"))
|| Build.FINGERPRINT.startsWith("generic")
|| Build.FINGERPRINT.startsWith("unknown")
|| Build.HARDWARE.contains("goldfish")
|| Build.HARDWARE.contains("ranchu")
|| Build.MODEL.contains("google_sdk")
|| Build.MODEL.contains("Emulator")
|| Build.MODEL.contains("Android SDK built for x86")
|| Build.MANUFACTURER.contains("Genymotion")
|| Build.PRODUCT.contains("sdk_google")
|| Build.PRODUCT.contains("google_sdk")
|| Build.PRODUCT.contains("sdk")
|| Build.PRODUCT.contains("sdk_x86")
|| Build.PRODUCT.contains("vbox86p")
|| Build.PRODUCT.contains("emulator")
|| Build.PRODUCT.contains("simulator");
}
}
Also, do not forget to add that class to the <application> tag of your AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.natigbabayev.stackoverflow">
<application
...
android:name=".App">
...
</application>
</manifest>
I hope my answer helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With