Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the .apk file of an application programmatically

Tags:

android

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.

like image 667
KK_07k11A0585 Avatar asked Nov 29 '11 07:11

KK_07k11A0585


People also ask

How do I get an APK file from an app?

From the Google Play Store, copy the URL of the app you want to extract. Next, head to this web tool in your browser and paste the URL. Select “Generate Download Link.” This web app will then automatically extract the APK file and provide you with the relevant link to access it.

How do I find my APK code?

May be the easy one to see the source: In Android studio 2.3, Build -> Analyze APK -> Select the apk that you want to decompile . You will see it's source code.


3 Answers

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
}
like image 168
Bassel Kh Avatar answered Oct 19 '22 07:10

Bassel Kh


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;
}
like image 21
Shiba Prasad J. Avatar answered Oct 19 '22 06:10

Shiba Prasad J.


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.

like image 3
kabuko Avatar answered Oct 19 '22 07:10

kabuko