title: Is it possible to check if 2 APK files are the same, without comparing byte-by-byte (or use algorithms that do it)?
body: <h2>Background</h2>
  
  <p>I want to check if 2 APK files are identical (or have a very high chance of being identical) in the minimal work on the device.</p>
  
  <p>The reason for this, is that I have an app (<a href="https://play.google.com/store/apps/details?id=com.lb.app_manager" rel="nofollow noreferrer"><strong>here</strong></a>) that allows to install apps using APK files, so I want to check if the installed app is already the same as the APK file. This includes using root privilege for background install. So far, what I did was to check the package name and the version code, but I want to know if there is a little better way to perform this check.</p>
  
  <h2>The problem</h2>
  
  <p>By "minimal work", I mean minimal reads of the APK file itself. This means that going through all of the bytes of each of the APK files is the most correct way to check if files are identical, but if there is a signature for the APK, that identifies it relatively uniquely, that would be much better.</p>
  
  <p>For example, I know you can do MD5 check on both files, and if it's the same for both, it's a really good chance both are the same, but such an algorithm, along with other similar algorithms (sha1 etc...), go over the entire file, so this is about the same as what I wrote before. It could take a while for large APK files.</p>
  
  <h2>What I've found</h2>
  
  <p>What I do know is that comparing the package name and the version number gives a clue about whether the APK files are of the same app and version, but I think that Android-Studio knows more about the APK files, because sometimes it asks if we want to uninstall the installed app even though they have the same version, and it doesn't seem like it takes it a lot of time to ask this.</p>
  
  <p>I could add a file size check too, which should be relatively as fast as the package name and the version number, but maybe there is more ...</p>
  
  <p>Here's a sample code of what I did:</p>
  
  <pre><code>public static boolean areApksMostProbablyIdentical(PackageInfo packageInfo, PackageInfo packageInfo2) {
      if (packageInfo == null || packageInfo2 == null)
          return packageInfo == packageInfo2;
      if (!packageInfo.packageName.equals(packageInfo2.packageName))
          return false;
      if (packageInfo.versionCode != packageInfo2.versionCode)
          return false;
      final File file = new File(packageInfo.applicationInfo.publicSourceDir);
      final File file2 = new File(packageInfo2.applicationInfo.publicSourceDir);
      if (file.exists() &amp;&amp; file2.exists())
          return file.length() == file2.length();
      return true;
  }
  </code></pre>
  
  <h2>The question</h2>
  
  <p>My question is: </p>
  
  <p>Is it possible to perform a "good-enough" check on both files, avoiding comparing all bytes, to see if 2 APK files are the same? </p>