50

I want to create an application which has the following functionality. It should save its .apk file to the sdcard. Imagine I have a Button. On clicking it I have to save the .apk file of the application.

Cœur
  • 37,241
  • 25
  • 195
  • 267
KK_07k11A0585
  • 2,381
  • 3
  • 26
  • 33
  • 2
    Not sure why you're getting down-voted. While it's a bit of a weird thing to want to do, it seems a valid question to me. It'd be interesting to hear why you want to do this though. – kabuko Nov 29 '11 at 07:13
  • Up vote for you , interesting question over here – xDragonZ Nov 29 '11 at 07:19
  • Do you mean, you have a list of application which is installed on the device and when you click on it, it should save that application's apk to sdcard. – aNi Nov 29 '11 at 07:31
  • Hi aNi , i want exactly that but it would be better if i can implement the above thing also I can do that using this app https://market.android.com/details?id=xcxin.filexpert&feature=search_result#?t=W251bGwsMSwxLDEsInhjeGluLmZpbGV4cGVydCJd – KK_07k11A0585 Nov 29 '11 at 07:57
  • If he can do this for an installed app. Why can't we implement the above functionality – KK_07k11A0585 Nov 29 '11 at 07:57
  • 1
    Up vote for you. It is real question with, by the way, great answer! Thank's for both of you. – Borg8 May 17 '12 at 14:44

5 Answers5

62

It is easy to do that..

  1. First you get all installed applications,
  2. For each one, get public source directory.
  3. copy the file to the SDCard.

Note: No need to be rooted.

Here is the snippt code:

final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> apps = getPackageManager().queryIntentActivities(mainIntent, 0);
for (ResolveInfo info : apps) {
    File file = new File(info.activityInfo.applicationInfo.publicSourceDir);
    // Copy the .apk file to wherever
}
rekire
  • 47,260
  • 30
  • 167
  • 264
Bassel Kh
  • 1,941
  • 1
  • 19
  • 30
  • @Bassel Kh : can you help me how can i copy my apk in my sdcard pls. refer my question [http://stackoverflow.com/questions/21058564/take-backup-of-all-install-apk-file-into-sdcard-programitically-in-android](http://stackoverflow.com/questions/21058564/take-backup-of-all-install-apk-file-into-sdcard-programitically-in-android). – Harshal Kalavadiya Jan 11 '14 at 05:41
  • Thank you! Works for me. – DmitryKanunnikoff Feb 21 '17 at 19:17
  • I'm not sure, I haven't work in Android since that time – Bassel Kh Jun 06 '17 at 08:27
4

It's easy to find and and get all the installed app's apk file through a simple function. Below I wrote a method that return a HashMap Object what holds all the installed apk file's absolute path with their corresponding package name.. I hope it will be very useful to you.

private HashMap<String, String> getAllInstalledApkFiles(Context context) {
        HashMap<String, String> installedApkFilePaths = new HashMap<>();

        PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> packageInfoList = packageManager.getInstalledPackages(PackageManager.SIGNATURE_MATCH);

        if (isValid(packageInfoList)) {
            for (PackageInfo packageInfo : packageInfoList) {
                ApplicationInfo applicationInfo;

                try {
                    applicationInfo = getApplicationInfoFrom(packageManager, packageInfo);

                    String packageName = applicationInfo.packageName;
                    String versionName = packageInfo.versionName;
                    int versionCode = packageInfo.versionCode;

                    File apkFile = new File(applicationInfo.publicSourceDir);
                    if (apkFile.exists()) {
                        installedApkFilePaths.put(packageName, apkFile.getAbsolutePath());
                        LogHelper.d(getClass(), packageName + " = " + apkFile.getName());
                    }
                } catch (PackageManager.NameNotFoundException error) {
                    error.printStackTrace();
                }
            }
        }

        return installedApkFilePaths;
    }

private boolean isValid(List<PackageInfo> packageInfos) {
        return packageInfos != null && !packageInfos.isEmpty();
    }

Now you call the following method to get the apk file for a particular package name :

public File getApkFile(Context context, String packageName) {
        HashMap<String, String> installedApkFilePaths = getAllInstalledApkFiles(context);
        File apkFile = new File(installedApkFilePaths.get(packageName));
        if (apkFile.exists()) {
            return apkFile;
        }

        return null;
    }

and getApplicationInfoFrom method:

private ApplicationInfo getApplicationInfoFrom(PackageManager packageManager, PackageInfo packageInfo) {
    return packageInfo.applicationInfo;
}
Sayed Abolfazl Fatemi
  • 3,678
  • 3
  • 36
  • 48
Shiba Prasad J.
  • 387
  • 2
  • 7
  • Missing Line of a code .... private ApplicationInfo getApplicationinfofrom(PackageManager packageManager, PackageInfo packageInfo) { return packageInfo.applicationInfo; } – Sohaib Aslam Jan 14 '18 at 20:34
  • Very useful, I knew there had to be such a thing. This is what APK extractors do I think, usually have one installed. I don't have a Java sandbox at the moment. – Alan Corey Apr 22 '18 at 02:35
3

I don't know of an official way. However, it seems like the APK is stored in /data/app with the filename of your.package.name-#.apk where your.package.name is your package name (e.g. com.google.earth) and # is usually 1 or 2, but I imagine it could go up more. Unless your device is rooted, you don't have permissions to list the files in /data/app but you should have read access to the actual file. You can try to copy that file (start with 1 and increment until you find it) to the SD card.

Alternatively, if you have internet access, you could store the APK in some web location and download it to SD card directly.

kabuko
  • 36,028
  • 10
  • 80
  • 93
  • Kabuko, Firstly thanks a lot for your answer. I have also tried that by using Super One click but i cant implement that. However, i am able to get the .apk of any application including apps from Android Market using the following https://market.android.com/details?id=xcxin.filexpert&feature=search_result#?t=W251bGwsMSwxLDEsInhjeGluLmZpbGV4cGVydCJd app from market – KK_07k11A0585 Nov 29 '11 at 08:00
0

You can use the PackageManager and ApplicationInfo class for this purpose.

List<ApplicationInfo> packagelist = new ArrayList<>();
File file;
PackageManager packageManager = getPackageManager();
packagelist = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
if (packagelist.size() > 0) {
        for (int i = 0; i < packagelist.size(); i++) {
            file = new File(packagelist.get(i).publicSourceDir);
   }

This is how you can get the publicSourceDir of every apk. Then you can just copy that apk in a particular folder. No ROOT required in this case.

I have created a similar app to extract the apk from both System and Installed apps. It has many more features. Attaching the link of source code and app here . Have a look at them.

View app on Google PlayStore

View Source Code on Github

Mrudul Tora
  • 715
  • 8
  • 14
0

Here's the simplest way I know on how to get the path to your applications apk file:

String classname = "com.mycompany.app.MyActivity";
String apkPath = activity.getApplication().getPackageManager().getApplicationInfo(classname, 0).sourceDir

classname is the name of your application Activity or the AppCompatActivity class.

This call would return something like this:

/data/app/com.mycompany.app.MyActivity-89lzYNI0vTAV056RT-WHBw==/base.apk

You will be able to read the file but you won't be able to write since its protected. And of course, base.apk is a jar.

Tom Rutchik
  • 1,183
  • 1
  • 12
  • 15