案例:
Image without contentDescription:
问题概要:[Accessibility] Missing contentDescription attribute on image
解决办法:所有的imageview都添加属性 android:contentDescription=”@string/desc” 此处字符串不能为“”
其实这个属性是方便一些生理功能有缺陷的人使用应用程序的。比如我们有一个ImageView里面放置一张颜色复杂的图片,可能一些色弱色盲的人,分不清这张图片中画的是什么东西。
如果用户安装了辅助浏览工具比如TalkBack,TalkBack就会大声朗读出用户目前正在浏览的内容。TextView控件TalkBack可以直接读出里面的内容,但是ImageView TalkBack就只能去 读
contentDescription的值,告诉用户这个图片到底是什么。
2.Correctness 缺陷模式匹配
案例:
(1)WifiManager Leak On versions prior to Android N (24), initializing the WifiManager via Context#getSystemService can cause a memory leak if the context is not the application context. Change context.getSystemService(…) to context.getApplicationContext().getSystemService(…).
Android N平台,创建WifiManager实例的时候,对应的context需要使用Apllication Context,不然会引起内存泄漏。
(2)Fragment not instantiatable From the Fragment documentation: Every fragment must have an empty constructor, so it can be instantiated when restoring its activity’s state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().在使用Fragment的时候,只要创建一个空的构造方法,不要创建带参数的构造方法,对应的参数通过setArguments(Bundle)传入。具体原因是:Activity重新创建时,会重新构建它所管理的Fragment,原先的Fragment的字段值将会全部丢失,但是通过Fragment.setArguments(Bundlebundle)方法设置的bundle会保留下来。所以尽量使用Fragment.setArguments(Bundlebundle)方式来传递参数
3.Internationalization 检查代码书写是否兼容了app国际化标准。
案例:
Hardcoding text attributes directly in layout files is bad for several reasons: * When creating configuration variations (for example for landscape or portrait)you have to repeat the actual text (and keep it up to date when making changes) * The application cannot be translated to other languages by just adding new translations for existing string resources. There are quickfixes to automatically extract this hardcoded string into a resource lookup.
1.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. This is particularly useful when the value types are primitives like ints, where you can use SparseIntArray and avoid auto-boxing the values from int to Integer. If you need to construct a HashMap because you need to call an API outside of your control which requires a Map, you can suppress this warning using for example the @SuppressLint annotation.HashMap内部是使用一个默认容量为16的数组来存储数据的,而数组中每一个元素却又是一个链表的头结点,所以,更准确的来说,HashMap内部存储结构是使用哈希表的拉链结构(数组+链表).
SparseArray比HashMap更省内存,在某些条件下性能更好,主要是因为它避免了对key的自动装箱(int转为Integer类型),它内部则是通过两个数组来进行数据存储的,一个存储key,另外一个存储value,为了优化性能,它内部对数据还采取了压缩的方式来表示稀疏数组的数据,从而节约内存空间.
2.Static Field Leaks: A static field will leak contexts. Non-static inner classes have an implicit reference to their outer class. If that outer class is for example a Fragment or Activity, then this reference means that the long-running handler/loader/task will hold a reference to the activity which prevents it from getting garbage collected. Similarly, direct field references to activities and fragments from these longer running instances can cause leaks. ViewModel classes should never point to Views or non-application Contexts.
非静态内部类对于外部类存在引用,如果外部类是Activity或者Fragment,被非静态内部类长生命周期对象比如handler、loader、task引用,那么会引起activity或者fragment的资源不会正常被垃圾回收。因此,避免使用长生命周期对象引用activites或者fragments。
5. Security 检查代码书写是否考虑到了访问控制等安全问题
案例:
1.Content provider does not require permission Content providers are exported by default and any application on the system can potentially use them to read and write data. If the content provider provides access to sensitive data, it should be protected by specifying export=false in the manifest or by protecting it with a permission that can be granted to other applications.
2.Code might contain an auth leak Strings in java apps can be discovered by decompiling apps, this lint check looks for code which looks like it may contain an url with a username and password
Usability 检查代码书写是否考虑到了最好的使用交互体验
案例
1.Convert to WebP The WebP format is typically more compact than PNG and JPEG. As of Android 4.2.1 it supports transparency and lossless conversion as well. Note that there is a quickfix in the IDE which lets you perform conversion. Launcher icons must be in the PNG format.
WebP格式占用空间比PNG小很多,通过转换格式使用WebP格式的图片,可以有效减小APK包的大小。
2.Text size is too small Avoid using sizes smaller than 12sp.避免使用小于12sp的文字,可能因为太小不方便用户浏览。