SoFunction
Updated on 2025-04-08

How to get application size for Android


package ; 

import ; 
import ; 
import ; 
import ; 

import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 
import ; 

public class Main extends Activity { 
    private PackageStats ps; 

    public void getPackageStats(String packageName) { 
        try { 
//Get the Context of the setting package
            Context mmsCtx = createPackageContext("", 
                    Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY); 
//Use the setting classloader to load the class
            Class<?> maClass = ("", true, ()); 
//Create an object of it
            Object maObject = (); 

            /*
* Assign the private domain mPm. Because mPm is used in SizeObserver's invokeGetSize,
* However, it has not initialized because onCreate is not executed, so it needs to be initialized here.
             */ 
            Field f_mPm = ("mPm"); 
            f_mPm.setAccessible(true);             
            f_mPm.set(maObject, ()); 

            /*
* Assign the mHandler to a redefined Handler to receive the SizeObserver's
* The message of dispatch in the onGetStatsCompleted callback method, and gets the PackageStats object from it.
             * */ 
            Field f_mHandler = ("mHandler"); 
            f_mHandler.setAccessible(true); 
            f_mHandler.set(maObject, new Handler() { 
                  public void handleMessage(Message msg) { 
                      if( == 1) { 
//Get the PackageStats object here
                          ps = (PackageStats) ().getParcelable("ApplicationPackageStats");                          
                          ("", ""+);                           
                      } 
                  } 
            }); 

//Load the internal class SizeObserver
            Class<?> sizeObserverClass = ("$SizeObserver", true, ()); 
            Constructor sizeObserverConstructor = ()[0]; 
            (true); 
            /*
* Create a SizeObserver object with two parameters, the first one is an object of the external class.
* That is, the ManageApplications object, the second one is msgId, that is
* The id of the message is the same as the msgId received by the Handler.
             * */ 
            Object soObject = (maObject, 1); 
//Execute the invokeGetSize method
            ("invokeGetSize", , 
                    ).invoke(soObject, packageName, new CountDownLatch(1));          
        } catch (NameNotFoundException e) { 
            (); 
        } catch (ClassNotFoundException e) { 
            (); 
        } catch (IllegalAccessException e) { 
            (); 
        } catch (IllegalArgumentException e) { 
            (); 
        } catch (SecurityException e) { 
            (); 
        } catch (InvocationTargetException e) { 
            (); 
        } catch (NoSuchMethodException e) { 
            (); 
        } catch (InstantiationException e) { 
            (); 
        } catch (NoSuchFieldException e) { 
            (); 
        } 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        (savedInstanceState);   
        getPackageStats("");        
    }