I compile against Android 4.2 (API 17), in my Manifest I have:
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/>
In code I use:
String first = sdf.format(new Date(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).firstInstallTime));
Field firstInstallTime was introduced in API 9.
Lint does not warn me, i.e. that this field is not valid in API 8. What am I missing, how should one detect this?
If I compile against Android 2.2 (API 8), I find the error and a bunch of extra errors due to new features used (> API 8) and the project won't compile.
(I'm aware of handling such things in runtime with for example Build.VERSION.SDK_INT)
What's the best way of working?
Why is lint not working?
Thanks!
This answer may be late but You should check your lint preferences
Right click on project -> Properties -> Android Lint Preferences
then search for min in the searchbox and select "UsesMinSDKAttributes"
Finally select "Include all" button. Hopefully the check had just been surpressed (Something I've had to do just to fix a silly lint error)
Good luck.
If it is intoduced in api level9 then try this.
< uses-sdk android:minSdkVersion="9" android:targetSdkVersion="10"/>
Probably it will works when the https://code.google.com/p/android/issues/detail?id=56427 will be solved
Related
I use the following code:
FingerprintManager fingerprintManager = (FingerprintManager) getBaseContext().getSystemService(Context.FINGERPRINT_SERVICE);
Context.FINGERPRINT_SERVICE gets underlined in red like an error and it suggests:
Must be one of: Context.POWER_SERVICE, Context.WINDOW_SERVICE, Context.LAYOUT_INFLATER_SERVICE, Context.ACCOUNT_SERVICE, Context.ACTIVITY_SERVICE, Context.ALARM_SERVICE, Context.NOTIFICATION_SERVICE, Context.ACCESSIBILITY_SERVICE, android.content.Context.CAPTIONING_SERVICE, Context.KEYGUARD_SERVICE, Context.LOCATION_SERVICE, Context.SEARCH_SERVICE, Context.SENSOR_SERVICE, android.content.Context.STORAGE_SERVICE, Context.WALLPAPER_SERVICE, Context.VIBRATOR_SERVICE, Context.CONNECTIVITY_SERVICE, android.content.Context.NETWORK_STATS_SERVICE, Context.WIFI_SERVICE, android.content.Context.WIFI_P2P_SERVICE, android.content.Context.NSD_SERVICE, Context.AUDIO_SERVICE, android.content.Context.FINGERPRINT_SERVICE, android.content.Context.MEDIA_ROUTER_SERVICE, Context.TELEPHONY_SERVICE, android.content.Context.TELEPHONY_SUBSCRIPTION_SERVICE, android.content.Context.CARRIER_CONFIG_SERVICE, android.content.Context.TELECOM_SERVICE, Context.CLIPBOARD_SERVICE, Context.INPUT_METHOD_SERVICE, android.content.Context.TEXT_SERVICES_MANAGER_SERVICE, android.content.Context.APPWIDGET_SERVICE, Context.DROPBOX_SERVICE, Context.DEVICE_POLICY_SERVICE, Context.UI_MODE_SERVICE, android.content.Context.DOWNLOAD_SERVICE, android.content.Context.NFC_SERVICE, android.content.Context.BLUETOOTH_SERVICE, android.content.Context.USB_SERVICE, android.content.Context.LAUNCHER_APPS_SERVICE, android.content.Context.INPUT_SERVICE, android.content.Context.DISPLAY_SERVICE, android.content.Context.USER_SERVICE, android.content.Context.RESTRICTIONS_SERVICE, android.content.Context.APP_OPS_SERVICE, android.content.Context.CAMERA_SERVICE, android.content.Context.PRINT_SERVICE, android.content.Context.CONSUMER_IR_SERVICE, android.content.Context.TV_INPUT_SERVICE, android.content.Context.USAGE_STATS_SERVICE, android.content.Context.MEDIA_SESSION_SERVICE, android.content.Context.BATTERY_SERVICE, android.content.Context.JOB_SCHEDULER_SERVICE, android.content.Context.MEDIA_PROJECTION_SERVICE, android.content.Context.MIDI_SERVICE, android.content.Context.HARDWARE_PROPERTIES_SERVICE, android.content.Context.SHORTCUT_SERVICE, android.content.Context.SYSTEM_HEALTH_SERVICE
my compileSdkVersion is 24.
Although I have this error, my app runs and works fine.
When I start a new project, the same code has no warnings at all.
I know that it is not a big problem, but it annoys me.
Suggestions plz?
I solved it..
I had a couple of older projects as libraries in my project and their target sdks and compiled sdks in their manifests and gradle files were lower than 24. Although I was allowed to build this thing before, I had a few problems that were fixed once I updated everything.
I reached to this solution after I fixed a problem similar to All com.android.support libraries must use the exact same version specification
I am working with a large project, which has a minimum API level:16. however, I came across API usages that are above API level 16.
Is there any tool in Android studio or elsewhere, other than testing with a device, to check if the code doesn't violate the minimum required API level or better point it out like an error etc.?
Thank you.
The IDE will use the minimum android SDK, thus you will not get compile errors. If you there are classes in SDK 14 which are moved in sdk 16, yet you are using the imports from SDK 14, it will give a standard compile error.
So no, not that I am aware of.
You can use something like this:
public static boolean supports(final int version) {
return Build.VERSION.SDK_INT >= version;
}
Like this,
if (supports(Build.VERSION_CODES.HONEYCOMB)) {
// do something HONEYCOMB+ compatible here
}
More codes here,
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html
Since update AS 1.1 Preview 2, I'm getting red lines under all my Log messages
Log.d(TAG, "message");
With message: "The logging tag can be at most 23 characters..".
I didn't update anything fundamentally, except Android Studio itself. Is this a bug?
You can disable it if you so choose.
In Android Studio, Analyze->Inspect Code.
Under Inspection Profile, click on the button with the 3 horizontal dots.
The following window should open. Search for "log" and uncheck "Too Long Log Tags".
Update: Android Studio 2.2, it is located under Android Lint: Correctness
No, it's not a bug.
From Android Studio's Recent Changes on 1.1 Preview 2,
Checks that the tag passed to the logging calls, if its value can be resolved, is at most 23 characters long (as required by the Logging API.)
As shortly explained on the recent changes, it's due to how Log API doesn't allow tag that exceeds 23 characters.
SLF4J Android has an explanation to this:
[...] the length of such tags is currently limited to 23 characters (23 = 32 - 8 for namespace prefix - 1 for C terminator)
which matches the Android's source code.
Currently, the only function that explicitly mentions this exception is Log.isLoggable(),
...
Throws
IllegalArgumentException is thrown if the tag.length() > 23.
However, based on the comments, apparently the logger does throw the exception on release mode (it's ignored in debug mode).
You can disable the lint checking by following Terence's answer, but you've been warned.
Complementing the answer by #Terence
You can also turn off the specific check via gradle with this in your build.gradle file:
lintOptions {
disable 'LongLogTag'
}
Or by adding a lint.xml file to your project with xml:
<?xml version="1.0" encoding="UTF-8"?>
<lint>
<issue id="LongLogTag" severity="ignore" />
</lint>
You can never ignore this lint check, it definitely could bring unexpected results on your release version since it throws exceptions and stops executing (it would not crash your app).
I have had a terrible lesson learned recently: it's OK on debug mode, but behave differently on release version.
This is recent change and In this build, its a new lint check. Which says,
Checks that the tag passed to the logging calls, if its value can be resolved, is at most 23 characters long (as required by the Logging API.)
For more info, read 3rd point in below link.
https://sites.google.com/a/android.com/tools/recent/androidstudio11preview2
If you dont want to get this, minimize the number of characters in your TAG and make sure that they wont cross the length more than 23.
To explain why this happens:
According to AOSP source code you can log with any tag you want. The problem is in Log.isLoggable.
Log.isLoggable checks the system property log.tag.<YOUR_TAG> if the priority you want to log is enabled. Here's documentation of this mechanism:
public static boolean isLoggable (String tag, int level)
Checks to see whether or not a log for the specified tag is loggable at the specified level. The default level of any tag is set to INFO. This means that any level above and including INFO will be logged. Before you make any calls to a logging method you should check to see if your tag should be logged. You can change the default level by setting a system property: 'setprop log.tag. ' Where level is either VERBOSE, DEBUG, INFO, WARN, ERROR, ASSERT, or SUPPRESS. SUPPRESS will turn off all logging for your tag. You can also create a local.prop file that with the following in it: 'log.tag.=' and place that in /data/local.prop.
Source: https://developer.android.com/reference/android/util/Log#isLoggable(java.lang.String,%20int)
Below API 26 (Oreo) the limit of system property keys was 31 characters. And "log.tag.".length() + 23 equals 31. If you call Log.isLoggable below Android Oreo with a tag longer than 23 characters it will throw, as described in the source code. Since Android O this limit no longer applies.
The Lint rule exists just to shield you from all these (typically) unnecessary details.
The documentation for Log.isLoggable also states the IllegalArgumentException will not be thrown since API 24, which according to my findings, is wrong. Follow: https://issuetracker.google.com/issues/124593220
Solution. Year 2020 ver.
build.gradle (app)
android {
lintOptions {
disable 'LongLogTag'
} // put this.
}
This error was thrown for me for a node_module library, complaining about a separate node_mode library.
I added this lint options property within that node_module library's build gradle file.
android {
lintOptions {
abortOnError false
}
}
The library was aws-amplify push notification.
Error:
Execution failed for task ':#aws-amplify/pushnotification:lint'
File updated: node_modules/#aws-amplify/pushnotification/android/build.gradle
When building android apps for different behavior on different targets we can do:
if (Build.VERSION.SDK_INT < 10) {
Toast.makeText(this.context, "Not Supported", Toast.LENGTH_LONG ).show();
} else {
this.doComplicatedOperation();
}
But in the method doComplicatedOperation() it would be logical to use higher api classes than the current build target (eg. api 5), but lint keeps complaining that ClassIntroducedInApiLevel11 can not be resolved as a type
How could I change the code of doComplicatedOperation() that my project compiles?
#TargetApi(11)
private void doComplicatedOperation() {
ClassIntroducedInApiLevel11 = new ClassIntroducedInApiLevel11();
}
The purpose of minSdkVersion is to exclude platforms where you do not provide backward compatibility support.
In order to provide the proper libraries for your build you need to set a higher targetSdkVersion so the IDE or whatever knows what libraries to include when creating your APK.
It sounds like you don't want to target a higher SDK because some methods or objects may be deprecated or even unsupported. That's when you use support libraries, if necessary, for backward compatibility.
You need to change your SDK target to at least the SDK that the method is in so in. You should always be targeting the highest API there is available (currently 19) so your manifest should look like this
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="19" />
if you are still targeting SDK 5 you are in the dark ages
I want to check if the android version is above ICS:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){
I have a code error,
ICE_CREAM_SANDWICH cannot resolved or it is not a field
But I can read here
http://developer.android.com/reference/android/os/Build.VERSION_CODES.html
this is the correct way, where is the mistake?
Edit after Andy Res answer
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
But if I check Project->properties->Android, ICS doesn't appear, from Android 3.1 to Android 4.1. I suppose this is the error, how can I solve it?
But if I check Project->properties->Android, ICS doesn't appear, from Android 3.1 to Android 4.1. I suppose this is the error, how can I solve it?
Use the SDK Manager and download the SDK(s) that you want. They will then appear in the list for you to set your build target.
Simply use this
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 14){
Make sure the project build target is Android 4.0 or higher.
(Assuming you use Eclipse, you would do this by: Right clicking on the project -> Properties -> From the left menu click on Android -> then choose the target)