1

I'm working in API 22, but I want to compile my project in Android M 6.0, I have this code:

Declared at the top:

private static final String[] REQUIRED_PERMISSIONS = new String[]{"READ_EXTERNAL_STORAGE"};
private static final int REQUEST_PERMISSIONS = (Integer) null;

And on my onCreate():

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    LinkedList<String> missingPermissions = new LinkedList<>();
    for(String p : REQUIRED_PERMISSIONS){
        if(checkCallingOrSelfPermission(p) != PackageManager.PERMISSION_GRANTED){
            missingPermissions.add(p);
        }
    }
    if(!missingPermissions.isEmpty()){
        String[] mpArray = new String[missingPermissions.size()];
        missingPermissions.toArray(mpArray);
        requestPermissions(mpArray, REQUEST_PERMISSIONS);
    }
}

I was inspired here for checking my problem and in Eclipse is giving me an error on Build.VERSION_CODES.M(M not found), and then, the callback method requestPermissions(mpArray, REQUEST_PERMISSIONS) isn't found too, any suggestion?

If I'm working on API 22, and I'm compiling with Android 6.0 M. How I can solve the issue for the dangerous permissions like READ_EXTERNAL_STORAGE correctly on API 22?

Community
  • 1
  • 1
Jose
  • 9
  • 5

1 Answers1

3

As per the documentation, Build.VERSION_CODES.M and requestPermissions() were added in API level 23.

Since you are compiling with API level 22, those simply do not exist.

To access APIs introduced in API level 23, you need to compile with API 23. You cannot access these APIs if you continue to compile with API 22.

Note that simply compiling with API 23 will not affect the way your application behaves on any devices, it simply opens up the newer APIs for your use on devices running at least API 23.

if I'm working on API 22, and I'm compiling with Android 6.0 M, how i can do for solve the issue for the dangerous permissions like READ_EXTERNAL_STORAGE correctly on API 22?

Devices running API 22 will continue to use the old install-time model for permissions. Nothing has changed for devices running API 22 and below. Only devices running API 23 use the new runtime permissions model.

Bryan Herbst
  • 66,602
  • 10
  • 133
  • 120
  • So i need to change all my project to api 23? – Jose Jun 28 '16 at 14:11
  • You should generally always compile against the latest version of Android, as there is no downside to doing so. You should also set your `targetSdkVersion` to the latest version of Android, but only once you have tested on the latest version, since targeting a newer version will enforce certain platform changes such as run time permissions. Any project you want to properly support runtime permissions needs to target at least API 23. – Bryan Herbst Jun 28 '16 at 14:13
  • Okay...there´s no way to add api 23 lib into my project for work them methods? – Jose Jun 28 '16 at 14:27
  • That is exactly what setting your compile SDK version does- it specifies which version of the Android SDK to use. However, if you don't target API 23, then going through the effort of supporting runtime permissions doesn't mean anything. – Bryan Herbst Jun 28 '16 at 15:07
  • If I were developing a library, I would target and compile against the latest version of Android, because there is no reason not to. – Bryan Herbst Jun 28 '16 at 15:50
  • And if you are developing an app with api 22 but compiling with api 23 you should change your target to api 23 too so? – Jose Jun 28 '16 at 16:01
  • Yes. Any user running a device that has 23+ can turn off permissions to your app, and you won't know about it. You should target 23 so you can properly handle runtime permissions. – Bryan Herbst Jun 28 '16 at 16:03