While in most cases you can get the store name by including a check similar to this:
final PackageManager packageManager = getPackageManager();
try {
final ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
if ("com.android.vending".equals(packageManager.getInstallerPackageName(applicationInfo.packageName))) {
// App was installed by Play Store
} else if ("com.amazon.venezia".equals(packageManager.getInstallerPackageName(applicationInfo.packageName))) {
// App was installed by Amazon Appstore
} else {
// App was installed from somewhere else
}
} catch (final NameNotFoundException e) {
e.printStackTrace();
}
"com.android.vending" is Google Play Store and
"com.amazon.venezia" is the Amazon Appstore, and
null when it was sideloaded
The results could be unreliable however, as for example during beta testing a store might not set this value, and besides it's possible to sideload your app specifying the installer's package name that could be interpreted as a store name:
adb install -i <INSTALLER_PACKAGE_NAME> <PATH_TO_YOUR_APK>
You might want to consider having different application IDs for different stores, for example "com.example.yourapp" for Google and "com.example.yourapp.amazon" for Amazon -- you can easily set those in your Gradle script.
PackageManagerandgetInstallerPackageName()will tell you what the installer was. You would have to run experiments to see what that returns under your different scenarios.