Find higher api 23 calls than minSdk 21 - android

Hello there are some answer on this topic, but none which worked for me so far.
My build.gradle looks like this
compileSdkVersion 23
buildToolsVersion "23.0.3"
minSdkVersion 21
targetSdkVersion 23
Somewhere in my Fragments I called the method getContext() and that crashed the app on a Lollipop 5.0 device. It works fine on Marshmallow 6.0.
The Fragment is imported from the non support library package.
import android.app.Fragment;
and since I have compileSDK on 23 I can call the method getContext() withing the Fragment to get the Context.
This will lead to a crash on Lollipop 5.0 and 5.1 since that method was added with API 23 and not API 21,22.
My Question is, how can I find such high level calls in Android Studio when the min SDK is below that?

how can I find such high level calls in Android Studio when the min
SDK is below that?
AFAIK Android studio normally warn the developer whenever they are using any methods which is not completely backward compatible till minSDKVersion defined by the application. So, At that point of time, you can check out current version of device and call relevant other method accordingly.
However, for some reason its not showing any lint warning while calling getContext() method. So, It seems we have to deal with it now.

Go to
-> Analyze -> Inspect Code -> run code inspection
Then in the result view there is
"Project Name"
- Android > Lint > Correctness
- calling new methods in older versions
Under (calling new methods in older versions) all unavailable calls should be listed

Now that we're in 2022, I found a more appropriate solution.
Run the gradlew lint command in Terminal and waiting for it to finish will generate a report file.
Wrote HTML report to file:///C:/Users/Administrator/path/to/project/module/build/reports/lint-results-$flavorName$buildType.html
open this file in browser, find InlinedApi and NewApi and you will find all of it.

Related

How does Android Studio know about backwards compatibility issues?

I am trying to understand how Android Studio determines if a code is available in a certain API. When using MediaStore.setRequireOriginal, Android Studio warns me that "this call requires API level 29". Does Android Studio check that this code is available in previous Android version sources?
photoContentUri = MediaStore.setRequireOriginal(photoContentUri)
I am trying to understand how it knows this.
The linter just knows all the APIs in all the versions. You don't need to download all the previous Android version sources (I was wondering how Android Studio's Linter knew about older versions when I only had API level 29 and 30 sources downloaded on my machine).
As you can see, lint now has a database of the full Android API such that it knows precisely which version each API call was introduced in.
Lint API Check page
The Short Answer:
It's set by the developer, And Android Studio just compares your minSdkVersion set in build.gradle file with the required api level.
The Longer Answer:
When you get this warning on a method, just CTRL+click on it to go to the source class, and there you will find it annotated #RequiresApi or/and #TargetApi, for example :
class MediaStore{
#RequiresApi(api = 29)
#TargetApi(29)
void setRequiredOriginal(...){}
}
Your build.gradle file:
defaultConfig {
minSdkVersion 23
...
}
Android Studio compares minSdkVersion to #RequiresApi or/and #TargetApi at the time you call the method MediaStore.setRequiredOriginal(...); and warn you if minSdkVersion is less that the recommended api.
Please note that there are differences between #RequiresApi and #TargetApi, sometimes you find them used along with each other but sometimes just one of them.
For more about difference between the two see: https://stackoverflow.com/a/50578783/10005752
There is something in build.gradle of application module like:
defaultConfig {
minSdkVersion 23
targetSdkVersion 30
}
So you can change the "minSdkVersion" to 29, and the warning message disappear ...
And if not:
With android OS version >= 29: your code works normally
With android OS version <29: might be an exception occurs

Android studio check for compatibility with API < 19

I have an application project with this settings:
android {
compileSdkVersion 21
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 10
targetSdkVersion 19
}
buildTypes {
release {
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
And I have a block with try-with resources which not use checking if Build.SDK is less than API 19.
I don't get any errors from IDE about it.
I ran a program on emulator with target SDK Android 4.1.2 and everything is fine, also checked on the device with Android 4.2.2. The program is invoke this code, checked with the debugger.
Is everything ok? I expect that there is might be compatibility errors from IDE but there's not.
If I try to create a new project in IDE with same minVersionSdk 10, I've got a error from IDE about the compatibility.
But in my working project I don't get it.
I don't know, is there any other settings for compatibility, not in build.gradle and AndroidManifest.xml? Why is it working on API < 19 ?
UPDATE:
you need to check Lint settings in Android Studio.
Editor - Inspections
Android Lint, Calling new methods on older versions
Why is it working on API < 19 ?
According to this: https://code.google.com/p/android/issues/detail?id=73483 It "mostly works" from API 15, and you tested on an API 16 emulator (4.1.2).
Issue raiser states:
Since it was unhidden in API level 19, try-with-resources is backwards compatible down to API level 15.
Google member replies (edited down):
AutoCloseable was in ics. and it's just an interface; it's javac that emits code to actually do the closing. iirc there are fewer classes that are AutoCloseable in ics than we actually unhid
so "backwards compatible" is a bit misleading. "mostly works" is closer to the truth.
Because it's not fully compatible, the warning is from API 19.
As to why you are not seeing the warning, I think that is down to your very old buildToolsVersion which dates back to December 2013. https://developer.android.com/tools/revisions/build-tools.html
You should always keep your Build Tools component updated by downloading the latest version using the Android SDK Manager
If you only use methods which were created before API 10 (You can check here) everything is ok.
If you are not sure, you can run lint (by right clicking on your root folder) and then you can check if lint warn you about deprecated method usage.
If you want more explanation don't hesitate to comment

Voice Actions Activity code not found

I'm working with adding Voice Actions to my app. Documentation tells me to use the method isVoiceInteraction() in an activity however every time I run a build (command line or IDE) I get errors saying the method can't be found. My question is, why?
I decompiled the source of an Activity and saw the method is there when I target API 21.
Here's my default build config:
compileSdkVersion 21
buildToolsVersion '22.0.1'
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
// version name and code set here
}
The method can be found if I change the compile, min, and target SDK version to android-MNC however that isn't what I need.
Why is my build not recognizing the any of the voice methods defined in an Activity (as of Android 5.0+)?
My confusion is that I'm working with a custom voice action. It was approved however I was told (from the Google Voice Actions Team) that I need to have my new APK submitted to the play store by August 7th, 2015. That seems odd because I don't think I can submit an APK targeting the preview SDK.
Voice Interactions is one of the features added in Android M - you'll need to ensure you follow the Preview SDK instructions and compile and target "android-MNC"

How do i get Android Studio's Lint to alert me about calls that don't meet minSDK?

I have set the min sdk version in build.gradle as minSdkVersion 8 (I set it low so it was easy to find an API that wasn't added yet). but when I run Analyze->Inspect Code... it doesn't show the call to the above-8 API in the errors. How do I get this to work?
I needed to sync the gradle changes. Then Lint sees the issues.

Build target with API level below 14 and v7 appcompat library

Having issues with appcompat-v7 and compileSdkVersion, the app build target is API 10 so I set compileSdkVersion 10 to compile the code safely, as expected it works well with support-v4: the app compiles and runs on API 10 devices.
Then I want to add appcompat-v7 to dependencies (or replace v4 with it) and perform clean re-build of the app without any changes at the code or resources, build fails at the R generation stage unless the compileSdkVersion is set to a higher value.
I understand it as the v7 library is using some values unavailable at API 10. It raises the question of how someone can continue to write safe code and use v7 without need to manually check API level of each variable and method. Is there a way to keep using v7 (that is claimed to be "designed to be used with Android 2.1 (API level 7)") and compileSdkVersion 10 ?
Apparently at the newest Intellij version Lint produces errors if the methods form the API above minSdkVersion is used (can be enabled/disabled at Preferences-Inspections, expand Android Lint at the list, look for Calling new methods on older versions or use "NewApi" annotation to suppress the error if needed).
That'll have to do until some kind of dynamic resources compilation is introduced. I'm going to leave the question here for a future reference.

Categories

Resources