-1
private static final long CACHE_APP = Long.MAX_VALUE;
private CachePackageDataObserver mClearCacheObserver;
btnCache.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        clearCache();
    }
});//End of btnCache Anonymous class

void clearCache() 
{
if (mClearCacheObserver == null) 
{
  mClearCacheObserver=new CachePackageDataObserver();
}

PackageManager mPM=getPackageManager();

@SuppressWarnings("rawtypes")
final Class[] classes= { Long.TYPE, IPackageDataObserver.class };

Long localLong=Long.valueOf(CACHE_APP);

try 
{
  Method localMethod=
      mPM.getClass().getMethod("freeStorageAndNotify", classes);

  /*
   * Start of inner try-catch block
   */
  try 
  {
    localMethod.invoke(mPM, localLong, mClearCacheObserver);
  }
  catch (IllegalArgumentException e) 
  {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  catch (IllegalAccessException e) 
  {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  catch (InvocationTargetException e)
  {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  /*
   * End of inner try-catch block
   */
}
catch (NoSuchMethodException e1)
{
  // TODO Auto-generated catch block
  e1.printStackTrace();
}
}//End of clearCache() method

private class CachePackageDataObserver extends IPackageDataObserver.Stub 
{
public void onRemoveCompleted(String packageName, boolean succeeded) 
{

}//End of onRemoveCompleted() method
}//End of CachePackageDataObserver instance inner class



 class CachePackageDataObserver extends IPackageDataObserver.Stub {
    public void onRemoveCompleted(String packageName, boolean succeeded) {

    }//End of onRemoveCompleted() method
}

I am using Below permission

<uses-permission android:name="android.permission.CLEAR_APP_CACHE"/>

The above code works fine for devices < Marsh Mellow but when i run this code on Marshmellow devices it causes exception. i read many post that say that CLEAR_APP_CACHE permission is system reserved in and not for third party. then how do Clean master and other apps clear internal caches of other apps in android. please help me to solve the exception so that i can clear the caches of third party apps progrmatically. Thanks in Advance.

Akshat Vajpayee
  • 274
  • 1
  • 3
  • 15
  • Have you tried checking and asking for this permission at runtime? – Maksym V. Jun 07 '18 at 07:24
  • Yup but nothing happened and there was no dialog appear for runtime permission. for reference i used below this link https://stackoverflow.com/questions/32742327/neither-user-10102-nor-current-process-has-android-permission-read-phone-state/38782876?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Akshat Vajpayee Jun 07 '18 at 07:26
  • possible related https://stackoverflow.com/questions/40261139/how-to-take-clear-app-cache-permission-in-android-marshmallow-at-runtime?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – E.Abdel Jun 07 '18 at 07:29
  • 1
    Possible duplicate of [How to take CLEAR\_APP\_CACHE permission in Android Marshmallow at runtime?](https://stackoverflow.com/questions/40261139/how-to-take-clear-app-cache-permission-in-android-marshmallow-at-runtime) – Rohit5k2 Jun 07 '18 at 07:31

1 Answers1

0

Starting from Android 6.0, permission level for CLEAR_APP_CACHE is signature|privileged. Normal Android apps cannot hold this permission. This permission is only granted if your app is signed with the firmware's signing key or you are installed on the privileged system partition.

Prior to Android 6.0, protection level for CLEAR_APP_CACHE was dangerous, so normal apps could request it in the manifest.

Sagar
  • 23,903
  • 4
  • 62
  • 62
  • " This permission is only granted if your app is signed with the firmware's signing key or you are installed on the privileged system partition." Please share the steps that how can i signed the app with firmware key or install the app on /system/priv-app. – Akshat Vajpayee Jun 07 '18 at 09:02