1. 程式人生 > >android lint三部曲三:Android-Lint 檢查問題列表

android lint三部曲三:Android-Lint 檢查問題列表

這裡的Android-Lint所檢查的問題列表是從AndroidSDK Tools r20.0.3 (ADT v20.0.3)中得來的預設的,執行下列指令即可得到:

$lint --show

$lint --list

--show可獲得詳細列表(id,summary, priority, severity, category and details);--list僅可獲得Issue的id和summary的簡表

Android-Lint相關的文章

Android-Lint的簡述:Lint檢查哪些問題;如何使用;有哪些選項;與其他系統整合。

對Android-Lint發現的問題進行處理。可定製專案中採用的規則。

Android SDK Tools / ADT 20.0.3中所支援的預設檢查的所有問題。

英文版本中文版本英文版本從Android-Lint中直接匯出;本文為中文版本還不完整,對每一條的解釋會逐步完善。

===========

-------------------

Summary: Checksthat AdapterViews do not define their children in XML

Priority: 10 / 10

Severity: Warning

Category

:Correctness

AdapterViews such as ListViews must be configured with datafrom Java code, such as a ListAdapter.

More information:http://developer.android.com/reference/android/widget/AdapterView.html

-------

Summary: Ensuresthat onClick attribute values refer to real methods

Priority: 10 / 10

Severity: Error

Category:Correctness

The onClick attribute value should be the name of a methodin this View's context to invoke when the view is clicked. This name mustcorrespond to a public method that takes exactly one parameter of type View.

Must be a string value, using '\;' to escape characters suchas '\n' or '\uxxxx' for a unicode character.

----------------

Summary: Checksfor 'import android.R' statements, which are usually accidental

Priority: 9 / 10

Severity: Warning

Category:Correctness

Importing android.R is usually not intentional; it sometimeshappens when you use an IDE and ask it to automatically add imports at a timewhen your project's R class it not present.

Once the import is there you might get a lot of"confusing" error messages because of course the fields available onandroid.R are not the ones you'd expect from just looking at your own R class.

-------------

Summary: Looksfor incorrect casts to views that according to the XML are of a different type

Priority: 9 / 10

Severity: Error

Category:Correctness

Keeps track of the view types associated with ids and if itfinds a usage of the id in the Java code it ensures that it is treated as thesame type.

-------------

Summary: DetectXML attributes not using the Android namespace

Priority: 8 / 10

Severity: Warning

Category:Correctness

Most Android views have attributes in the Android namespace.When referencing these attributes you *must* include the namespace prefix, oryour attribute will be interpreted by aapt as just a custom attribute.

-------------

Summary: Looksfor misspellings in namespace declarations

Priority: 8 / 10

Severity: Warning

Category: Correctness

Accidental misspellings in namespace declarations can leadto some very obscure error messages. This check looks for potentialmisspellings to help track these down.

--------

Summary: Looksfor problems in proguard config files

Priority: 8 / 10

Severity: Fatal

Category:Correctness

Using -keepclasseswithmembernames in a proguard config fileis not correct; it can cause some symbols to be renamed which should not be.

Earlier versions of ADT used to create proguard.cfg fileswith the wrong format. Instead of -keepclasseswithmembernames use-keepclasseswithmembers, since the old flags also implies "allowshrinking" which means symbols only referred to from XML and not Java(such as possibly CustomViews) can get deleted.

More information: http://http://code.google.com/p/android/issues/detail?id=16384

---------------

Summary: Checksthat ScrollViews have exactly one child widget

Priority: 8 / 10

Severity: Warning

Category:Correctness

ScrollViews can only have one child widget. If you want morechildren, wrap them in a container layout.

----------

Summary: Looksfor cycles in style definitions

Priority: 8 / 10

Severity: Fatal

Category:Correctness

There should be no cycles in style definitions as this canlead to runtime exceptions.

More information:http://developer.android.com/guide/topics/ui/themes.html#Inheritance

---------

Summary: Checksfor id references in RelativeLayouts that are not defined elsewhere

Priority: 8 / 10

Severity: Fatal

Category:Correctness

The "@+id/" syntax refers to an existing id, orcreates a new one if it has not already been defined elsewhere. However, thismeans that if you have a typo in your reference, or if the referred view nolonger exists, you do not get a warning since the id will be created on demand.This check catches errors where you have renamed an id without updating all ofthe references to it.

------------

一個Layout內部的id應該是唯一的。

Summary: Checksfor duplicate ids within a single layout

Priority: 7 / 10

Severity: Warning

Category:Correctness

Within a layout, id's should be unique since otherwisefindViewById() can return an unexpected view.

------------------

字串國際化中,同一名字的的String-Array對應的item值不相同

Summary: Checksfor inconsistencies in the number of elements in arrays

Priority: 7 / 10

Severity: Warning

Category:Correctness

When an array is translated in a different locale, it shouldnormally have the same number of elements as the original array. When adding orremoving elements to an array, it is easy to forget to update all the locales,and this lint warning finds inconsistencies like these.

Note however that there may be cases where you really wantto declare a different number of array items in each configuration (for examplewhere the array represents available options, and those options differ fordifferent layout orientations and so on), so use your own judgement to decideif this is really an error.

You can suppress this error type if it finds false errors inyour project.

---------------

Summary: Checkswhether a scrolling widget has any nested scrolling widgets within

Priority: 7 / 10

Severity: Warning

Category:Correctness

A scrolling widget such as a ScrollView should not containany nested scrolling widgets since this has various usability issues

---------------

Summary: Looksfor calls to setColor where a resource id is passed instead of a resolved color

Priority: 7 / 10

Severity: Error

Category: Correctness

Methods that take a color in the form of an integer shouldbe passed an RGB triple, not the actual color resource id. You must call getResources().getColor(resource)to resolve the actual color value first.

--------------

Summary: Checksthat ScrollViews use wrap_content in scrolling dimension

Priority: 7 / 10

Severity: Warning

Category:Correctness

ScrollView children must set their layout_width orlayout_height attributes to wrap_content rather than fill_parent or match_parentin the scrolling dimension

-------------

Summary: Looksfor TextViews being used for input

Priority: 7 / 10

Severity: Warning

Category:Correctness

Using a <TextView> to input text is generally anerror, you should be using <EditText> instead.  EditText is a subclass of TextView, and someof the editing support is provided by TextView, so it's possible to set some input-relatedproperties on a TextView. However, using a TextView along with input attributesis usually a cut & paste error. To input text you should be using<EditText>.

This check also checks subclasses of TextView, such asButton and CheckBox, since these have the same issue: they should not be usedwith editable attributes.

---------------

Summary: Looksfor code editing a SharedPreference but forgetting to call commit() on it

Priority: 6 / 10

Severity: Warning

Category:Correctness

After calling edit() on a SharedPreference, you must callcommit() or apply() on the editor to save the results.

--------------------

Summary: Checksfor duplicate ids across layouts that are combined with include tags

Priority: 6 / 10

Severity: Warning

Category:Correctness

It's okay for two independent layouts to use the same ids.However, if layouts are combined with include tags, then the id's need to beunique within any chain of included layouts, or Activity#findViewById() canreturn an unexpected view.

-----------------

Summary: Flagscustom attributes in libraries, which must use the res-auto-namespace instead

Priority: 6 / 10

Severity: Error

Category:Correctness

When using a custom view with custom attributes in a libraryproject, the layout must use the special namespacehttp://schemas.android.com/apk/res-auto instead of a URI which includes thelibrary project's own package. This will be used to automatically adjust thenamespace of the attributes when the library resources are merged into theapplication project.

---------------

Summary: Checksthat the <uses-sdk> element appears at most once

Priority: 6 / 10

Severity: Fatal

Category:Correctness

The <uses-sdk> element should appear just once; thetools will *not* merge the contents of all the elements so if you split up theatttributes across multiple elements, only one of them will take effect. To fixthis, just merge all the attributes from the various elements into a single<uses-sdk> element.

More information:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html

------

程式碼中使用的某些API高於Manifest中的MinSDK

Summary: FindsAPI accesses to APIs that are not supported in all targeted API versions

Priority: 6 / 10

Severity: Error

Category:Correctness

This check scans through all the Android API calls in the applicationand warns about any calls that are not available on *all* versions targeted by thisapplication (according to its minimum SDK attribute in the manifest).

If your code is *deliberately* accessing newer APIs, and youhave ensured (e.g. with conditional execution) that this code will only ever becalled on a supported platform, then you can annotate your class or method withthe @TargetApi annotation specifying the local minimum SDK to apply, such [email protected](11),such that this check considers 11 rather than your manifest file's minimum SDKas the required API level.

----------

Activity/Service/ContentProvider沒有通過AndroidManifest註冊

Summary: Ensuresthat Activities, Services and Content Providers are registered in the manifest

Priority: 6 / 10

Severity: Warning

Category:Correctness

Activities, services and content providers should beregistered in the AndroidManifext.xml file using <activity>,<service> and <provider> tags.

If your activity is simply a parent class intended to be subclassedby other "real" activities, make it an abstract class.

More information:http://developer.android.com/guide/topics/manifest/manifest-intro.html

----------

Summary: Looksfor hardcoded references to /sdcard

Priority: 6 / 10

Severity: Warning

Category:Correctness

Your code should not reference the /sdcard path directly;instead use Environment.getExternalStorageDirectory().getPath()

More information:http://developer.android.com/guide/topics/data/data-storage.html#filesExternal

---------

Summary: Looksfor code creating a Toast but forgetting to call show() on it

Priority: 6 / 10

Severity: Warning

Category:Correctness

Toast.makeText() creates a Toast but does *not* show it. Youmust call show() on the resulting object to actually make the Toast appear.

-------------

Summary: Ensuresthat Fragment subclasses can be instantiated

Priority: 6 / 10

Severity: Warning

Category:Correctness

From the Fragment documentation: *Every* fragment must havean empty constructor, so it can be instantiated when restoring its activity'sstate. It is strongly recommended that subclasses do not have otherconstructors with parameters, since these constructors will not be called whenthe fragment is re-instantiated; instead, arguments can be supplied by thecaller with setArguments(Bundle) and later retrieved by the Fragment withgetArguments().

More information:http://developer.android.com/reference/android/app/Fragment.html#Fragment()

-------------------

Summary: Checksthat various manifest elements are declared in the right place

Priority: 6 / 10

Severity: Fatal

Category:Correctness

The <uses-library> element should be defined as adirect child of the <application> tag, not the <manifest> tag or an<activity> tag. Similarly, a <uses-sdk> tag much be declared at theroot level, and so on. This check looks for incorrect declaration locations inthe manifest, and complains if an element is found in the wrong place.

More information:http://developer.android.com/guide/topics/manifest/manifest-intro.html

-----------------

Summary: Checksthat an activity is registered only once in the manifest

Priority: 5 / 10

Severity: Error

Category:Correctness

An activity should only be registered once in the manifest.If it is accidentally registered more than once, then subtle errors can occur,since attribute declarations from the two elements are not merged, so you may accidentallyremove previous declarations.

-------------

Summary: Checksfor manifest problems like <uses-sdk> after the <application> tag

Priority: 5 / 10

Severity: Warning

Category:Correctness

The <application> tag should appear after the elementswhich declare which version you need, which features you need, which librariesyou need, and so on. In the past there have been subtle bugs (such as themesnot getting applied correctly) when the <application> tag appears beforesome of these other elements, so it's best to order your manifest in thelogical dependency order.

------------------

Summary: Looksfor unreachable states in a <selector>

Priority: 5 / 10

Severity: Warning

Category:Correctness

In a selector, only the last child in the state list shouldomit a state qualifier. If not, all subsequent items in the list will beignored since the given item will match all.

-----------------

Summary: Makessure that @+id references refer to views in the same layout

Priority: 5 / 10

Severity: Warning

Category:Correctness

The "@+id/" syntax refers to an existing id, orcreates a new one if it has not already been defined elsewhere. However, thismeans that if you have a typo in your reference, or if the referred view nolonger exists, you do not get a warning since the id will be created on demand.

This is sometimes intentional, for example where you arereferring to a view which is provided in a different layout via an include.However, it is usually an accident where you have a typo or you have renamed aview without updating all the references to it.

--------------

Summary: Looksfor code sending text messages to unlocalized phone numbers

Priority: 5 / 10

Severity: Warning

Category:Correctness

SMS destination numbers must start with a country code orthe application code must ensure that the SMS is only sent when the user is inthe same country as the receiver.

----------

Summary: Checksfor potential GridLayout errors like declaring rows and columns outside thedeclared grid dimensions

Priority: 4 / 10

Severity: Fatal

Category:Correctness

Declaring a layout_row or layout_column that falls outsidethe declared size of a GridLayout's rowCount or columnCount is usually anunintentional error.

---------

Summary: Looksfor extraneous text in layout files

Priority: 3 / 10

Severity: Warning

Category:Correctness

Layout resource files should only contain elements andattributes. Any XML text content found in the file is likely accidental (andpotentially dangerous if the text resembles XML and the developer believes thetext to be functional)

---------------

Summary: Looksfor references to private resources

Priority: 3 / 10

Severity: Fatal

Category: Correctness

Private resources should not be referenced; the may not bepresent everywhere, and even where they are they may disappear without notice.

To fix this, copy the resource into your own project. Youcan find the platform resources under $ANDROID_SK/platforms/android-$VERSION/data/res/.

-------------

Summary: Checksfor old proguard.cfg files that contain generic Android rules

Priority: 3 / 10

Severity: Warning

Category:Correctness

Earlier versions of the Android tools bundled a single"proguard.cfg" file containing a ProGuard configuration file suitablefor Android shrinking and obfuscation. However, that version was copied intonew projects, which means that it does not continue to get updated as weimprove the default ProGuard rules for Android.

In the new version of the tools, we have split the ProGuardconfiguration into two halves:

* A simple configuration file containing onlyproject-specific flags, in your project

* A generic configuration file containing the recommendedset of ProGuard options for Android projects. This generic file lives in theSDK install directory which means that it gets updated along with the tools.

In order for this to work, the proguard.config property inthe project.properties file now refers to a path, so you can reference both thegeneric file as well as your own (and any additional files too).

To migrate your project to the new setup, create a newproguard-project.txt file in your project containing any project specificProGuard flags as well as any customizations you have made, then update yourproject.properties file to contain:

proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-projec.txt

----------

使用已經廢棄的API

Summary: Looksfor usages of deprecated layouts, attributes, and so on.

Priority: 2 / 10

Severity: Warning

Category:Correctness

Deprecated views, attributes and so on are deprecatedbecause there is a better way to do something. Do it that new way. You've beenwarned.

-------

避免使用px,使用dp代替

Summary: Looksfor use of the "px" dimension

Priority: 2 / 10

Severity: Warning

Category:Correctness

For performance reasons and to keep the code simpler, theAndroid system uses pixels as the standard unit for expressing dimension orcoordinate values.

That means that the dimensions of a view are alwaysexpressed in the code using pixels, but always based on the current screendensity. For instance, if myView.getWidth() returns 10, the view is 10 pixelswide on the current screen, but on a device with a higher density screen, thevalue returned might be 15. If you use pixel values in your application code towork with bitmaps that are not pre-scaled for the current screen density, youmight need to scale the pixel values that you use in your code to match theun-scaled bitmap source.

More information:http://developer.android.com/guide/practices/screens_support.html#screen-independence

--------------------

Summary: Checksthat the minimum SDK and target SDK attributes are defined

Priority: 2 / 10

Severity: Warning

Category:Correctness

The manifest should contain a <uses-sdk> element whichdefines the minimum minimum API Level required for the application to run, aswell as the target version (the highest API level you have tested the versionfor.)

More information:http://developer.android.com/guide/topics/manifest/uses-sdk-element.html

---------------

Summary: Findsunused namespaces in XML documents

Priority: 1 / 10

Severity: Warning

Category:Correctness

Unused namespace declarations take up space and requireprocessing that is not necessary

2. Correctness:Messages

====================

-------------------

Summary: Checksthat format strings are valid

Priority: 9 / 10

Severity: Error

Category:Correctness:Messages

If a string contains a '%' character, then the string may bea formatting string which will be passed to String.format from Java code toreplace each '%' occurrence with specific values.

This lint warning checks for two related problems:

(1) Formatting strings that are invalid, meaning thatString.format will throw exceptions at runtime when attempting to use theformat string.

(2) Strings containing '%' that are not formatting stringsgetting passed to a String.format call. In this case the '%' will need to beescaped as '%%'.

NOTE: Not all Strings which look like formatting strings areintended for use by String.format; for example, they may contain date formatsintended for android.text.format.Time#format(). Lint cannot always figure outthat a String is a date format, so you may get false warnings in thosescenarios. See the suppress help topic for information on how to suppresserrors in that case.

-------------------