40

I am getting a list of ApplicationInfo Objects with packageManager.getInstalledApplications(0) and attempting to categorize them by whether or not they are a system application.

For a while I have been using the technique described here, however after seeing that in my application, some of the apps were not in the non-system apps list (such as Facebook, which when available asks the system to install itself on the SD card). After next reading the actual documentation for ApplicationInfo.FLAG_SYSTEM, and understanding that it doesn't actually filter system apps, I am now looking for a new approach.

My guess is that there is a large gap between UIDs of System and non-system apps that I can gather to make this distinction, but as of yet I have not found an answer. I also looked into other flags, such as ApplicationInfo.FLAG_EXTERNAL_STORAGE, however I am supporting API 1.5.

Does anyone have a real solution to this (not involving FLAG_SYSTEM)?

Community
  • 1
  • 1
Phil
  • 34,061
  • 21
  • 117
  • 154

13 Answers13

31
PackageManager pm = mcontext.getPackageManager();
List<PackageInfo> list = pm.getInstalledPackages(0);

for(PackageInfo pi : list) {
    ApplicationInfo ai = pm.getApplicationInfo(pi.packageName, 0);

    System.out.println(">>>>>>packages is<<<<<<<<" + ai.publicSourceDir);

    if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
        System.out.println(">>>>>>packages is system package"+pi.packageName);          
    }
}
Flow
  • 22,048
  • 13
  • 91
  • 147
Nitin
  • 1,415
  • 13
  • 16
  • @Ocool, I specifically state in my question that `FLAG_SYSTEM` does not actually work, so this is not what I am looking for. – Phil Jan 09 '12 at 16:06
  • @Phil I know FLAG_SYSTEM doesnt work on its own that why the "&" condition, try it, it worked for me. – Nitin Jan 11 '12 at 05:18
  • @Ocool, this is what I have been using for a while, and it *appears* to work, until you notice that some apps are missing. The fact is that `FLAG_SYSTEM` simply gets the apps that are [installed in the device's system image](http://developer.android.com/reference/android/content/pm/ApplicationInfo.html#FLAG_SYSTEM). This means that it will not get apps installed on the SD card, etc. The best way to get **all** the apps is unfortunately to check their directory ("/data/apps"). – Phil Jan 11 '12 at 17:18
  • 1
    @Phil Apps on the SD Card are always user apps and updates to system apps also go to /data/apps – sergio91pt Feb 02 '13 at 19:11
28

I was under the impression that all apps in the system image are system apps (and normally installed in /system/app).

If FLAG_SYSTEM is only set to system applications, this will work even for apps in external storage:

boolean isUserApp(ApplicationInfo ai) {
    int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
    return (ai.flags & mask) == 0;
}

An alternative is to use the pm command-line program in your phone.

Syntax:

pm list packages [-f] [-d] [-e] [-s] [-3] [-i] [-u] [--user USER_ID] [FILTER]

pm list packages: prints all packages, optionally only
  those whose package name contains the text in FILTER.  Options:
    -f: see their associated file.
    -d: filter to only show disbled packages.
    -e: filter to only show enabled packages.
    -s: filter to only show system packages.
    -3: filter to only show third party packages.
    -i: see the installer for the packages.
    -u: also include uninstalled packages.

Code:

ProcessBuilder builder = new ProcessBuilder("pm", "list", "packages", "-s");
Process process = builder.start();

InputStream in = process.getInputStream();
Scanner scanner = new Scanner(in);
Pattern pattern = Pattern.compile("^package:.+");
int skip = "package:".length();

Set<String> systemApps = new HashSet<String>();
while (scanner.hasNext(pattern)) {
    String pckg = scanner.next().substring(skip);
    systemApps.add(pckg);
}

scanner.close();
process.destroy();

Then:

boolean isUserApp(String pckg) {
    return !mSystemApps.contains(pckg);
}
sergio91pt
  • 1,398
  • 16
  • 21
18

You can check the signature of application which it signed with system. Like below

/**
 * Match signature of application to identify that if it is signed by system
 * or not.
 * 
 * @param packageName
 *            package of application. Can not be blank.
 * @return <code>true</code> if application is signed by system certificate,
 *         otherwise <code>false</code>
 */
public boolean isSystemApp(String packageName) {
    try {
        // Get packageinfo for target application
        PackageInfo targetPkgInfo = mPackageManager.getPackageInfo(
                packageName, PackageManager.GET_SIGNATURES);
        // Get packageinfo for system package
        PackageInfo sys = mPackageManager.getPackageInfo(
                "android", PackageManager.GET_SIGNATURES);
        // Match both packageinfo for there signatures
        return (targetPkgInfo != null && targetPkgInfo.signatures != null && sys.signatures[0]
                .equals(targetPkgInfo.signatures[0]));
    } catch (PackageManager.NameNotFoundException e) {
        return false;
    }
}

You can get more code on my blog How to check if application is system app or not (By signed signature)

Pankaj Kumar
  • 78,055
  • 25
  • 161
  • 180
  • I have not had a chance to try this, but I really like this approach if it works - definitely a great way to do the comparison. – Phil Jul 08 '14 at 15:24
  • @Phil It would be my pleasure to know that it worked for your expectation. Thank you. – Pankaj Kumar Jul 09 '14 at 06:23
  • @PankajKumar : I have developed a system app for iBall tab and I used both methods as per your blog and I get return value as 'true' in both cases.(So I am hoping that my app really has become a system app :) ).Two doubts,(1)your second method 'isAppPreLoaded' does the same thing as 'isSystemApp' in addition to checking if app is preloaded or not,right? ;(2) What is the meaning of preloaded,does it mean that it checks whether the app resides in system/app or is it checking that the app came baked with the ROM or both? – Basher51 Aug 14 '14 at 04:51
  • 1
    @Basher51 Sometime applications are pre-installed, but those are not system app. Like you developed an application, signed with your certificate and added to custom AOSP code, that is signed by manufacturer certificate. In this case your app is pre-installed but that is not a system app (considering the certificate). So the last method at blog does both, first checks if application is pre-installed, if yes then checks for the signature of application. Hope I am clear now :)\ – Pankaj Kumar Aug 14 '14 at 05:40
  • @PankajKumar: Not clear fully.Why do you say that "In this case your app is pre-installed but that is not a system app (considering the certificate)" ? My app resides in system/app and is signed by the same certificates as that of the system.So isn't it a system app now.Also I ran both 'methods' as per your blog and gave a return value of 'true'.Hence doesn't that confirm that my app has become a system app? – Basher51 Aug 14 '14 at 05:47
  • 1
    @Basher51 Your application is system app. Look in this case, You build an application and signed by your certificate (not the same certificate which sign AOSP code). Till here your app is like a third party app. Right? Now you copy that apk into `system/app`. This step does not make your app as system app (there are too many API your app can not access, even if that is into system dir). For this it must signed by the same certificate AS YOU ARE DOING, and location must be /system/app. No worry about your case, you are doing the correct way. – Pankaj Kumar Aug 14 '14 at 05:56
  • @PankajKumar. I tried your method it works in some cases but for few packages it says not system app...like-"com.google.android.gms"," com.sec.android.app.launcher","com.google.android.googlequicksearchbox" are they not system app? – sandy Apr 20 '15 at 06:31
  • @sandy No they are not system apps. They are applications from Google and they are signed by Google's certificate not with the manufaturer's certificate. Even these application are installed on other location not into /system/app. Am I clear now? – Pankaj Kumar Apr 20 '15 at 07:37
  • @sandy `adb shell pm path com.google.android.gms` try into command window, and you will get the location of this package. – Pankaj Kumar Apr 20 '15 at 07:41
  • 1
    very interesting – Vlad Jul 07 '18 at 08:08
7

There are 2 type of Non - system applications :

  1. Apps downloaded from Google Play Store
  2. Preloaded apps by device manufacturer

This code will return a list of all above applications:

ArrayList<ApplicationInfo> mAllApp = 
        mPackageManager.getInstalledApplications(PackageManager.GET_META_DATA);

for(int i = 0; i < mAllApp.size(); i++) {
    if((mAllApp.get(i).flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
         // 1. Applications downloaded from Google Play Store
        mAllApp1.add(mAllApp.get(i));
    }

    if((mAllApp.get(i).flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
        // 2. Applications preloaded in device by manufecturer
        mAllApp1.add(mAllApp.get(i));
    }
}
scopchanov
  • 6,933
  • 10
  • 30
  • 56
Kushal
  • 6,695
  • 6
  • 48
  • 72
6

There is a little bit of misunderstanding here. For Android the notion of a "system app" is one that is install on the system image, it says nothing about what developer it came from. So, if an OEM decides to preload Facebook on to the system image, it is a system app and will continue to be so, regardless of where updates to the app get installed. They won't get installed on the system image, for sure, because it is read-only.

So ApplicationInfo.FLAG_SYSTEM is correct, but that doesn't seem to be the question you are asking. I think you're asking if a package is signed with the system certificate. Which is not necessarily a good indicator of anything, this may vary from device to device and some surprising components on vanilla Android are not signed with the system certificate, even though you might expect them to be.

In newer versions of Android there is a new path, /system/priv-app/ that attempts to be the install location for "real" system apps. Apps that are just pre-loaded on the system image then end up in /system/app/. See AOSP Privileged vs System app

Community
  • 1
  • 1
cyngus
  • 1,723
  • 13
  • 14
  • Nice perspective.I have created a system app and it resides in system/app .I did sign it with system keys(at least thats what I am hoping).But is there a way(programatically or otherwise) to fully confirm that my app has now become a real system app (which is signed with the same keys as that were baked in the ROM)? – Basher51 Aug 14 '14 at 04:11
6

Well, it's a sloppy solution in my opinion (what if /data/app isn't the apps directory on all devices?), but after a thorough search, this is what I have come up with:

for (ApplicationInfo ai : appInfo) {
    if (ai.sourceDir.startsWith("/data/app/")) {
        //Non-system app
    }
    else {
        //System app
    }
}
Phil
  • 34,061
  • 21
  • 117
  • 154
  • 11
    If you update a system app, it will put the update into the /data/app directory. But it still is a system app. – Ion Aalbers Nov 26 '12 at 14:01
  • @IonAalbers: I had this doubt too.Most places its written that system apps reside in system/app and user apps in data/app.Why is it that when system/app is updated then its apk is stored in data/app ?Could you pls explain a bit more? – Basher51 Aug 14 '14 at 04:04
  • 1
    @Basher51 The application which location is `/system/app/` does not mean that, it is system app. It means that application is pre-loaded. And for update application doubt, I can guess that, they have written this logic to keep `factory-reset` in mind. If application will be updated at same location, it will hard to find original application state while factory reset. – Pankaj Kumar Aug 14 '14 at 05:49
  • NOT WOKING AT ALL. 3rd party apps has also dir starts with `/data/app/` – Vlad Jul 07 '18 at 08:08
5

If an Application is a non-system application it must have a launch Intent by which it can be launched. If the launch intent is null then its a system App.

Example of System Apps: "com.android.browser.provider", "com.google.android.voicesearch".

For the above apps you will get NULL when you query for launch Intent.

PackageManager pm = getPackageManager();
List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
for(ApplicationInfo packageInfo:packages){
    if( pm.getLaunchIntentForPackage(packageInfo.packageName) != null ){
                String currAppName = pm.getApplicationLabel(packageInfo).toString();
               //This app is a non-system app
    }
}
Darshan Patel
  • 465
  • 1
  • 8
  • 14
5

This is a simplified and more efficient version of other responses listed here. It is more efficient if you just iterate directly over the ApplicationInfos.

    List<ApplicationInfo> applications = context.getPackageManager()
            .getInstalledApplications(PackageManager.GET_META_DATA);
    for(ApplicationInfo appInfo : applications){
        if((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0){
            // Not a system app
        }
    }
Tyler
  • 6,153
  • 3
  • 20
  • 14
3

Here are different possible ways to see if the app is a system app by its package name (used some of the codes in this post)

package com.test.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
import java.util.regex.Pattern;

import timber.log.Timber;


public class SystemAppChecker {
    private PackageManager packageManager = null;

    public SystemAppChecker(Context context) {
        packageManager = context.getPackageManager();
    }

    /**
     * Check if system app by 'pm' command-line program
     *
     * @param packageName
     *            package name of application. Cannot be null.
     * @return <code>true</code> if package is a system app.
     */
    public boolean isSystemAppByPM(String packageName) {
        if (packageName == null) {
            throw new IllegalArgumentException("Package name cannot be null");
        }
        ProcessBuilder builder = new ProcessBuilder("pm", "list", "packages", "-s");
        Process process = null;
        try {
            process = builder.start();
        } catch (IOException e) {
            Timber.e(e);
            return false;
        }

        InputStream in = process.getInputStream();
        Scanner scanner = new Scanner(in);
        Pattern pattern = Pattern.compile("^package:.+");
        int skip = "package:".length();

        Set<String> systemApps = new HashSet<String>();
        while (scanner.hasNext(pattern)) {
            String pckg = scanner.next().substring(skip);
            systemApps.add(pckg);
        }

        scanner.close();
        process.destroy();

        if (systemApps.contains(packageName)) {
            return true;
        }
        return false;
    }

    /**
     * Check if application is preloaded.
     *
     * @param packageName
     *            package name of application. Cannot be null.
     * @return <code>true</code> if package is preloaded.
     */
    public boolean isSystemPreloaded(String packageName) {
        if (packageName == null) {
            throw new IllegalArgumentException("Package name cannot be null");
        }
        try {
            ApplicationInfo ai = packageManager.getApplicationInfo(
                    packageName, 0);
            if (ai.sourceDir.startsWith("/system/app/") || ai.sourceDir.startsWith("/system/priv-app/")) {
                return true;
            }
        } catch (NameNotFoundException e) {
            Timber.e(e);
        }
        return false;
    }

    /**
     * Check if the app is system signed or not
     *
     * @param packageName
     *            package of application. Cannot be blank.
     * @return <code>true</code> if application is signed by system certificate,
     *         otherwise <code>false</code>
     */
    public boolean isSystemSigned(String packageName) {
        if (packageName == null) {
            throw new IllegalArgumentException("Package name cannot be null");
        }
        try {
            // Get packageinfo for target application
            PackageInfo targetPkgInfo = packageManager.getPackageInfo(
                    packageName, PackageManager.GET_SIGNATURES);
            // Get packageinfo for system package
            PackageInfo sys = packageManager.getPackageInfo(
                    "android", PackageManager.GET_SIGNATURES);
            // Match both packageinfo for there signatures
            return (targetPkgInfo != null && targetPkgInfo.signatures != null && sys.signatures[0]
                    .equals(targetPkgInfo.signatures[0]));
        } catch (PackageManager.NameNotFoundException e) {
            Timber.e(e);
        }
        return false;
    }

    /**
     * Check if application is installed in the device's system image
     *
     * @param packageName
     *            package name of application. Cannot be null.
     * @return <code>true</code> if package is a system app.
     */
    public boolean isSystemAppByFLAG(String packageName) {
        if (packageName == null) {
            throw new IllegalArgumentException("Package name cannot be null");
        }
        try {
            ApplicationInfo ai = packageManager.getApplicationInfo(
                    packageName, 0);
            // Check if FLAG_SYSTEM or FLAG_UPDATED_SYSTEM_APP are set.
            if (ai != null
                    && (ai.flags & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
                return true;
            }
        } catch (NameNotFoundException e) {
            Timber.e(e);
        }
        return false;
    }
}
Vinayak Bevinakatti
  • 38,839
  • 25
  • 103
  • 135
  • How can an app be "isSystemSigned"? Do I need the system key to do it? – lucky1928 Jun 11 '18 at 01:16
  • "isSystemSigned" means signing your apk with platform keys. Refer this for creating a development build - https://github.com/aosp-mirror/platform_build/tree/master/target/product/security – Vinayak Bevinakatti Jun 12 '18 at 21:04
  • If you have more questions, then these posts may answers some of those - https://stackoverflow.com/questions/37586255/signing-my-android-application-as-system-app/46729285#46729285 and https://stackoverflow.com/a/3651653/28557 – Vinayak Bevinakatti Jun 12 '18 at 21:12
1
if (!packageInfo.sourceDir.toLowerCase().startsWith("/system/"))
MiladCoder
  • 49
  • 2
  • 12
  • 2
    This is not necessarily true, you can always sign an app with platform key (given you have access to one) and install it with system **sharedUserId** and it would run as a system app – Monu Surana Sep 12 '16 at 23:10
1

If having an APK file and want to check is it System app or User installed a Simple logic:- System app Files are not writable

private boolean isSystemApkFile(File file){
   return !file.canWrite();
}
shivampip
  • 1,306
  • 11
  • 14
0

Here is an AppUtil I wrote for that purpose.
Usage example:

new AppsUtil(this).printInstalledAppPackages(AppsUtil.AppType.USER);

AppsUtil.java

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.util.Log;

public class AppsUtil
{
    public static final String  TAG = "PackagesInfo";
    private Context             _context;
    private ArrayList<PckgInfo> _PckgInfoList;

    public enum AppType 
    {
        ALL {
            @Override
            public String toString() {
              return "ALL";
            }
          },
        USER {
            @Override
            public String toString() {
              return "USER";
            }
          },
        SYSTEM {
            @Override
            public String toString() {
              return "SYSTEM";
            }
          }
    }

    class PckgInfo
    {
        private AppType appType;
        private String  appName     = "";
        private String  packageName = "";
        private String  versionName = "";
        private int     versionCode = 0;

        private void prettyPrint()
        {
            Log.i(TAG, appName + "\n  AppType: " + appType.toString() + "\n  Package: " + packageName + "\n  VersionName: " + versionName + "\n  VersionCode: " + versionCode);
        }
    }

    public AppsUtil(Context context)
    {
        super();
        this._context = context;
        this._PckgInfoList = new ArrayList<PckgInfo>();
    }

    public void printInstalledAppPackages(AppType appType)
    {
        retrieveInstalledAppsPackages();
        Log.i(TAG, "");
        for (int i = 0; i < _PckgInfoList.size(); i++)
        {
            if (AppType.ALL == appType)
            {
                _PckgInfoList.get(i).prettyPrint();
            }
            else
            {
                if (_PckgInfoList.get(i).appType == appType)
                    _PckgInfoList.get(i).prettyPrint();
            }
        }
    }

    public ArrayList<PckgInfo> getInstalledAppPackages(AppType appType)
    {
        retrieveInstalledAppsPackages();
        ArrayList<PckgInfo> resultPInfoList = new ArrayList<PckgInfo>();

        if (AppType.ALL == appType)
        {
            return _PckgInfoList;
        }
        else
        {
            for (int i = 0; i < _PckgInfoList.size(); i++)
            {
                if (_PckgInfoList.get(i).appType == appType)
                    resultPInfoList.add(_PckgInfoList.get(i));
            }
            return resultPInfoList;
        }
    }

    private void retrieveInstalledAppsPackages()
    {
        PackageManager pm = _context.getPackageManager();
        List<PackageInfo> packs = pm.getInstalledPackages(0);
        for (PackageInfo pi : packs)
        {
            try
            {
                PckgInfo newInfo = new PckgInfo();
                ApplicationInfo ai = pm.getApplicationInfo(pi.packageName, 0);

                newInfo.appType = getAppType(ai);
                newInfo.appName = pi.applicationInfo.loadLabel(pm).toString();
                newInfo.packageName = pi.packageName;
                newInfo.versionName = pi.versionName;
                newInfo.versionCode = pi.versionCode;
                _PckgInfoList.add(newInfo);
            }
            catch (NameNotFoundException e)
            {
                e.printStackTrace();
            }
        }
    }

    AppType getAppType(ApplicationInfo ai)
    {
        AppType resultType ;
        if (isUserApp(ai))
            resultType = AppType.USER;
        else
            resultType = AppType.SYSTEM;

        return resultType;
    }

    boolean isUserApp(ApplicationInfo ai)
    {
        int mask = ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;
        return (ai.flags & mask) == 0;
    }
}
AivarsDa
  • 296
  • 3
  • 6
0

You can use checkSignatures to determine if an app is a system app or not.

All system apps are signed with the same key.

https://developer.android.com/reference/android/content/pm/PackageManager#checkSignatures(java.lang.String,%20java.lang.String)

And signed with the system key is the "android" package.

    val checkPackage: String = "com.package.to.check"
    val systemPackageName = "android"
    if (packageManager.checkSignatures(systemPackageName, checkPackage) == PackageManager.SIGNATURE_MATCH) {
        Log.d("TUT", "System app")
    } else {
        Log.d("TUT", "Non-System app")
    }
Blundell
  • 69,653
  • 29
  • 191
  • 215