GetInstallerPackageName returns null

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

abstract String getInstallerPackageName(String packageName) 

Get the package name of the application that installed the package.

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

 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 is returning null on a real device.

Please let me know if we have other ways to get the application installation source.

+4
source share
3 answers

As for your question, how was the application installed on the device? Through "adb install" it will be zero.

Using the following, you can override it with everything you want for testing purposes.

 pm install -i installername com.example.package 

GooglePlay should be set to com.google.android.feedback. I'm not sure what values ​​other app stores use.

+5
source

if you want to test this you need to save apk on the device using

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

then go to adb shell by typing "adb shell" and pressing Enter in the terminal, then install the following command

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

when installing the application in the above method, calling getInstallerPackageName () returns the name of the installer specified in the above command.

@mattmook credits for the pm install command.

+5
source

I tested this API method with both an emulator and real devices. In most cases, it returns null. Return com.android.vending only if the application is installed from the official Google Play.

With apps in the local app market for China, such as AppChina and wandoujia , null .

Below is a snippet of code and a log: Github Gist

+2
source

Source: https://habr.com/ru/post/1436271/


All Articles