zomba
zomba

Reputation: 11

Check if package name changed

I want to protect my APK from reverse engineering, by showing Toast, or do something if package name changed, now if package changed the app will stop working.

if (getPackageName().compareTo("com.apk.example") != 0) {

    String error = null; 
    error.getBytes();
}

Upvotes: 1

Views: 535

Answers (1)

ישו אוהב אותך
ישו אוהב אותך

Reputation: 29794

You need to check both your package name and application id to make sure your app haven't been tampered with:

String yourPackageName = "com.apk.example"; // android package name
String packageName = getApplicationContext().getPackageName();

// can be different from your package name if you're using flavor
// in app.build.gradle, 
String yourApplicationId = "com.apk.example"; 

if(packageName.equals(yourPackageName) && BuildConfig.APPLICATION_ID.equals(yourApplicationId)) {
   // no problem here
} else {
   // app is tampered, do something
}

Upvotes: 2

Related Questions