7

I want to get the exact file name of a program if I already know the package name of the target apk. For instance, if I know the package name of my apk, which is com.packagename, how can I get the exact path and file name of that package? Btw, i don't want to get just MY apk location, i want the location of any package name i apply. SystemTuner pro is able to do this so i know it is possible, just not sure how.

Thanks guys!

Seth
  • 1,769
  • 4
  • 26
  • 39

4 Answers4

7
/**
 * Get the apk path of this application.
 * @param context any context (e.g. an Activity or a Service)
 * @return full apk file path, or null if an exception happened (it should not happen)
 */
public static String getApkName(Context context) {
    String packageName = context.getPackageName();
    PackageManager pm = context.getPackageManager();
    try {
        ApplicationInfo ai = pm.getApplicationInfo(packageName, 0);
        String apk = ai.publicSourceDir;
        return apk;
    } catch (Throwable x) {
    }
    return null;
}

EDIT In defense of catch (Throwable x) in this case. At first, now it is well-known that Checked Exceptions are Evil. At second, you cannot predict what may happen in future versions of Android. There already is a trend to wrap checked exceptions into runtime exceptions and re-throw them. (And a trend to do silly things that were unthinkable in the past.) As to the children of Error, well, if the package manager cannot find the apk that is running, it is the kind of problems for which Errors are thrown. Probably the last lines could be

    } catch (Throwable x) {
        return null;
    }

but I do not change working code without testing it.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • Guys, I've seen this downvoted. Does it still work on modern Androids? – 18446744073709551615 Jan 06 '18 at 12:49
  • It does work on newer devices (tried on Android X emulator and it worked). Though, there's a thing which I believe it's not that good to be there which is `Throwable`. That's not recommended to use (only in rarer cases, I'd say). I'd put there `PackageManager.NameNotFoundException` as getApplicationInfo requires (could be that the reason for the downvotes you saw?). Other than that it's perfect. Thank you. – Edw590 Jun 11 '20 at 21:12
4

PackageManager.getPackageInfo() returns information about the package, and PackageInfo.applicationInfo field has required information about the application.

Y2i
  • 3,748
  • 2
  • 28
  • 32
3

Well, i would like to mark Yuri as the answer but i already knew about that stuff. So I went through each and every option from PackageManager.ApplicationInfo and found .publicSourceDir

So a complete answer with code to my question would be

PackageManager pm = getPackageManager();
    try {
        ApplicationInfo ai = pInfo.getApplicationInfo(<packageName here>, 0);
        String sourceApk = ai.publicSourceDir;
    } catch (NameNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

So thanks again guys, got my brain goin once again Love StackOverflow!

Seth
  • 1,769
  • 4
  • 26
  • 39
  • 1
    If you "already knew about that stuff" you might have said so in your question and gotten an answer more specific to what you did not know. – Chris Stratton Jun 06 '13 at 22:51
  • Seith, you asked about the exact file name of a program, and the exact path and file name of the package. Considering this, ApplicationInfo was the best answer I could think of, I could not be more precise :) – Y2i Jun 07 '13 at 00:33
  • 1
    @Yuri your answer truly did help! I will select your answer as if it werent for your answer i wouldn't have gone back through that class. I looked through it too quickly and missed it :P I was looking for things like "directory" or "getDir" but i missed "publicSourceDir" so thanks again! – Seth Jun 07 '13 at 01:24
  • Thank you Seth! I just tried to explain why I didn't go into the details of ApplicationInfo, no complaints there. And I do appreciate you selecting my answer. – Y2i Jun 07 '13 at 06:13
  • According to the JavaDoc in the ApplicationInfo sources - you want 'sourceDir' not 'publicSourceDir' (sourceDir comment says "Full path to the base APK for this application.") – Nick Cardoso Jan 04 '18 at 09:40
1

in above answer need change pInfo to pm

like this

PackageManager pm = getPackageManager();
try {
    ApplicationInfo ai = pm.getApplicationInfo(<packageName here>, 0);
    String sourceApk = ai.publicSourceDir;
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

this answer by Seth

ibrahim
  • 9
  • 1
  • 3