geNia
geNia

Reputation: 1025

Android, find all places in code to request permissions

I am working on an application for pre-Marshmallow devices. The source code was not written by me and the project itself is quite big. I am currently making the app to request permissions when needed.

The question is: How to find all places in code where permission should be requested? I am using Android Studio.

EDIT
Some people suggest to change api to 23 and just run the app and see the places where it crashes. The problem is that the app does not crash in every place.

For example, running this code without a permission will crash the app:

TelephonyManager manager = (TelephonyManager) context
            .getSystemService(Context.TELEPHONY_SERVICE);

While this one just returns an empty array, instead of crashing.

final String[] SELF_PROJECTION = new String[]{
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,};
Cursor cursor = context.getContentResolver()
    .query(ContactsContract.Profile.CONTENT_URI, SELF_PROJECTION, null, null, null);

Also, Android Lint does not show these places. I'm not sure if it should be.

Upvotes: 18

Views: 2775

Answers (5)

Joe
Joe

Reputation: 81

Add

lintOptions {
    enable 'MissingPermission'
}

in your build.gradle. This will show warnings after you build your application.

Upvotes: 2

Swing
Swing

Reputation: 926

Request all permissions you want at the first startup of your app.

It's not a Best Practices, but it is an answer of this question.

Upvotes: -4

DDsix
DDsix

Reputation: 1984

You can do this trick: delete the dangerous permissions from the manifest. This way, you can test on pretty much any device, you'll be sure it will crash and you'll find the exact places where you need those permissions. This is how I did it anyway.

Upvotes: 0

rothloup
rothloup

Reputation: 1290

According to the developer page regarding security permissions :

In almost all cases, however, a permission failure will be printed to the system log.

So run the app and search the log for "permissions" or similar.

Use unit testing to ensure coverage of all the places in the code where permissions may be required.

Upvotes: 5

Shmuel
Shmuel

Reputation: 3916

Sure, compile targeting api 23, do not add in permissions code, run the app and see where it crashes.

Once you start pinpointing the locations then flip permissions on (via app settings) so you can get past that screen and then turn them back off so you can see if it crashes.

Logcat is pretty descriptive in letting you know that permissions are denied...

Upvotes: 1

Related Questions