How to programmatically clear application data
I am developing automated tests for an android application (using Robotium). In order to ensure the consistency and reliability of tests, I would like to start each test with clean state (of the application under test). In order to do so, I need to clear the app data. This can be done manually in Settings/Applications/Manage Applications/[My App]/Clear data
What is the recommended way to get this done programmatically?
Answers 11
- sagar3 Years ago
What is the recommended way to get this done programmatically?
The only possible option is to run ADB command adb shell pm clear package before the test. The biggest problem is that it's kind of headache combining tests execution and shell commands.
However, we (at Mediafe) came with some solution that can work for you on regular unrooted device. All you need to do is to add an annotation. All the rest is done by running simple bash script.
Just add @ClearData annotation before ANY of your tests and tada ????, ADB clear command will be executed before the test execution.
This is an example of such test:
@Test @ClearData public void someTest() { // your test }The idea is as follows
- Read all tests by using adb shell am instrument -e log true
- Build execution plan by parsing the output from (1)
- Run the execution plan line by line
Using the same idea these are all options you can easily support:
- Clear data
- Clear notification bar
- Parameterize
- Filter and run by tags
Use only annotations. Like this:
Bonus! ???? For each failed test:
- Collect Logcat + stacktrace
- Record video (mp4)
- Dump DB (sqlite)
- Dump default shared preferences (xml)
- Collect dumpsys files like: battery, netstats and other.
In general, it's easy to add more options, since the tests are executed one by one from bash script rather than from gradle task.
???? The full blog post: //medium.com/medisafe-tech-blog/running-android-ui-tests-53e85e5c8da8
???? The source code with examples: //github.com/medisafe/run-android-tests
Hope this answers 6 years question ;)
- Rakesh3 Years ago
if android version is above kitkat you may use this as well
if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) { ((ActivityManager) context.getSystemService(ACTIVITY_SERVICE)) .clearApplicationUserData(); return; } - sagar3 Years ago
This solution has really helped me :
By using below two methods we can clear data programatically
public void clearApplicationData() { File cacheDirectory = getCacheDir(); File applicationDirectory = new File(cacheDirectory.getParent()); if (applicationDirectory.exists()) { String[] fileNames = applicationDirectory.list(); for (String fileName : fileNames) { if (!fileName.equals("lib")) { deleteFile(new File(applicationDirectory, fileName)); } } } } public static boolean deleteFile(File file) { boolean deletedAll = true; if (file != null) { if (file.isDirectory()) { String[] children = file.list(); for (int i = 0; i < children.length; i++) { deletedAll = deleteFile(new File(file, children[i])) && deletedAll; } } else { deletedAll = file.delete(); } } return deletedAll; } - Sunil Patel3 Years ago
The Simplest way to do this is
private void deleteAppData() { try { // clearing app data String packageName = getApplicationContext().getPackageName(); Runtime runtime = Runtime.getRuntime(); runtime.exec("pm clear "+packageName); } catch (Exception e) { e.printStackTrace(); } }This will clear the data and remove your app from memory. It is equivalent to clear data option under Settings --> Application Manager --> Your App --> Clear data.
This will remove the data completely as well as force close the app
- sahil Kothiya3 Years ago
Using Context,We can clear app specific files like preference,database file. I have used below code for UI testing using Espresso.
@Rule public ActivityTestRule<HomeActivity> mActivityRule = new ActivityTestRule<>( HomeActivity.class); public static void clearAppInfo() { Activity mActivity = testRule.getActivity(); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity); prefs.edit().clear().commit(); mActivity.deleteDatabase("app_db_name.db"); }