背景
最近在用lint做一個代碼規(guī)范的檢測,雖然意義不是特別大,但是也有一定的作用,這里把自己遇到的一些給記錄一下,后面會不定時補充:
DisableBaselineAlignment
Set `android:baselineAligned="false"` on this element for better performance
當LinerLayout的子View都是ViewGroup(自定義控件除外)時,Lint認為它的子View已經(jīng)不需要基準線對齊了,為了不讓LinerLayout去做無用的計算對齊的操作,提出了如上警告,修改掉之后就可以提高性能。
DrawAllocation
Avoid object allocations during draw/layout operations (preallocate and reuse instead)
用Android自帶的lint測試,發(fā)現(xiàn)上面這個問題。。。。百度一番發(fā)現(xiàn)。。。在ondraw/onlayout中盡量不要用new 對象的操作。。。因為ondraw/onlayout會經(jīng)常被調(diào)用;這樣比較耗內(nèi)存。。。。
DuplicateIncludedIds
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 be unique within any chain of included layouts, or `Activity#findViewById()` can return an unexpected view.
如果是兩個獨立的布局用同一個ID,是可以的。但是,如果是用include標簽把它們聯(lián)合起來的話,那么id就必須是唯一的。
ExportedContentProvider
Exported content providers can provide access to potentially sensitive data
其實就是說這些內(nèi)容可以被其他應(yīng)用訪問到,如果這是你想的就標記忽略,否則就去掉:
android:exported="true"
ExportedService
Exported service does not require permission
其實就是說這些內(nèi)容可以被其他應(yīng)用訪問到,如果這是你想的就標記忽略或者設(shè)置訪問權(quán)限,否則就這只:
android:exported="false"
ExportedReceiver
Exported receiver does not require permission
其實就是說這些內(nèi)容可以被其他應(yīng)用訪問到,如果這是你想的就標記忽略或者設(shè)置訪問權(quán)限,否則就這只:
android:exported="false"
GetInstance
`Cipher.getInstance` should not be called without setting the encryption mode and padding
密碼不應(yīng)該被稱為密碼模式,也不需要設(shè)置密碼模式,因為android的默認模式是歐洲,這是不安全的。
Google了一下貌似說是Android的Bug大家參考一下
Deprecated
`android:singleLine` is deprecated: Use `maxLines="1"` instead
這個東西是說android:singleLine`是一個過時的方法,讓你用maxLines="1"來替代這個地方會有坑,有些老手機上是不好用的maxLines="1",所以我們只能選擇忽略
ExtraTranslation
The resource string "`network_exception`" has been marked as `translatable="false"`
大概的意思就是你的“network_exception”寫了translatable="false"
但是你又在其他地方做了多語言,出現(xiàn)這種問題大概的原因就是我們在開發(fā)的時候多個人或者多個Module寫了相同的資源,導致了沖突,建議統(tǒng)一維護和修改就可以了。
InvalidPackage
Invalid package reference in library; not included in Android: `java.time`. Referenced from `com.alibaba.fastjson.parser.deserializer.Jdk8DateCodec`.
拿阿里的某一個Jar包為例,大概的意思就是你使用了Android中不支持的API
LongLogTag
Log tags are only allowed to be at most 23 tag characters long.
后面的日志長度不能超過23個字符,你可以修改自己的日志,也可以直接關(guān)閉這一個lint檢測。
NewApi
這個的意思就是你使用的方法有API限制,需要做API判斷,如果你確認自己已經(jīng)做了判斷,那就直接標記忽略就行了。
RestrictedApi
FragmentManager.getFragments can only be called from within the same library group (groupId=com.android.support)
拿FragmentManager.getFragments舉例,大概的意思是說這個方法在support包里面如果support包升級了可能會變化,這種我們選擇不改,當我們升級support包的時候監(jiān)控一下就可以了。
WrongConstant
傳遞參數(shù)的類型不對。
ResourceAsColor
需要傳遞一個color值,不能直接用color的ID。
ButtonStyle
Buttons in button bars should be borderless; use `style="?android:attr/buttonBarButtonStyle"` (and `?android:attr/buttonBarStyle` on the parent)
兩個 Buttons 放在一個布局里會被判斷為按鈕欄,需要添加樣式取消它的邊框。個人感覺如果我們自己設(shè)置了背景什么的就不需要處理了,否則樣式可能會變
HardcodedText
在代碼中直接寫了文字,我們應(yīng)該寫到string文件里面,這個我們一定要養(yǎng)成良好的習慣。
InflateParams
Avoid passing `null` as the view root (needed to resolve layout parameters on the inflated layout's root element)
具體的說明可以參考這篇文章LayoutInflater使用的正確姿勢