Android R上面对于系统api的修改变更有了新的检查方式,对于代码静态检查变得更严格了,代码中类型或是参数有些必须要添加 @NonNull 等注释,否则编译不过。编译报错日志大致如下
See tools/metalava/API-LINT.md for how to handle these.
$************************************************************\nYour API changes are triggering API Lint warnings or errors.\nTo make these errors go away, fix the code according to the\nerror and/or warning messages above.\n\nIf it is not possible to do so, there are workarounds:\n\n1. You can suppress the errors with @SuppressLint("<id>")\n2. You can update the baseline by executing the following\n command:\n cp \\\n "out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api_lint_baseline.txt" \\\n "frameworks/base/api/lint-baseline.txt"\n To submit the revised baseline.txt to the main Android\n repository, you will need approval.\n************************************************************\n
(咳咳咳,工具报错日志格式贼丑,看到里面\n,猜测是原本想换行格式化显示,结果被当字符输出了,不知道是我环境问题还是大佬们写的时候问题)
日志中提示我们相关信息,里面出现的两个文件是关键的
out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api_lint_baseline.txt
frameworks/base/api/lint-baseline.txt
文件一就是记录编译中不符合规范的代码条数,会比从编译log中查看更直观些,编译log有时候打的真的不全,贼坑,也不知道为啥
文件二就是我们可以添加过滤防止报错的配置文件,具体添加后面说明
举几个常见栗子如下
1、返回类型缺少非空判断
Missing nullability on method `buildParamsForInviteConferenceParticipants` return [MissingNullability]
2、使用了具象类,要求使用父类,这里就是 List 来替换 ArrayList ,使接口可以更宽泛
Parameter type is concrete collection (`java.util.ArrayList`); must be higher-level interface [ConcreteCollection]
3、静态工具类缺少私有构造函数
Fully-static utility classes must not have constructor
修改方式有两种
一是在代码中添加报错提示的注释,如下
public static @NonNull Bundle buildParamsForInviteConferenceParticipants(
@NonNull String callId, @NonNull ArrayList<String> numbers) {
return obtainBuilder(ACTION_INVITE_CONFERENCE_PARTICIPANTS)
.putStringParam(PARAM_STRING_CALL_ID, callId)
.putStringArrayListParam(PARAM_STRING_ARRAY_LIST_NUMBERS, numbers)
.build();
}
二是修改系统的 frameworks/base/api/lint-baseline.txt 把报错部分添加上,跳过编译检查报错
ConcreteCollection: android.telecom.TelecomHelper.InCallServiceHelper#buildParamsForInviteConferenceParticipants(String, java.util.ArrayList<java.lang.String>) parameter #1:
添加时按照 class#mothod() parameter #index 格式,mothod 参数把类型写全,parameter # 后面从0开始,注明是参数几。这种方式有时候没用,还没搞清楚为啥。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)