title: Why getInstallerPackageName() returns null sometimes?
body: <p>My app can only be downloaded by Google Play but unfortunately it is also available in some piracy sites. As I was losing downloads, I checked in code the installation source. If it is &quot;com.android.vending&quot; (Google Play) then the app runs as normal but if it is another one then the app stops running and prompts the user to uninstall it and shows a link to the Google Play app.</p>
  <p>To do that I run this code:</p>
  <pre><code>PackageManager oPackageManager = getPackageManager();
  String sMarket = oPackageManager.getInstallerPackageName(getPackageName());
  if (sMarket == null) sMarket = &quot;&quot;; 
  if (sMarket.equalsIgnoreCase(&quot;com.android.vending&quot;) == true)         
      return true; //It has been installed from Play Store.
  else
      return false; //It has NOT been installed from Play Store.
  </code></pre>
  <p>The problem is that a quite a few users have reported that they installed the app from Google Play weeks or months ago. Suddenly, without doing anything, the app started displaying that it was NOT installed from Google Play. This forced the users to uninstall and reinstall again.
  I checked the logs and I saw that getInstallerPackageName() was returning null.
  I asked them if the system got any update or similar and all of them said that they didn't noticed that.
  So now I am afraid something similar starts happening to the rest of my users. Of course, forcing users to uninstall the app is terrible if they installed it correctly. But how can I check to avoid piracy?</p>
  <p>My real question is: Why is getInstallerPackageName() returning null from no reason?</p>