title: Android 'Couldn't create directory for SharedPreferences'
body: <p>This question has been asked before but there don't seem to be any answers of substance. Also, my code was working a little over a month ago.</p>
  <p>I'm trying to set shared preferences before an android instrumentation test. However, whenever I try to commit() or apply() my changes, I get the same error:</p>
  <blockquote>
  <p>com.androidapp.zitgenius E/SharedPreferencesImpl: Couldn't create directory for SharedPreferences file /data/user/0/com.androidapp.zitgenius.test/shared_prefs/login_ref.xml</p>
  </blockquote>
  <p>Here is the test code:</p>
  <pre><code>@RunWith(AndroidJUnit4.class)
  
  public class LoginActivityTest{
      public final String INCORRECT_USERNAME = &quot;this isn't a real username&quot;;
      public final String INCORRECT_PASSWORD = &quot;this isn't a real password&quot;;
      public final String CORRECT_USERNAME = &quot;test2@l.com&quot;;
      public final String CORRECT_PASSWORD = &quot;123&quot;;
      private static final String SHARE_PREF_NAME =&quot;login_ref&quot; ;
      private static final String KEY_USER_ID =&quot;id&quot; ;
  
      @Rule
      public ActivityScenarioRule&lt;LoginActivity&gt; activityScenarioRule
              = new ActivityScenarioRule&lt;&gt;(LoginActivity.class);
      private View decorView;
  
      public void setSharedPrefs(){
          SharedPreferences sharedPreferences = InstrumentationRegistry.getInstrumentation().getContext().getSharedPreferences(SHARE_PREF_NAME, Context.MODE_PRIVATE);
          SharedPreferences.Editor editor = sharedPreferences.edit() ;
          editor.putInt(KEY_USER_ID, -1) ;
          editor.apply();  //this will prevent automatic logins from disrupting tests
          Log.i(&quot;TESTING&quot;,&quot;Setting up shared prefs&quot;);
  
      }
      @Before
      public void setUp() {
          Intents.init();
          setSharedPrefs();
      }
      @Test
      public void inputLoginJustUsername() {
          SharedPreferences sharedPreferences = getInstrumentation().getContext().getSharedPreferences(SHARE_PREF_NAME, Context.MODE_PRIVATE);
  
          Log.i(&quot;TESTING&quot;,String.valueOf(sharedPreferences.getInt(KEY_USER_ID, -1)));
          //Type text and then hit Login Button
          onView(withId(R.id.editText_username_input)).perform(typeText(CORRECT_USERNAME), closeSoftKeyboard());
          onView(withId(R.id.button_login)).perform(click());
  
          // Confirm inputted text remains
          onView(withId(R.id.editText_username_input)).check(matches(withText(CORRECT_USERNAME)));
          //TODO: Confirm Toast error message
      }
      @Test
      public void inputLoginJustPassword() {
          //Type text and then hit Login Button
          onView(withId(R.id.editText_password_input)).perform(typeText(CORRECT_PASSWORD), closeSoftKeyboard());
          onView(withId(R.id.button_login)).perform(click());
  
          // Confirm inputted text remains
          onView(withId(R.id.editText_password_input)).check(matches(withText(CORRECT_PASSWORD)));
  
          //TODO: Confirm Toast error message
      }
      @Test
      public void inputLoginIncorrectCredentials() {
          //Type text and then hit Login Button
          onView(withId(R.id.editText_username_input)).perform(typeText(INCORRECT_USERNAME), closeSoftKeyboard());
          onView(withId(R.id.editText_password_input)).perform(typeText(INCORRECT_PASSWORD), closeSoftKeyboard());
          onView(withId(R.id.button_login)).perform(click());
  
          // Confirm inputted text remains
          onView(withId(R.id.editText_username_input)).check(matches(withText(INCORRECT_USERNAME)));
          onView(withId(R.id.editText_password_input)).check(matches(withText(INCORRECT_PASSWORD)));
  
          //TODO: Confirm Toast error message
      }
      @Test
      public void inputLoginCorrectCredentials() {
          int totalMillisToWait = 3000;
          int pollEveryMillis = 300;
          //Type text and then hit Login Button
          onView(withId(R.id.editText_username_input)).perform(typeText(CORRECT_USERNAME), closeSoftKeyboard());
          onView(withId(R.id.editText_password_input)).perform(typeText(CORRECT_PASSWORD), closeSoftKeyboard());
          onView(withId(R.id.button_login)).perform(click());
          //Wait for response
          for(int i = 0; i &lt; totalMillisToWait/pollEveryMillis; i++){
              try {
                  Thread.sleep((long)pollEveryMillis);
              } catch(InterruptedException e) {
                  System.out.println(&quot;got interrupted!&quot;);
              }
          }
          // Confirm transition to NavBarActivity
          intended(hasComponent(BottomNavigationActivity.class.getName()));
  
      }
      @Test
      public void switchToSignupActivity() {
          //Hit button to switch to signup
          onView(withId(R.id.button_switch_signup)).perform(click());
  
          // Confirm transition to Signup Activity
          intended(hasComponent(SignupActivity.class.getName()));
      }
  
      @After
      public void tearDown() throws Exception {
          Intents.release();
      }
  
      } 
  </code></pre>
  <p>I'm not sure where to even begin as I can't find any reference to this kind of error within the last 3 years, and those that exist beyond that point are either problems with Samsung devices or wildly inconsistent/nonsensical (I've tried them anyway and they have not helped)<br />
  I'm using Android Studio Arctic Fox | 2020.3.1 and testing on Pixel 4 and Pixel 4a virtual devices. Testing is done with espresso</p>