TuanPM
TuanPM

Reputation: 703

Grant READ_EXTERNAL_STORAGE permission for android private app without dialog at runtime

I'm an Android application developer for an OEM and I'm building a MediaPlayer application that needs to access external storage to play audio located in it. (I add my application to AOSP build system then build whole Android image including my MediaPlayer application). Note that i use AOSP version 9 (Android Pie)

My implementation steps are as below

  1. Declare required permissions in AndroidManifest.xml

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

  2. Create Privileged Permission Whitelisting: privapp-permissions-test.xml

    <?xml version="1.0" encoding="utf-8"?>
    <permissions>
        <privapp-permissions package="com.example.mediaplayer">
            <permission name="android.permission.READ_EXTERNAL_STORAGE" />
            <permission name="android.permission.WRITE_EXTERNAL_STORAGE" />
        </privapp-permissions>
    </permissions>
    
  3. After built
    • My application located in /system/priv-app/
    • privapp-permissions-test.xml copied to /system/etc/permissions/

But when a booted device and start my application, it was crashed and logcat displayed like this

com.example.mediaplayer.service.MusicService: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=3452, uid=10021 requires android.permission.READ_EXTERNAL_STORAGE

Does anyone have any idea to help me ?? Any help is highly appreciated.

Upvotes: 1

Views: 1516

Answers (2)

Shawlaw
Shawlaw

Reputation: 568

According to relative doc, to auto-grant runtime-permission, you need to add/edit items of method grantDefaultSystemHandlerPermissions(int userId) in /frameworks/base/services/core/java/com/android/server/pm/permission/DefaultPermissionGrantPolicy.java

You can follow the source code's pattern to add other permission-granted logic on your demand.

Upvotes: 0

TuanPM
TuanPM

Reputation: 703

Finally, I solved my problem. Below is the solution

  1. Create the default permission file default-permission-sample.xml
    <?xml version="1.0" encoding="utf-8"?>
    <exceptions>
        <exception package="com.example.mediaplayer">
            <permission name="android.permission.READ_EXTERNAL_STORAGE"/>
            <permission name="android.permission.WRITE_EXTERNAL_STORAGE"/>
        </exception>
    </exceptions>
    
  2. Config makefile to copy default-permission-sample.xml into /system/etc/default-permissions/ folder

Done. When I launch my application the first time, it no longer ask for permission.

Upvotes: 2

Related Questions