Search code examples
androidaidl

Retrieving Java package name of AIDL consumer


I am exposing some API through AIDL mechanism. Clients need to bind to the AIDL and call methods synchronously. Is there a way I can retrieve the client's Java package name?

For example, if I expose a method boolean isFooAvailable() as AIDL API, from within the implementation of isFooAvalable, can I determine the Java package name of the app that binds to the AIDL service?


Solution

  • Yes, you can find out the package name from within the implementation as follows :

    IAidl.Stub mBinder = new IAidl.Stub() {
    
        @Override
        public boolean isFooAvailable() throws RemoteException {
    
           String pkgName = "";
            int pid = Binder.getCallingPid();
    
            ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
            List<ActivityManager.RunningAppProcessInfo> processes = am.getRunningAppProcesses();
    
            for (ActivityManager.RunningAppProcessInfo proc : processes) {
    
                if (proc.pid == pid) {
    
                    pkgName = proc.processName;
                }
            }
           // package name of calling application package
           Log.e("Package Name", pkgName);
           return false;
        }
    }
    

    Finding package name through PID is the best approach compared to UID.