Android11.0(R) framework 新增类 lint 编码检查问题

312 阅读2分钟

从 10.0 移植了几个类过来,没想到一编译出来几十个 errors,这就很离谱,明明是现成的代码。

后来仔细看了错误 log 提示,Your API changes are triggering API Lint warnings or errors

详细 log 如下

26 new API lint issues were found.
See tools/metalava/API-LINT.md for how to handle these.
************************************************************
Your API changes are triggering API Lint warnings or errors.
To make these errors go away, fix the code according to the
error and/or warning messages above.

If it is not possible to do so, there are workarounds:

1. You can suppress the errors with @SuppressLint("<id>")
2. You can update the baseline by executing the following
   command:
       cp \
       "/home/xxx/android11.0/8788/alps/out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api_lint_baseline.txt" \
       "/home/xxx/android11.0/8788/alps/frameworks/base/api/lint-baseline.txt"
   To submit the revised baseline.txt to the main Android
   repository, you will need approval.
************************************************************
[ 45% 4001/8864] //frameworks/base:test-api-stubs-docs metalava merged [common]

那就按照提示,把 out 下的 api_lint_baseline.txt copy 到 frameworks/base/api/lint-baseline.txt

再次编译没啥变化,还是一样的错误提示。网上找了这篇

Android R 系统编译时 Lint 工具检查问题记录

看评论里说的如果 lint-baseline.txt 不生效,去检查 frameworks/base/StubLibraries.bp 中是否有添加依赖。

后来我把依赖加进去后,再次编译错误提示变了,有点类似需要执行

make api-stubs-docs-non-updatable make api-stubs-docs-non-updatable-update-current-api make api-stubs-docs-update-current-api

但是执行了以后好像错误依旧。

解决办法

根据错误 log 提示找到对应地方添加 @SuppressLint("")

frameworks/base/core/java/android/hardware/player/AndroidPlayer.java:63: error: Registration methods should have overload that accepts delivery Executor: `AndroidPlayer` [ExecutorRegistration]
frameworks/base/core/java/android/hardware/player/CodecPlayerImpl.java:16: error: Don't expose your implementation details: `CodecPlayerImpl` ends with `Impl` [EndsWithImpl]
frameworks/base/core/java/android/hardware/player/CodecPlayerImpl.java:62: error: Registration methods should have overload that accepts delivery Executor: `CodecPlayerImpl` [ExecutorRegistration]
frameworks/base/core/java/android/hardware/player/CodecPlayerImpl.java:496: error: Returned time values are strongly encouraged to be in milliseconds unless you need the extra precision, was `getVsyncDurationNanos` [MethodNameUnits]
frameworks/base/core/java/android/hardware/player/MediaCodecPlayer.java:34: error: Registration methods should have overload that accepts delivery Executor: `MediaCodecPlayer` [ExecutorRegistration]
frameworks/base/core/java/android/hardware/player/MediaCodecPlayer.java:15: error: All constants must be defined at compile time: android.hardware.player.MediaCodecPlayer#PLAY_TIME_MS [CompileTimeConstant]
frameworks/base/core/java/android/hardware/player/MediaTimeProvider.java:9: error: Returned time values are strongly encouraged to be in milliseconds unless you need the extra precision, was `getVsyncDurationNanos` [MethodNameUnits]
frameworks/base/core/java/android/hardware/player/VideoFrameReleaseTimeHelper.java:91: error: Returned time values are strongly encouraged to be in milliseconds unless you need the extra precision, was `getVsyncDurationNanos` [MethodNameUnits]
8 new API lint issues were found.
See tools/metalava/API-LINT.md for how to handle these.
************************************************************
Your API changes are triggering API Lint warnings or errors.
To make these errors go away, fix the code according to the
error and/or warning messages above.

If it is not possible to do so, there are workarounds:

1. You can suppress the errors with @SuppressLint("<id>")
2. You can add a baseline file of existing lint failures
   to the build rule of api-stubs-docs-non-updatable.
************************************************************

import android.annotation.NonNull; import android.annotation.SuppressLint;

导入上面这两个包

@SuppressLint("") 中的 id 就是错误 log 中的 [xxxxxx]

@NonNull 也可以用 @SuppressLint("MissingNullability")

具体修改

CodecPlayerImpl.java:62: error: Registration methods should have overload that accepts delivery Executor: CodecPlayerImpl [ExecutorRegistration]

 @SuppressLint("ExecutorRegistration")
    public CodecPlayerImpl(@NonNull Surface surface,
                           @NonNull ImageReader imageReader,
                           double defaultDisplayRefreshRate, boolean AudioDecodeOnly,
                           @NonNull CodecState.OnVideoImageAvailablelistener onVideoImageAvailablelistener,
                           @NonNull OnCodecErrorListener onCodecErrorListener){

CodecPlayerImpl.java:496: error: Returned time values are strongly encouraged to be in milliseconds unless you need the extra precision, was getVsyncDurationNanos [MethodNameUnits]

 @SuppressLint("MethodNameUnits")
    public long getVsyncDurationNanos() {
        if (mFrameReleaseTimeHelper != null) {
            return mFrameReleaseTimeHelper.getVsyncDurationNanos();
        } else {
            return -1;
        }
    }

Android API 规范