Reputation: 1818
I'm trying to get the path of my current running apk on android. I'm using System.getProperty("user.dir"). But it gives out a "/", which is the root directory of the Android system. Am i missing something?
Upvotes: 3
Views: 7642
Reputation: 10073
Check the Context class. The functions you want are:
and so forth.
Upvotes: 1
Reputation: 62864
As the documentation says, the user.dir
property is the user working directory, which is not necessarily the same as the directory where your apk is placed.
Anyway, the List<ApplicationInfo> PackageManager.getInstalledApplications()
will give you a list of the installed applications, and ApplicationInfo.sourceDir
is the path to the .apk
file.
Here's some sample code:
PackageManager pm = getPackageManager();
for (ApplicationInfo app : pm.getInstalledApplications(0)) {
System.out.println("SourceDir: " + app.sourceDir);
}
The above will give you the sourcepath for all the installed apks.
The example is taken from here.
Upvotes: 4
Reputation: 34657
The ApplicationInfo class is your answer:
PackageManager pm = getPackageManager();
for (ApplicationInfo app : pm.getInstalledApplications(0)) {
if (app.packageName.equals(appPackage) { Log.i( app.sourceDir); }
}
Hope that sorts you.
Upvotes: 0
Reputation: 82553
Your apk is located at /data/app/<your_package_name>
after installation on the internal memory. This path is consistent across Android, and you don't need to use System
.
Upvotes: 1