Alexis Le Compte
Alexis Le Compte

Reputation: 347

Programmatically retrieve permissions from manifest.xml in android

I have to programmatically retrieve permissions from the manifest.xml of an android application and I don't know how to do it.

I read the post here but I am not entirely satisfied by the answers. I guess there should be a class in the android API which would allow to retrieve information from the manifest.

Thank you.

Upvotes: 28

Views: 20759

Answers (6)

AllanRibas
AllanRibas

Reputation: 1184

Try this

fun Context.getDeclaredPermissions(): Array<String> {
    return packageManager.getPackageInfo(packageName, PackageManager.GET_PERMISSIONS).requestedPermissions
}

fun customLog(myTag: String, message: String) {
    val caller = getCallerInfo(Throwable().stackTrace)
    println("customLog $myTag $message $caller")
}

Then in your activity

val allPermissions = getDeclaredPermissions()
customLog(myTag, "allPermissions ${allPermissions.toList()}")   

Upvotes: 0

Here's a useful utility method that does just that (in both Java & Kotlin).

Java

public static String[] retrievePermissions(Context context) {
    final var pkgName = context.getPackageName();
    try {
        return context
                .getPackageManager()
                .getPackageInfo(pkgName, PackageManager.GET_PERMISSIONS)
                .requestedPermissions;
    } catch (PackageManager.NameNotFoundException e) {
         return new String[0];
         // Better to throw a custom exception since this should never happen unless the API has changed somehow.
    }
}

Kotlin

fun retrievePermissions(context: Context): Array<String> {
    val pkgName = context.getPackageName()
    try {
        return context
                .packageManager
                .getPackageInfo(pkgName, PackageManager.GET_PERMISSIONS)
                .requestedPermissions
    } catch (e: PackageManager.NameNotFoundException) {
        return emptyArray<String>()
        // Better to throw a custom exception since this should never happen unless the API has changed somehow.
    }
}

You can get a working class from this gist.

Upvotes: 21

OhhhThatVarun
OhhhThatVarun

Reputation: 4330

If anyone is looking for a short Kotlin Version

fun Manifest.getDeclaredPermissions(context: Context): Array<String> {
    return context.packageManager.getPackageInfo(context.packageName, PackageManager.GET_PERMISSIONS).requestedPermissions
}

Upvotes: -2

Phil
Phil

Reputation: 36299

You can get an application's requested permissions (they may not be granted) using PackageManager:

PackageInfo info = getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
String[] permissions = info.requestedPermissions;//This array contains the requested permissions.

I have used this in a utility method to check if the expected permission is declared:

//for example, permission can be "android.permission.WRITE_EXTERNAL_STORAGE"
public boolean hasPermission(String permission) 
{
    try {
        PackageInfo info = getPackageManager().getPackageInfo(context.getPackageName(), PackageManager.GET_PERMISSIONS);
        if (info.requestedPermissions != null) {
            for (String p : info.requestedPermissions) {
                if (p.equals(permission)) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

Upvotes: 31

user9089961
user9089961

Reputation:

I have a simple C# code, "using System.Xml"

private void ShowPermissions()
   {
    XmlDocument doc = new XmlDocument();
    doc.Load("c:\\manifest.xml");

    XmlNodeList nodeList = doc.GetElementsByTagName("uses-permission");


    foreach(XmlNode node in nodeList)
    {
        XmlAttributeCollection Attr = node.Attributes;
        string Permission=Attr["android:permission"].Value;

        MessageBox.Show(Permission);
    }
}

Upvotes: -6

Yousha Aleayoub
Yousha Aleayoub

Reputation: 5715

Use this:

public static String getListOfPermissions(final Context context)
{
    String _permissions = "";

    try
    {
        final AssetManager _am = context.createPackageContext(context.getPackageName(), 0).getAssets();
        final XmlResourceParser _xmlParser = _am.openXmlResourceParser(0, "AndroidManifest.xml");
        int _eventType = _xmlParser.getEventType();
        while (_eventType != XmlPullParser.END_DOCUMENT)
        {
            if ((_eventType == XmlPullParser.START_TAG) && "uses-permission".equals(_xmlParser.getName()))
            {
                for (byte i = 0; i < _xmlParser.getAttributeCount(); i ++)
                {
                    if (_xmlParser.getAttributeName(i).equals("name"))
                    {
                        _permissions += _xmlParser.getAttributeValue(i) + "\n";
                    }
                }
            }
            _eventType = _xmlParser.nextToken();
        }
        _xmlParser.close(); // Pervents memory leak.
    }
    catch (final XmlPullParserException exception)
    {
        exception.printStackTrace();
    }
    catch (final PackageManager.NameNotFoundException exception)
    {
        exception.printStackTrace();
    }
    catch (final IOException exception)
    {
        exception.printStackTrace();
    }

    return _permissions;
}
// Test: Log.wtf("test", getListOfPermissions(getApplicationContext()));

Upvotes: 3

Related Questions