Order of installing the application is changing th

2019-04-09 09:31发布

问题:

I have three applications: University, Student, Staff.

University is the Main application. Student and Staff are provider application.

University is having the permission to access the info from this provider applications.

I installed the apk in following order student, staff and University. this is working fine

But if i install in the order University, student and staff.

In this case University is crashed and the error is "access permission is denied".

Why this is happening even if we have proper permission? Why the provider installed later is not accessible by main application?

    01-29 16:49:48.257: E/AndroidRuntime(2622): Caused by: java.lang.SecurityException: 
Permission Denial: opening provider com.content.StudentProvider
from ProcessRecord{b4849ad0 2622:com.example.University/u0a44} (pid=2622, uid=10044) 
requires com.content.READ or com.content.WRITE
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.os.Parcel.readException(Parcel.java:1425)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.os.Parcel.readException(Parcel.java:1379)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2354)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.app.ActivityThread.acquireProvider(ActivityThread.java:4219)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:1703)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1099)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.content.ContentResolver.query(ContentResolver.java:354)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.content.CursorLoader.loadInBackground(CursorLoader.java:65)
01-29 16:49:48.257: E/AndroidRuntime(2622):     at android.content.CursorLoader.loadInBackground(CursorLoader.java:43)

Provider - Student - Manifest

 <permission android:description="@string/readPermissionDescription" 
    android:name="com.content.student.READ" android:protectionLevel="normal"></permission>

    <permission android:description="@string/writePermissionDescription" 
    android:name="com.content.student.WRITE" android:protectionLevel="dangerous"></permission>

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <provider
            android:name=".StudentProvider"
            android:authorities="com.content.student"
            android:readPermission="com.content.student.READ"
            android:writePermission="com.content.student.WRITE" >
        </provider>
    </application>

University - Manifest

 <uses-permission android:name="com.content.student.READ" />
    <uses-permission android:name="com.content.student.WRITE" />

回答1:

Why this is happening even if we have proper permission?

Because they were installed in the wrong order.

Why the provider installed later is not accessible by main application?

Because the permission did not exist when the main application was installed.

The solution is to define the same permissions in all three APK files.

Also, unless you plan on third party apps being able to use this content provider, please use signature-level permissions, not normal or dangerous.