工作中Lint工具使用實錄及整理
轉載請聲明,轉自【https://www.cnblogs.com/andy-songwei/p/7090934.html】,謝謝!
AndroidStudio內置的Lint工具,對app中的代碼規范帶來了極大的方便。對內存泄漏、代碼冗余、代碼安全、國際化、代碼規范等很多方面都能檢測,是一款非常強大的工具。本篇文章是自己使用過程中的過程記錄,包括問題檢測,問題分析,問題解決等做了一些總結。希望能對讀者有一定的幫助。
對該工具的掃盲,不再本篇范疇內,讀者可以參考:
(1)Android lint 簡介:http://blog.csdn.net/hudashi/article/details/8333349;
(2)如何在android studio 中使用 link工具:http://www.cnblogs.com/cheerego/p/5175764.html
1.Inspection Result for Inspection Profile 'Project Default'
2 該項給了兩類提示
(1) Image without contentDescription:
問題概要:[Accessibility] Missing contentDescription attribute on image
解決辦法:所有的imageview都添加屬性 android:contentDescription="@string/desc" 此處字符串不能為“”
說明:其實這個屬性是方便一些生理功能有缺陷的人使用應用程序的。比如我們有一個ImageView里面放置一張顏色復雜的圖片,可能一些色弱色盲的人,分不清這張圖片中畫的是什么東西。如果用戶安裝了輔助瀏覽工具比如TalkBack,TalkBack就會大聲朗讀出用戶目前正在瀏覽的內容。TextView控件TalkBack可以直接讀出里面的內容,但是ImageView TalkBack就只能去 讀contentDescription的值,告訴用戶這個圖片到底是什么。
參考:https://stackoverflow.com/questions/8500544/android-lint-contentdescription-warning
(2) Missing labelFor attribute
問題概要:No label views point to this text field with an android:labelFor="@+id/@+id/editableHeight" attribute
解決辦法:EditText添加labelFor屬性,其值為對應的id
參考:http://blog.csdn.net/kx_nullpointer/article/details/18499509
3 Android>Lint>Correctness>Message
(1)Potential Plurals
問題概要:在字符串的國際化工程中,有一個英文字符串為:<string name="details_title">%1$d of %2$d items:</string>
lint提示warning:Formatting %d followed by words ("items"): This should probably be a plural rather than a string
解決辦法:在該項使用plurals的形式定義該字符串
<plurals name="number_of_albumset_video">
<item quantity="one">%1$d video</item>
<item quantity="other">%1$d videos</item>
</plurals >
主要是針對不同語言對單復數等形式的數據進行處理
(2)Unused quantity translations
問題概要: 在中文字string.xml中定義了如下的字符串:
<plurals name="number_of_albumset_photo">
<item quantity="one">%1$d 張照片</item>
<item quantity="other">%1$d 張照片</item>
</plurals >
lint提示For language "zh" (Chinese) the following quantities are not relevant: one
解決辦法:中文中沒有單復數的區分,所以,將"<item quantity="one">%1$d 張照片</item>" 刪除即可
4.Android>Lint>Usability
(1) Button should be borderless
問題概要:在Button控件設置如下屬性android:layout_width="0dp" android:layout_weight="1" 時,提示warning
解決辦法:添加 style="?android:attr/buttonStyle" 屬性
參考:http://blog.csdn.net/lyalei/article/details/48025417
(2) Missing inputType or hint
問題概要:使用EditText時,需要添加一些必備的屬性,如hint,inputType等,否則會提示warning
解決辦法:添加hint屬性 android:hint="";
參考:http://blog.csdn.net/aeolus1019/article/details/8112716
(3)Missing support for Google App Indexing
不知道修改了哪里,改warning消失了 -_-
(4)Text size is too small
問題概要:從用戶角度出發,官方建議開發者Avoid using sizes smaller than 12sp.
根據UI設計師的設計,該處沒有修改
5. Android>Lint>Usability>Icons
該項中可以檢測重復命名的圖片,規范要求必須有的密度文件夾,不同文件夾下應該有針對不同密度的圖片等
6.Android>Lint>Security
(1) AllowBackup/FullBackupContent Problems
問題概要:該項顯示了清單文件中屬性allowBackup=true使得backup/restore會導致數據不安全
解決辦法:將allowBackup設置為false
參考:http://blog.csdn.net/qq_17338093/article/details/55506543
https://wenku.baidu.com/view/c43af666f342336c1eb91a37f111f18583d00c79.html(如何使用adb backup 和 adb restore 進行備份)
https://developer.android.com/preview/backup/index.html(一般打不開)
(2) Using setJavaScriptEnabled
問題概要:Using setJavaScriptEnabled can introduce XSS vulnerabilities into you application, review carefully.
解決辦法:Your code should not invoke setJavaScriptEnabled if you are not sure that your app really requires JavaScript support.
添加該行:@SuppressLint("SetJavaScriptEnabled")
參考:http://blog.csdn.net/huanongjingchao/article/details/18261611
(3) ConentProvider 安全問題(沒有及時現場截圖)
問題概要:Exported content providers can provide access to potentially sensitive
解決辦法:如果確定要給其他應用調用 設置export = "true" 時,加上tools:ignore="ExportedContentProvider",並在manifest文件開頭添加tools命名空間;如果只在應用內使用,設置export = "false"
參考: https://stackoverflow.com/questions/13448480/exported-content-providers-can-provide-access-to-potentially-sensitive-data
(4) Service安全問題(沒有及時現場截圖)
問題概要:Exported service does not require permission
解決辦法:由於我使用的Service不需要給其他app使用,設置android:exported="false"
參考:http://www.cnblogs.com/myorange/p/5425835.html
7 Android > Lint > Internationalization
(1)Hardcoded text:該項主要針對xml布局文件中的硬編碼問題,[I18N] Hardcoded string "title", should use @string resource;[I18N] Hardcoded string "這是PreviewLayout", should use @string resource
(2)Overlapping items in RelativeLayout:該項主要針對RelativeLayout中部分空間相互重疊了,只要調整大小和位置即可。
(3)TextView Internationalization:該項主要針對代碼中直接使用字符串的問題,盡量提取取來作為資源文件存儲在xml中
8.Android > Lint > Performance(性能)
(1)FrameLayout can be replaced with <merge> tag
問題描述:xml布局文件中,FrameLayout作為根布局時,會給出該warning(自己通過觀察和對比總結的,不敢保證是否准確)
解決辦法:使用merge來代替FrameLayout ;
參考:https://stackoverflow.com/questions/21488027/warning-this-framelayout-can-be-replaced-with-a-merge-tag/21490723
http://blog.csdn.net/time_hunter/article/details/8664665
(2)Handler reference leaks
問題概要:Handler的使用不當引起內存泄漏
解決辦法:如果按照下面參考網站的建議,handler被定義為static,那么其中引用的很多變量和方法都必須定義為static,會比較麻煩。筆者在按照提其給定的方法 private final MyHandler mHandler = new MyHandler(this); 實例化靜態handler的時候甚至連編譯都無法通過,this處報錯。自己通過嘗試,使用了如下的方法進行了處理,最終解決了hanler內存泄漏的問題
private Handler mHandler = new Handler(getMainLooper());
參考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1106/1922.html (引用了權威開發者的建議,對handler做了一定深度的剖析)
(3) HashMap can be replaced with SparseArray
問題描述:For maps where the keys are of type integer, it's typically more efficient to use the Android SparseArray API. This check identifies scenarios where you might want to consider using SparseArray instead of HashMap for better performance.
解決辦法:如果HashMap中key值為Integer或Long,使用SparseArray來取代
參考:https://stackoverflow.com/questions/25560629/sparsearray-vs-hashmap
(4) Inefficient layout weight
當在一個LinearLayout布局中僅包含一個定義weight屬性的view控件時,直接指定0dp到其width或height屬性更高效,因為在初始化時不再需要測量這個view控件的尺寸,反正到最后它都會霸占所有剩余空間的了。
(5) Layout has too many views
問題概要:檢測某個布局文件,出現warning:activity_share_template_detail.xml has more than 80 views, bad for performance
解決辦法:減少單個頁面的view個數,少於80個
(6) Missing baselineAligned attribute
問題概要:When a LinearLayout is used to distribute the space proportionally between nested layouts, the baseline alignment property should be turned off to make the layout computation faster.(as中的提示)
解決辦法:Set android:baselineAligned="false" on this element for better performance(as中的提示)
參考:http://markjoker.iteye.com/blog/2243398
(7) Missing recycle() calls
問題概要:Many resources, such as TypedArrays, VelocityTrackers, etc., should be recycled (with a recycle() call) after use. This lint check looks for missing recycle() calls.
解決辦法:使用完后,回收即可
(8) Nested layout weights
問題概要:在布局進行嵌套使用時,父布局與子布局都使用了android:layout_weight,但不是必須使用時,會報這個warning
解決辦法:根據實際情況,去掉部分weight屬性
(9) Node can be replaced by a TextView with compound drawables
問題概要:A LinearLayout which contains an ImageView and a TextView can be more efficiently handled as a compound drawable (a single TextView, using the drawableTop, drawableLeft,drawableRight and/or drawableBottom attributes to draw one or more images adjacent to the text). If the two widgets are offset from each other with margins, this can be replaced with a drawablePadding attribute.
解決方法:使用一個Textview + compound drawables 可以替代掉 一個LinearLayout 嵌套 textview和imageview的布局,效率和代碼量明顯極大地優化了
(10) Obsolete layout params
問題概述:The given layout_param is not defined for the given layout, meaning it has no effect. This usually happens when you change the parent layout or move view code around without updating the layout params.This will cause useless attribute processing at runtime, and is misleading for others reading the layout so the parameter should be removed.
解決辦法:把無效的屬性刪除即可。比如LinearLayout子布局中使用layout_toRightOf屬性,屬於無效屬性
(11)Overdraw: Painting regions more than once
問題概述:如果在布局文件xml中的根布局中使用background屬性,會和主題中的background產生重復繪制,導致了“overdraw”
If you set a background drawable on a root view, then you should use a custom theme where the theme background is null. Otherwise, the theme background will be painted first, only to have your custom background completely cover it; this is called "overdraw". NOTE: This detector relies on figuring out which layouts are associated with which activities based on scanning the Java code, and it's currently doing that using an inexact pattern matching algorithm. Therefore, it can incorrectly conclude which activity the layout is associated with and then wrongly complain that a background-theme is hidden. If you want your custom background on multiple pages, then you should consider making a custom theme with your custom background and just using that theme instead of a root element background. Of course it's possible that your custom drawable is translucent and you want it to be mixed with the background. However, you will get better performance if you pre-mix the background with your drawable and use that resulting image or color as a custom theme background instead.
解決辦法:在自定義主題中將background設置為null。因為不是每個界面的根布局都使用同一個背景 ,需要自己定義,所以最好在主題中處理(根據自己的項目中的情況來的)
(12)Unused resources
該處列出了app中的沒有使用過的各種資源,包括圖片,anim文件,顏色,字符串等
9. Android > Lint > Usability > Typography
該項主要檢驗一些排版錯誤
(1) 問題概要:在字符串中用三個"."表示省略號"…" ,導致提示warning:Replace "..." with ellipsis character (…, …) ? (at line 551)
解決辦法:使用標准的省略號"…"
(2)問題概要:在字符串中使用了非標准的連字符,導致提示warning:Replace "-" with an "en dash" character (–, –)
解決辦法:用“–”(有;沒有"")代替“–”;(沒有找到此處應該用的標注符號)
參考:部分符號對應的編碼 http://www.w3school.com.cn/tags/html_ref_symbols.html
更新中...