I use the following code:

try { Class[] classMethods = new Class[2]; classMethods[0] = Long.TYPE; classMethods[1] = IPackageDataObserver.class; Method localMethod = pm.getClass().getMethod("freeStorageAndNotify", classMethods); Object[] classParams = new Object[2]; classParams[0] = Long.valueOf(getEnvironmentSize() - 1L); classParams[1] = new IPackageDataObserver.Stub() { public void onRemoveCompleted(String paramAnonymousString, boolean paramAnonymousBoolean) throws RemoteException { } }; localMethod.invoke(pm, classParams); } catch (Exception localException) { while (true) localException.printStackTrace(); } 

IPackageDataObserver.aidl and
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" /> available

Ideas why not work? By the way, I checked similar applications. Everything works fine with this method.

    2 answers 2

    Solved a problem like this

     Method[] methods = pm.getClass().getDeclaredMethods(); for(Method m : methods) { if(m.getName().equals("freeStorageAndNotify")) { try { m.invoke(pm, Long.MAX_VALUE, null); } catch(Exception e) { e.printStackTrace(); } break; } } 

    As a parameter for the size of the cache to be cleared, it is necessary to pass Long.MAX_VALUE, that is, the maximum possible value.

      1. Look at this issue - the reason for this campaign.
      2. And the freeStorageAndNotify method is available, checked? It may not be for some APIs.
      • 1. It is not the case. 2. Yes, there is a method. Does not fall out. I will even say more, I got it when I decompiled it from a similar hallway, everything works fine on the same device. - AndroidDev
      • Maybe you need to perm on DELETE_CACHE_FILES ? - Barmaley
      • Added by. Nothing has changed - AndroidDev