Android Question Regarding API Levels and permissions

Sandman

Expert
Licensed User
Longtime User
(I'm just now in the process of trying to understand the API Levels and permissions myself, so I'm making this post a bit verbose with what I've learned, hopefully helping people coming after me.)


API Levels

I'm trying to wrap my head around the API Levels. I understand that with higher levels there will be more functions available (and probably some old removed also). System updates tend to raise the API Level. I've also understood that in the manifest one is supposed to define both the minimum API Level (meaning the oldest version of Android your app can support) and the target API (which, as far as I can tell, probably should be the latest available API Level). The target API Level also controls the look of the app, so if one uses an old target level, the app will look old from day one.

Picking a minimum API Level is a challenge in itself. Here all levels are listed:
https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels

And their initial release dates and support status:
https://en.wikipedia.org/wiki/Android_version_history

And here are the distributions of them in current use:
https://developer.android.com/about/dashboards/index.html

Looking at the distribution, one can see that as of today, if one support all the way back to 4.4 (including), one would target 89% of all devices. Which also syncs nicely with 4.4 being the oldest one actually supported by Google (look at the Wikipedia link above).

This would mean that we'd use this in the manifest:
B4X:
<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="25"/>
(because 19 = Android 4.4, and 25 = Android 7.1.1)


Permissions

Different API Levels handle permissions differently. Which means we need to do different things in the manifest, depending on what API Level we use for targetSdkVersion. For instance, if we target API Level 23 or higher, we need to use runtime permissions. If we use an old enough target we might get away with more because security was more lax way back.

So far so good, but...


Here's what I don't understand

How are we to actually know for sure what API Levels and permissions to add? An example: I saw this tutorial by Erel, on how to read NFC tags. In it he clearly states "This library requires Android version 2.3.3 or above (API level 10 or above).", which is great. Yet when I add that lib to a project and set my minSdkVersion to "9", I don't get a warning. And when I take a look at NFC using B4X Object Browser I can't find anything related to API Levels either.

This post is not a critique of B4A or Erel in any way. Nor is it specifically about the NFC library. This is about me not understanding how I'm supposed to think when making a project, from an API Level perspective.

I don't know if it's possible to do this, but I think it would be nice to be able to get a report on my B4A project and the API Levels my code hits. That could lead to reasoning like "if I redo this portion of code to remove function A and B and C, I could lower my minSdkVersion and gain another X% potential users". And if I enable a library that require a higher API Level than what I currently have in the project I could get a message about that.

Like I said, I'm just beginning to understand this world, so all help in clarifying and correcting me is appreciated. I'm suspect that many here understand things about this topic that I haven't grasped yet, and you find it quite easy. Please share. :)
 

rscheel

Well-Known Member
Licensed User
Longtime User
From La Api 23 upwards they need the RuntimePermissions or the android 6 permissions up, just to say that in the forum there is enough information on this topic.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
in the forum there is enough information on this topic

I wasn't able to find anything compiled on this topic, but it's great to hear there is enough information in the forum! Can you please link to a couple of posts that you can recommend on how one should think?
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Set you target less than 23, so you can keep using old style permission, at least if you need no new feature...

Yes, that's a nice rule of thumb to use. But it still doesn't answer how one should think, and where to find information on what API Level different functions require.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Tutorial about runtime persmissions: Runtime Permissions (Android 6.0+ Permissions)

The targetSdkVersion should be either 23 if you want to handle runtime permissions or lower if you don't (19 is a good option).

Unless otherwise written libraries support Android 2.2+ (API level 8). This has nothing to do with targetSdkVersion. Only minSdkVersion.
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
The targetSdkVersion should be either 23 if you want to handle runtime permissions or lower if you don't (19 is a good option).

Unless otherwise written libraries support Android 2.2+ (API level 8). This has nothing to do with targetSdkVersion. Only minSdkVersion.

Thank you, Erel. That was helpful to create a simple ruleset for how to think. This is as I understand it:

How to determine what minSdkVersion to use
(Devices running an API Level lower than this will not be able to install your app.)

If one uses a library that require over API Level 8, one should pick the level of that library...
It's not entirely clear where one finds this information, I've not been able to find a summary anywhere. It seems to be rare, and if it becomes more common information will be published.

...otherwise choose level 8 (or perhaps 5).
Level 8 was released 2010 so it's very old (which is a good thing as we want to include as many potential devices as possible). More info about Android releases here. When starting a new project in B4A 6.80, the minSdkVersion is specified as 5, which probably is just fine.


How to determine what targetSdkVersion to use
(Devices running API Levels above this will be able to install your app, but the look of the app is determined by this value so picking a low lovel will make your app look old from day one.)

If one wants to use the old permission system where user accepts at install, pick 19...
This also means you won't get the latest look for your app.

...but if you want to use Runtime Permissions and get the latest look, choose 24
The Runtime Permissions require at least 23, but using that one won't give you the latest look. The highest API Level at the moment is 25, but I don't know a reason to choose that instead of 24.

(NOTE: The difference in look seems to be that 24 is light, and the one before is dark. So nothing more dramatic than that. More information about this will surely be available in other places.)


Summary
Both below tries to target the largest number of possible devices, so minSdkVersion is 5.

Avoid Runtime Permissions (will not get latest theme)
B4X:
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>

Use Runtime Permissions (use latest theme)
B4X:
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="24"/>

I will gladly accept input to adjust this if I have misunderstood something.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
There is no relation between the targetSdkVersion and the looks (assuming that targetSdkVersion >= 14).

I will summarize it like this:
Set it to 23 if you want to support runtime permissions or 19 if not.
Don't set it to a higher value unless you actually tested it on a device running Android 7.
 
Last edited:
Upvote 0

Sandman

Expert
Licensed User
Longtime User
There is no relation between the targetSdkVersion and the looks (assuming that it targetSdkVersion >= 14).

I'm not sure I understand what you're saying. I have a clear memory of you saying that the looks are connected to targetSdkVersion (but I can't find that thread now).

In any case, I made a very small project and changed targetSdkVersion between 19 and 24, and made screenshots on my mobile (running 7.1.2, if that matters). Other than that my manifest is as-is for a new project.

<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="19"/>
Screenshot_20170515-150029.png


<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="24"/>
Screenshot_20170515-150108.png


(I've attached the project, but there's nothing in it really, just a couple of views and then one has to manually change the targetSdkVersion to see the differences.)
 

Attachments

  • targetSdkVersion.zip
    8.1 KB · Views: 287
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. I missed it. They changed the default theme to be Theme.Material.Light.DarkActionBar when targetSdkVersion is set to 24+.

However if you want to use a light theme with dark action bar then it is better to explicitly set the theme with this manifest code:
B4X:
SetApplicationAttribute(android:theme, "@style/LightTheme")
CreateResource(values-v20, theme.xml,
<resources>
  <style
  name="LightTheme" parent="@android:style/Theme.Material.Light.DarkActionBar">
  </style>
</resources>
)
CreateResource(values-v14, theme.xml,
<resources>
  <style
  name="LightTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
  </style>
</resources>
)
 
Upvote 0
Top