背景:

1.安卓9之前,libcore中有一个libcore/io/EventLogger.java,但是安卓10之后却没有了

2.EventLogger可以将进程中所有的event事件,收敛到这里,所以可以在代码中任何地方加埋点,然后简单调用writeEvent

    public static void writeEvent(int code, Object... list) {getReporter().report(code, list);}

代码改动:

Aosp android-11

添加 libcore/luni/src/main/java/libcore/util/EventCallback.java
添加 libcore/luni/src/main/java/libcore/util/EventTag.java

编译报错(代码规范相关)

metalava detected access to files that are not explicitly specified. See /data00/home/liuzhuangzhuang.das/work/aosp_sunfish/aosp/out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api-stubs-docs-violations.txt for details.
$************************************************************\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       "/data00/home/liuzhuangzhuang.das/work/aosp_sunfish/aosp$/out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api_lint_baseline.txt" \\\n       "/data00/home/liuzhuangzhuang.das/work/aosp_sunfish/aosp$/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
18:05:51 ninja failed with: exit status 1

把tools/metalava/API-LINT.md文件中拿出来,看看相关报错如何解决

API Lint

Metalava can optionally run warn about various API problems based on the Android
API Council’s guidelines, described in go/android-api-guidelines.

These rules are not always exact. For example, you should avoid using acronyms
in names; e.g. a method should be named handleUri, not handleURI. But what about
getZOrder? This is probably better than getZorder, but metalava can’t tell
without consulting a dictionary.

Therefore, there are cases where you want to say “ok, that’s good advice in
general, but wrong here”. In order to avoid having this warningshow up again
and again, there are two ways to mark an issue such that it is no longer
flagged.

(Note that metalava will not report issues on classes, methods and fields that
are deprecated since these are presumably already known to be bad and are already
discouraged.)

Suppressing with @Suppress

Next to an error message, metalava will include the issue id. For example,
here’s a sample error message:

src/android/pkg/MyStringImpl.java:3: error: Don't expose your implementation details: MyStringImpl ends with Impl [EndsWithImpl]

Here the id is “EndsWithImpl”. You can suppress this with the @SuppressLint
annotation:

...
import android.annotation.SuppressLint;
...@SuppressLint("EndsWithImpl")
public class MyStringImpl {...

Suppressing with baselines

Metalava also has support for “baselines”, which are files which record all the
current warnings and errors in the codebase. When metalava runs, it looks up the
baseline to see if a given issue is already listed in the baseline, and if so,
it is silently ignored.

You can pass a flag to metalava ("–update-baseline") to tell it to update the
baseline files with any new errors it comes across instead of reporting
them. With soong, if you specify the baseline explicitly, like this:

--- a/Android.bp
+++ b/Android.bp
@@ -1678,6 +1678,7 @@ droidstubs {},api_lint: {enabled: true,==>  baseline_filename: "api/baseline.txt", <==}jdiff_enabled: true,}

then the build system will automatically supply --update-baseline on your
behalf to a generated file in the out/ folder, and if there’s a failure metalava
will tell you where to find the updated baseline which you can then copy into
place:

...
93 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, you have two choices:1. You can suppress the errors with @SuppressLint("<id>")
2. You can update the baseline by executing the followingcommand:cp \out/soong/.intermediates/frameworks/base/system-api-stubs-docs/android_common/api/system-baseline.txt \frameworks/base/api/system-baseline.txtTo submit the revised baseline.txt to the main Androidrepository, you will need approval.
************************************************************

Then re-run the build and you should now see diffs to the baseline file; git
diff to make sure you’re really only marking the issues you intended to include.

$ git status
...
Untracked files:(use "git add <file>..." to include in what will be committed)baseline.txt

Copying File Manually

In the near future the build system will not allow source files to be modified
by the build. At that point you’ll need to manually copy in the file instead.
During the build, before failing, metalava will emit a message like this:

...
metalava wrote updated baseline to out/something/baseline.txt
...

At that point you can copy the file to where you need:

$ cp out/something/baseline.txt frameworks/base/api/baseline.txt

Existing Issues

You can view the exact set of existing issues (current APIs that get flagged by
API lint) by inspecting the baseline files in the source tree, but to get a
sense of the types of issues that are more likely to have a false positive,
here’s the existing distribution as of early 2019:

Count Issue Id                       Rule Severity
--------------------------------------------------932 ProtectedMember                M7   error321 OnNameExpected                      warning288 ArrayReturn                         warning269 ActionValue                    C4   error266 NoByteOrShort                  FW12 warning221 ExecutorRegistration           L1   warning211 AllUpper                       C2   error206 GetterSetterNames              M6   error185 BannedThrow                         error172 SamShouldBeLast                     warning159 NoClone                             error159 ParcelNotFinal                 FW8  error119 NotCloseable                        warning105 ListenerLast                   M3   warning81 ConcreteCollection             CL2  error78 StaticUtils                         error76 IntentName                     C3   error74 VisiblySynchronized            M5   error72 GenericException               S1   error65 KotlinOperator                      warning57 AcronymName                    S1   warning55 ParcelCreator                  FW3  error54 ParcelConstructor              FW3  error53 UseIcu                              warning48 Enum                           F5   error41 RethrowRemoteException         FW9  error37 AutoBoxing                     M11  error35 StreamFiles                    M10  warning28 IntentBuilderName              FW1  warning27 ServiceName                    C4   error26 ListenerInterface              L1   error25 ContextFirst                   M3   error25 InterfaceConstant              C4   error24 CallbackInterface              CL3  error24 RegistrationName               L3   error23 IllegalStateException          S1   warning22 EqualsAndHashCode              M8   error22 PackageLayering                FW6  warning18 MinMaxConstant                 C8   warning18 SingletonConstructor                error17 MethodNameUnits                     error15 MissingBuildMethod                  warning15 UserHandleName                      warning14 UserHandle                          warning13 ResourceFieldName                   error12 ManagerLookup                       error11 ManagerConstructor                  error9 CallbackMethodName             L1   error9 ParcelableList                      warning8 CallbackName                   L1   warning7 HeavyBitSet                         error7 ResourceValueFieldName         C7   error6 CompileTimeConstant                 error6 SetterReturnsThis              M4   warning4 EndsWithImpl                        error4 TopLevelBuilder                     warning4 UseParcelFileDescriptor        FW11 error3 MentionsGoogle                      error3 StartWithLower                 S1   error2 AbstractInner                       warning2 BuilderSetStyle                     warning2 OverlappingConstants           C1   warning2 PairedRegistration             L2   error2 SingularCallback               L1   error2 StartWithUpper                 S1   error1 ContextNameSuffix              C4   error1 KotlinKeyword                       error
--------------------------------------------------4902

(This is generated when metalava is invoked with both --verbose and --update-baseline.)

尝试使用@SuppressLint("")方案解决,发现报错

libcore中暂不能使用android.annotation包,用grep搜了一个libcore整个目录,确实没有地方使用。

尝试使用第二种方法(update the baseline),发现依然报错

cp "/data00/home/liuzhuangzhuang.das/work/aosp_sunfish/aosp$/out/soong/.intermediates/frameworks/base/api-stubs-docs/android_common/api_lint_baseline.txt" "/data00/home/liuzhuangzhuang.das/work/aosp_sunfish/aosp$/frameworks/base/api/lint-baseline.txt

然后看到报错的命令行,发现有个地方很有意思

--api-lint-ignore-prefix android.icu. --api-lint-ignore-prefix java. --api-lint-ignore-prefix junit. --api-lint-ignore-prefix org.  --api-lint prebuilts/sdk/30/public/api/android-non-updatable.txt

–api-lint-ignore-prefix这玩意可以忽略lint检查

在frameworks/base/Android.bp中将libcore/util加进去,使lint检查忽略

metalava_framework_docs_args = "--manifest $(location core/res/AndroidManifest.xml) " +"--ignore-classes-on-classpath " +"--hide-package com.android.server " +"--error UnhiddenSystemApi " +"--hide RequiresPermission " +"--hide CallbackInterface " +"--hide MissingPermission --hide BroadcastBehavior " +"--hide HiddenSuperclass --hide DeprecationMismatch --hide UnavailableSymbol " +"--hide SdkConstant --hide HiddenTypeParameter --hide Todo --hide Typo " +"--force-convert-to-warning-nullability-annotations +*:-android.*:+android.icu.*:-dalvik.* " +"--api-lint-ignore-prefix android.icu. " +"--api-lint-ignore-prefix java. " +"--api-lint-ignore-prefix junit. " +"--api-lint-ignore-prefix org. " +"--api-lint-ignore-prefix libcore.util. "

然后又报错了(apilist flag问题)

安卓P之后谷歌定义了api的级别,blacklist, greylist, whitelist等。

我又将EventCallback声明为hide,继续编译

/*** @hide*/
public class EventCallback {.........
}

又报错了

Llibcore/util/BasicLruCache;-><init>(I)V,greylist
Llibcore/util/BasicLruCache;->create(Ljava/lang/Object;)Ljava/lang/Object;,blacklist
Llibcore/util/BasicLruCache;->entryEvicted(Ljava/lang/Object;Ljava/lang/Object;)V,blacklist
Llibcore/util/BasicLruCache;->evictAll()V,greylist
Llibcore/util/BasicLruCache;->get(Ljava/lang/Object;)Ljava/lang/Object;,greylist
Llibcore/util/BasicLruCache;->map:Ljava/util/LinkedHashMap;,greylist
Llibcore/util/BasicLruCache;->maxSize:I,greylist-max-o
Llibcore/util/BasicLruCache;->put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;,greylist
Llibcore/util/BasicLruCache;->snapshot()Ljava/util/Map;,blacklist
Llibcore/util/BasicLruCache;->trimToSize(I)V,greylist-max-o

如果我的aosp是aosp-android10,则不会有该项报错。安卓11才会报错。

原因竟然是安卓11剥离出了一个framwork-wifi模块,该模块找不到EventCallback

解决方法:

framework-wifi模块如何使用的公共api呢?比如android.util.ArrayMap。根据这个思路,最后我又新加了一个android.util.EventCallback.java作为跳板,该跳板调用libcore.util.EventCallback。

再次编译成功的编译了代码。

该方法只是简单的绕过方案。有更好方案的,可留言评论,感谢您的宝贵建议。

package libcore.util;import android.compat.annotation.UnsupportedAppUsage;/*** @hide*/
@libcore.api.CorePlatformApi
public class EventCallback {private static volatile Callback sImpl = null;private EventCallback() {}/*** Used to replace default Reporter for logging events. Must be non-null.*/@UnsupportedAppUsage@libcore.api.CorePlatformApipublic static void setCallback(@NonNull Callback callback) {if (callback == null) {throw new NullPointerException("callback == null");}EventCallback.sImpl = callback;}@libcore.api.CorePlatformApipublic static interface Callback {@UnsupportedAppUsage@libcore.api.CorePlatformApipublic void onEvent(int code, @NonNull String method, @Nullable Object...objs);}@UnsupportedAppUsage@libcore.api.CorePlatformApipublic static void onEvent(int code, @NonNull String method, @Nullable Object... objs) {if (sImpl != null) {sImpl.onEvent(code, method, objs);}}
}

ROM-libcore中新加java文件编译报错相关推荐

  1. 使用cmd打开java文件,报错:“错误,编码GBK的不可映射字符”

    使用cmd打开java文件,报错:"错误,编码GBK的不可映射字符" 今天使用EditPlus写了一个小程序,用cmd运行时报错--"错误,编码GBK的不可映射字符&qu ...

  2. SQL 新加字段查询窗口报错

    SQL 新加字段查询窗口报错 这是由于SQL Server的intellisense的引起的,intellisense是SQL Server的智能记录智能感知功能,即当给sql表名加上".& ...

  3. eclipse中对单独JS文件取消报错的处理

    eclipse中对单独JS文件取消报错的处理 eclipse中js文件报错的情况,或许大家早已习以为常了,那么有什么好的方法可以将其忽略掉呢?如果你也在寻找此问题,那么本文或许可以帮助到你 - 忽略某 ...

  4. .java文件突然报错,且无法新建项目

    这是新建工程的时候弹出来的提示框,打开Details会发现罪魁祸首是这个 An error occurred while automatically activating bundle com.and ...

  5. eclipse运行JAVA文件输出报错

    如上图所示,编写完JAVA文件之后,运行输出之后输出乱码,求解

  6. java文件操作报错:java.io.FileNotFoundException:D..(拒绝访问)

    import java.io.*;/*** @Description: 复制文件,从一个输入流中读取数据,然后通过输出流写入目标位置,一边读一边写* @ClassName: CopyFile* @Ve ...

  7. linux java项目 编译报错_Linux下执行Java程序报错

    在linux下编译java程序,执行javac编译生成class文件时,在centos7终端输入如,javac hello.java    会提示未找到指令,但用java -verison测试环境变量 ...

  8. (完美解决)java文件操作报错:java.io.FileNotFoundException(拒绝访问)

    01-错误信息: Exception in thread "main" java.io.FileNotFoundException: e:b (拒绝访问.)at java.io.F ...

  9. 【错误记录】Kotlin 编译报错 ( Type mismatch: inferred type is String? but String was expected )

    文章目录 一.报错信息 二.解决方案 一.报错信息 Google Play 上架要求 Android 的编译版本 和 目标版本都要高于 30 才可以上传 ; 将 Android 的编译版本 和 目标版 ...

最新文章

  1. 《Adobe Fireworks CS5中文版经典教程》——导读
  2. 鸿蒙os芯片,没有了芯片,华为能靠鸿蒙OS系统打出差异化吗?
  3. spring @Scheduled 注解实现的定时任务 3步走
  4. 泡腾片的打开方式:刀割封条,拔下盖子
  5. FEA-Net论文梳理
  6. Python任务调度模块APScheduler
  7. 电子设备的节能分析与设计
  8. 高等代数100道题及答案解析
  9. 简述CC2530晶振、振荡器与时钟
  10. 白痴qwerta的胡言乱语(一句话日度感想?
  11. Android利用SpannableStringBuilder设置TextView中部分文字的颜色...
  12. C语言编程>第二十六周 ⑥ 请补充fun函数,该函数的功能是:按 “0”到 “9”统计一个字符串中的奇数数字字符各自出现的次数,结果保存在数组num中。注意:不能使用字符串库函数。
  13. jquery视频播放器_jQuery插入QuickTime视频播放器
  14. 微信公众号:开发者工具下载、安装、使用
  15. 【转】七个受用一生的心理寓言
  16. Incorrect string value: ‘\xE9\xBB\x84\xE8\x93\x89‘ for column ‘name‘ at row 1 MySQL 5.7解决方案
  17. [python] say hi
  18. win7(已安装)win10双系统安装教程
  19. 从惠普“质量门”到洋品牌的习惯性傲慢
  20. 深入理解生产则消费者模型

热门文章

  1. 你见过最差的程序员是怎样的?
  2. PTA python-计算利率
  3. 申请出国签证都有哪些?
  4. 《机器学习实战》学习大纲
  5. 路由器缓冲区膨胀导致的TCP传输延迟
  6. 推荐一堆好掂嘅 标头 影嘅东东 (内有靓女,慎入!)
  7. 【Windows】解决windows系统时间与北京时间相差8小时
  8. Maven引入本地jar包,解决打包时找不到本地jar包
  9. 设计模式之注册树模式
  10. 【进阶C语言】通讯录(完整版)