Android――Android lint工具項目資源清理
最近維護的項目已經有兩年多,經過很多前輩的迭代,項目并沒有變得健壯,而變得很臃腫.用Android lint工具清理了一次,清楚了不少廢棄的布局和資源.
1. Android lint工具
可以右鍵項目,Android tools,退出的時候clear lint markers即可

也可以如圖:

2.結果出來了,分析分析

3.xml中view太多,已經超過了80個,影響性能.
布局優化:
盡量使用include、merge、ViewStub標簽,盡量不存在冗余嵌套及過于復雜布局,盡量使用GONE替換INVISIBLE,使用weight后盡量將width和heigh設置為0dp減少運算,Item存在非常復雜的嵌套時考慮使用自定義Item View來取代,減少measure與layout次數等。
列表及Adapter優化;盡量復用getView方法中的相關View,不重復獲取實例導致卡頓,列表盡量在滑動過程中不進行UI元素刷新等。
背景和圖片等內存分配優化;盡量減少不必要的背景設置,圖片盡量壓縮處理顯示,盡量避免頻繁內存抖動等問題出現。
自定義View等繪圖與布局優化;盡量避免在draw、measure、layout中做過于耗時及耗內存操作,尤其是draw方法中,盡量減少draw、measure、layout等執行次數。
避免ANR,不要在UI線程中做耗時操作,遵守ANR規避守則,譬如多次數據庫操作等。
activity_group_number_detail1.xml has more than 80 views, bad for performance
Issue: Checks whether a layout has too many views
Id: TooManyViews
Using too many views in a single layout is bad for performance. Consider using compound drawables or other tricks for reducing the number of views in this layout.

4.沒有定義的id,刪掉就ok
he id "top" is not defined anywhere.
Issue: Checks for id references in RelativeLayouts that are not defined elsewhere
Id: UnknownId

5.同一個XML重復定義id
在同個一個Xml文件的中如果ID同名,則前一個有效,而后一個無效
是不是復制粘貼的時候出錯了?
Duplicate id @+id/group_imageView2, already defined earlier in this layout
Issue: Checks for duplicate ids within a single layout
Id: DuplicateIds

6.ID的引用不在同一級layout中,比如說:控件A在B(B是viewgroup)的下面,而不應該寫成A在B的子控件下面.


7.廢棄的四大組件,在mainfest.xml中沒有清掉.刪除就ok
Class referenced in the manifest, com.baidu.location.f, was not found in the project or the libraries
Issue: Ensures that classes referenced in the manifest are present in the project or libraries
Id: MissingRegistered

8.沒使用的資源,這是重頭戲,對于減小包的大小很有意義.其中包含了xml,dimens等.量比較大,建議先提交SVN之后再刪除,如果出了問題立馬可以還原.
The resource R.drawable.fc_seekbar_thumb appears to be unused
Issue: Looks for unused resources
Id: UnusedResources

9.這里檢測的結果只是提供一種參考,建議用Toast.LENGTH_SHORT或者 Toast.LENGTH_LONG


10.硬編碼的問題,使用Context.getFilesDir().getPath()
Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead
Issue: Looks for hardcoded references to /sdcard
Id: SdCardPath
Your code should not reference the /sdcard path directly; instead use Environment.getExternalStorageDirectory().getPath().

11.大家一看就懂了,viewholder的問題


12.handler導致的內存泄漏問題
一兩句話說不清,下面是已經說清楚的.
http://blog.csdn.net/lijunhuayc/article/details/47999931

13.webview的父控件,寬高建議用match_parent
提示
Placing a <WebView> in a parent element that uses a wrap_content layout_height can lead to subtle
bugs; use match_parent instead
14.I18N的問題就不說了.
總結:Android lint工具主要功能是規范編碼,優化布局性能,去除無用資源.
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答