title: Clearing the data of another application
body: <p>I'm writing an application that should be able clear the private data of any other application. If you're wondering about the use case, its along the lines of an MDM/MAM client. I'd like to selectively wipe application data (vs. a full device wipe).</p>
  
  <p>I came across the following API call in the Android source code. </p>
  
  <pre><code>ActivityManager.clearApplicationUserData(String packageName,IPackageDataObserverobserver)
  </code></pre>
  
  <p>The odd part is, that this is not really available to you as part of the SDK . (So eclipse will give you hell for trying to use it). However, it <strong>is</strong> present (see <a href="http://www.androidjavadoc.com/1.0_r1_src/index.html?android/app/ActivityManager.html" rel="nofollow">here</a>),you can invoke it via reflection. I'm still however, unable to get hold of the <a href="http://www.androidjavadoc.com/1.0_r1_src/android/content/pm/IPackageDataObserver.html" rel="nofollow">IPackageDataObserver</a> interface. </p>
  
  <p>Is there a better way of doing this? I know it CAN be done since I've seen products like MaaS360 do a selective wipe of applications' data.</p>
  
  <p>Any suggestions?</p>
  
  <hr>
  
  <h2>UPDATE</h2>
  
  <p>Based on what @lechlukasz has outlined below... the following code can execute...but you do finally land up with a SecurityException, since the package manager revokes the CLEAR_APP_USER_DATA permission when the app is installed. </p>
  
  <pre><code>Class&lt;?&gt; iPackageDataObserverClass= Class.forName("android.content.pm.IPackageDataObserver");
  
  Class&lt;ActivityManager&gt; activityManagerClass=ActivityManager.class;
  ActivityManager activityManager=(ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
  
  Method clearDataMethod=activityManagerClass.getMethods()[0];
  
  Object iPackageDataObserverObject = Proxy.newProxyInstance(
      MyApp.class.getClassLoader(), new Class[]{iPackageDataObserverClass}, 
                          new InvocationHandler() {
  
              public Object invoke(Object proxy, Method method, Object[] args) 
                      throws Throwable {
                  Log.i("Proxy", method.getName() + ": " + Arrays.toString(args));
                  return null;
              }
          });
  
  
  clearDataMethod.invoke(activityManager, "com.example.test",iPackageDataObserverObject);
  </code></pre>
  
  <p>So this works, insofar as the method can be called. No luck on actually being able to clear the data itself. :-(</p>