Issue
I have an application with more modules. In module A I have a ContentProvider
. In module B I am accessing to that ContentProvider
. The application works fine I can access the data. My issue is when I am trying to write an android instrumented test for a certain activity. In that activity in the onCreate
method I am calling an AsyncTask
that needs to access the database via the ContentProvider
and populate the activity with that data. In this moment I am getting an error:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.mycompany.dashboardlight.test, PID: 19196
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:318)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:762)
Caused by: java.lang.SecurityException: Permission Denial: opening provider com.mycompany.data.provider.HibpAccountProvider from ProcessRecord{7e4d57b 19196:com.mycompany.dashboardlight.test/u0a265} (pid=19196, uid=10265) that is not exported from uid 10264
at android.os.Parcel.readException(Parcel.java:1693)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4912)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:6043)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2474)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1521)
at android.content.ContentResolver.query(ContentResolver.java:520)
at android.content.ContentResolver.query(ContentResolver.java:478)
I have tried for both modules to have same shareUserId, seppration of permissions to read and write but nothing works.
This is in the manifest where the content provider is:
<provider
android:name="com.mycompany.data.provider.MyProvider"
android:authorities="${applicationId}.data.MyProvider"
android:enabled="true"
android:exported="false" />
This is the manifest in the module that is accessing the data:
<uses-permission android:name="${applicationId}.data.MyProvider" />
Creating other tests for activities that aren't accessing ContentProvider
are working fine.
And this is the test:
@RunWith(AndroidJUnit4.class)
public class MyActivityTest {
@Rule
public final ActivityTestRule<MyActivity> breachMain =
new ActivityTestRule<>(MyActivity.class);
@Test
public void isLaunchScreenDetected() {
onView(withText("No Data Breach Found"))
.check(ViewAssertions.matches(isDisplayed()));
}
}
Solution
Finally a solution. The issue was in how it was defined the authority. The applicationId is different for when you are installing the application and when you are trying to test the application. When testing the application you may notice that on your package name is appended .test, and in my case since I used hardcoded value com.mypackage lower in the code this caused the permission denial. So I ended with different authorities, because the applicationId is different when installing the application and running the test.
My proposal if you are defining the AUTHORITY also in your java classes, avoid the ${applicationId} not to have a mix like this.
Answered By - f.trajkovski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.