checkSelfPermission: How to do it prior to API 22 - android

I am trying to port my app from API 22 to API 26. Several API calls are failing (e.g. WRITE_EXTERNAL_STORAGE, etc.). I believe it is due to the new runtime permission model wherein permissions are not granted EVEN if they are in the manifest.
So I tried to do the right thing, namely using checkSelfPermission() and requesting the permission if denied.
My problem is that checkSelfPermission is not available before API 23 and my minSdkVersion is lower than that so Android Studio flags it as an error.
I'm now stuck between rock & hard place: If I raise my minSdkVersion to 23, I'll break the app for many existing customers. If I lower my targetSdkVersion below API 23 to avoid the problem, Google Play won't accept it.
Help!

You should check permissions only for API >= 23.For API < 21 will use install-time requests

Call ContextCompat.checkSelfPermission() in the v4 support library (see this question). This is a "compatibility layer", so that single function will work on all versions of Android.

Related

Accessing a new MediaStore feature on an old device

I have an app that accesses audio files through the MediaStore API. I'm using
a minSdkVersion of 16 and a targetSdkVersion of 30. When I query the
MediaStore, my projection array includes MediaStore.Audio.Media.DURATION.
Android Studio attaches a warning message to this, "Field requires API level
29". If I ignore the warning, and run my app anyway on a legacy device with
API level 16 or 19, the app runs fine, and the durations it returns are
correct.
If the DURATION attribute was only added in API 29, how come it doesn't
generate an error on older devices? What is the downside to continuing to
ignore the warning?

How do you targetSdkVersion for third party libraries, and what happens if you don't?

From here and here Google says "Make sure that your third-party SDK dependencies support API 29".
However, Google doesn't say what happens if third-party libraries don't target API 29. For example, greenDAO targets API 25. It's not clear to me what happens if my app targets 29, but greenDAO targets 25 - is this resolved somehow?
I'm assuming that so long as my app targets API 29, it will continue to auto-update, because this is the current state of the world: Play store currently requires at least API 28, and my app auto-updates even with third party targets targeting less than API 28.
I found the following topics, but they weren't comprehensive.
Dependencies in the app to fulfil the API Target 26 in Aug/Nov 2018
https://www.reddit.com/r/androiddev/comments/8wxuci/questions_about_3rd_party_libraries_targeting_api/

Why I can still publish app that has Api level lower than 26 if google will only accept apps with Api level 26 or newer?

Recently I read a book about android which says that Starting with august 2019, Google play will only accept apps built using Api 26 or newer. But recently we published android app that has minimum Api level 23 and it is successfully published. Can anyone tell whats the scenario, or the documentation in the book is wrong?
They are not talking about minSdk , they are talking about targetSdk or compileSdk.
Notice that I used "or" because your targetSdk can not be higher than your compileSdk.
When you increment targetSdk number, you have to comply with the new api and rules enforced however using an old compile/target sdk will let you use the features in the same way as they worked in earlier versions (which is one of the root causes of security vulnerablities). That is why they placed the restriction so every app is forced to use 'new way of doing things' rather than old.
For example, Before Android 5.0, permissions were granted when app was installed but then it was changed so now you have to ask permissions on runtime. If you target Sdk api lower than Android 5.0, you don't have to do anything and it will work on newer devices as well. However, if you target api higher than Android 5, you must ask for permissions on runtime or the newer sdk code throws an exception.
What this means is that your target API has to be their minimum (in this case 28), but you can still set your own minimum API to whatever you want to allow backwards compatibility.
When you upload an APK, it needs to meet Google Play’s target API level requirements. Starting August 1, 2019, Google Play requires that new apps target at least Android 9.0 (API level 28), and that app updates target Android 9.0 from November 1, 2019. Until these dates, new apps and app updates must target at least Android 8.0 (API level 26).
https://developer.android.com/distribute/best-practices/develop/target-sdk
I have an app that targets 28, but the min API level I want is 18. It still allows me to upload the app because it targets 28.
It means that you have to set in the build.gradle file
targetSdkVersion 28 //or later
Official doc:
When you upload an APK, it needs to meet Google Play’s target API level requirements. Starting August 1, 2019, Google Play requires that new apps target at least Android 9.0 (API level 28), and that app updates target Android 9.0 from November 1, 2019. Until these dates, new apps and app updates must target at least Android 8.0 (API level 26).
Keep in mind that:
minSdkVersion <= targetSdkVersion <= compileSdkVersion
If you want to have more details about minSdk, targetSdk and compileSdk I suggest you reading this blog.

Android M "Runtime Permissions" not requested at runtime?

I was getting ready for runtime permissions on android M when I recently figured out that (at least on theGgalaxy S6 of my friend), still all permissions have to be confirmed at installation time (Google Play).
He has Android M, and sure you can revoke the permissions in the app settings now, but initially when you start the app after install, everything is granted.
I am kind of surprised, that's not the "Runtime Permissions" I was expecting.
Any hints on that? Did I miss something?
According to the doc:
This lesson describes how you implement permissions requests on apps that target API level 23 or higher, and are running on a device that's running Android 6.0 (API level 23) or higher. If the device or the app's targetSdkVersion is 22 or lower, the
system prompts the user to grant all dangerous permissions when they
install or update the app.
The runtime permissions will only take effect if you update the target SDK to 23

How can I ensure my app can not be installed on Marshmallow

In android gradle build file I have the following closure:
android {
compileSdkVersion 21
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.mycoolapp"
minSdkVersion 16
targetSdkVersion 21
versionCode 41
versionName "3.0"
}
}
I do not want my app to run on Marshmallow. My targetSdkVersion is 21 and Marmallow would be 23. So imagine I have a phone that is Marshmallow, if I go to the play store will my app appear in the listing?
My second question is how would I stop my app from appearing in google play store for marshmallow devices?
So imagine i have a phone that is Marshmallow, if i go to the play store will my app appear in the listing ?
Yes.
how would i stop my app from appearing in google play store for marshmallow devices?
You are welcome to try android:maxSdkVersion in the <uses-sdk> element of your manifest, as the current documentation suggests that the Play Store uses it as a filter, though this comes at a cost to users who get upgrades of their app to Android 6.0.
Answering not to original question, but to reasons of it.
I'm not sure you have to do it.
api 23 is asking for runtime permissions. so if i have a device running 23 and since android is backward compatible, what will happen for example if im using apache httpClient which is depreacated in marshmallow
You still can use HTTP client. From the docs:
To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:
android {
useLibrary 'org.apache.http.legacy'
}
I tried to install my old applications which use HTTP Client on Android 6.0. They still work, without the dependency in build.gradle and even without recompiling.
or if im not prepared to handle runtime permissions ? wont my app break in this case ?
App wouldn't break. If you don't compile your app under Marshmallow (targetSdkVersion 23 or higher) then it will work in "legacy" mode: permissions will be requested before installation. With one exception: users still can switch off permissions in settings; I don't think that many users do it.
Try your app in emulator or real device. I'm almost sure it will work under Marshmallow.
may be i think it is described in app runtime permission model will work if we target api to 23
Note: This lesson describes how you implement permissions requests on apps that target API level 23 or higher, and are running on a device that's running Android 6.0 (API level 23) or higher. If the device or the app's targetSdkVersion is 22 or lower, the system prompts the user to grant all dangerous permissions when they install or update the app.
link: https://developer.android.com/intl/es/training/permissions/requesting.html
may be if i have some misunderstanding then please notify me

Categories

Resources