When debugging an application, often to clear the data generated by the APK initialization, the initialization will be in the/data/data/com.xxx.xx directory and/sdcard/a lot of new files, each time to the settings---application----software name---erase data. Busy operation trouble, want to use automation one click to clear.
0x1 Exhaustive traverse Delete folder
开始想到的是获取su权限后枚举/data/data/com.xx.xx目录下的文件夹,逐个删除文件夹
1 Public Static BooleanRemoveappdata (String packagename)2 {3File File =NewFile ("/data/data/" +packagename);4SYSTEM.OUT.PRINTLN ("Package Name:" +packagename);5 if(File.exists ())6 {7DataOutputStream OS =NULL;8DataInputStream is =NULL;9DataInputStream es =NULL;TenBufferedReader buf =NULL; OneProcess p =NULL; A Try - { -p = runtime.getruntime (). EXEC ("su");//Get root Permission theOS =NewDataOutputStream (P.getoutputstream ()); - //is = new DataInputStream (P.getinputstream ()); -BUF =NewBufferedReader (NewInputStreamReader (P.getinputstream ())); -ES =NewDataInputStream (P.geterrorstream ()); +Os.writebytes ("ls/data/data/" + packagename + "/\ n") ); - Os.flush (); +list< String > List =Newarraylist< String >( ); AList.add ("Cache" ); atList.add ("Databases" ); -List.add ("Files" ); -List.add ("Shared_prefs" ); - for(inti = 0; I < list.size (); i++ ) - { -Os.writebytes ("rm-f-r/data/data/" + PackageName + "/" + (String) list.get (i) + "\ n");//Delete files and folders inSystem.out.println ("Traverse" +(String) list.get (i)); - Os.flush (); to } + Os.flush (); -Os.writebytes ("exit\n" ); the Os.flush (); * os.close (); $ P.destroy ();Panax Notoginseng } - Catch(IOException e) the { + Try A { the os.close (); + is.close (); - } $ Catch(IOException E1) $ { - e1.printstacktrace (); - } the P.destroy (); - }Wuyi return true; the } - Else Wu { - return false; About } $}
The effect is OK, use the exhaustive traversal folder name to delete, although inefficient at least the desired function realized
0x2 using pm command to delete
无意在google看到某文章(研究删除数据到底会删除那些文件/夹)所使用的pm clear命令,尝试修改之后发现瞬间高大上!!!
1 Try2 {3p = runtime.getruntime (). EXEC ("su" );4OS =NewDataOutputStream (P.getoutputstream ());5is =NewDataInputStream (P.getinputstream ());6Os.writebytes ("PM Clear" + PackageName + "\ n" );7 Os.flush ();8Os.writebytes ("echo Test \ n");//echo test and get test to make sure the above code is executed9 Os.flush ();TenString result =is.readline (); OneOs.writebytes ("exit\n" ); A Os.flush (); - os.close (); - P.destroy (); the } - Catch(IOException e) - { - e.printstacktrace (); +}
Android clears other app data