5

In my Android app I wanted to check the source of app installation. I looked at the Android documentation and found the following API as part of the PackageManager class:

abstract String getInstallerPackageName(String packageName) 

Retrieve the package name of the application that installed a package.

Then I used the following code in the onCreate method of MyActivity:

if(Build.VERSION.SDK_INT >= 11) 
{
    PackageManager myapp= this.getPackageManager(); 
    String installer = myapp.getInstallerPackageName("com.MyPackage");
    if(installer == null)
    {
        Toast.makeText(getApplicationContext(), MyActivity.this.getString(R.string.invalidsource), Toast.LENGTH_SHORT).show();
        MyActivity.this.finish();
    }
}

But it looks like this API returns null on a real device.

Please let me know if we have any other means of getting the source of app installation.

stkent
  • 19,772
  • 14
  • 85
  • 111
user1123931
  • 479
  • 1
  • 8
  • 24

3 Answers3

9

if you want to test this the way is to store the apk on the device by using the

adb push 'test.apk' '/sdcard/Download'

then go to the adb shell by typing in 'adb shell' and hitting enter in the terminal then install using the following command

pm install -i 'installernameyouwant' /sdcard/Download/test.apk

when installing the app in above method the call to getInstallerPackageName() returns the installer name you gave in the above command

Yasir
  • 453
  • 1
  • 6
  • 15
5

With regard to your question how was the app installed onto the device? Through "adb install" it will be null.

Using the following you should be able to override it to whatever you want for testing purposes.

pm install -i installername com.example.package

GooglePlay should have a value of com.google.android.feedback. I'm not sure what values other app stores use.

appmattus
  • 2,788
  • 25
  • 16
  • another possible way is to use "setInstallerPackageName" , right? here's a link: http://developer.android.com/reference/android/content/pm/PackageManager.html#setInstallerPackageName(java.lang.String, java.lang.String) . I wonder which permission is needed. – android developer Dec 05 '13 at 21:21
  • beware the method you mention says _"A SecurityException will be thrown if installerPackageName is not signed with the same certificate as the calling application."_ i.e. you cant pretend an app has been installed from GooglePlay – Dori Mar 26 '14 at 16:42
2

I tested this API method with both emulator and real devices. Most of the time, it returns null. Only return com.android.vending when the app is installed from official Google Play.

With China local app market apps, such as AppChina and wandoujia, null is returned.

Code snippet and log is posted here: Github Gist

lucas
  • 2,756
  • 19
  • 15