Android. Where did ApplicationProvider go?

Viewed 8647

I'm trying to run this line:

Context context = ApplicationProvider.getApplicationContext();

Sample code such as https://developer.android.com/training/data-storage/room/testing-db#java recommends it.

But I can't import ApplicationProvider (and I just LOVE how the documentation never imports anything, assuming your editor will prompt you correctly...). This line fails, with "error: package androidx.test.core.app does not exist":

import androidx.test.core.app.ApplicationProvider;

So where did ApplicationProvider go? I'm trying to use the current context to create a Room database:

        Context context = ApplicationProvider.getApplicationContext();
        db = Room.inMemoryDatabaseBuilder(context, TestDatabase.class).build();
        userDao = db.getUserDao();
2 Answers

Add a redundant line androidTestImplementation 'androidx.test:core:1.2.0' to build.gradle.

add androidTestImplementation 'androidx.test:core:$version' or testImplementation 'androidx.test:core:$version' dependency based on which type of test you do to your module-level gradle file.

Related