• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file.
4 package com.android.tools.r8wrappers.utils;
5 
6 import com.android.tools.r8.Diagnostic;
7 import com.android.tools.r8.DiagnosticsHandler;
8 import com.android.tools.r8.DiagnosticsLevel;
9 import com.android.tools.r8.errors.DuplicateTypeInProgramAndLibraryDiagnostic;
10 import com.android.tools.r8.errors.UnsupportedMainDexListUsageDiagnostic;
11 
12 public class WrapperDiagnosticsHandler implements DiagnosticsHandler {
13 
14   private boolean printInfoDiagnostics = false;
15   private boolean warnOnUnsupportedMainDexList = false;
16   private DiagnosticsLevel duplicateTypesLevel = DiagnosticsLevel.INFO;
17 
setPrintInfoDiagnostics(boolean value)18   public void setPrintInfoDiagnostics(boolean value) {
19     printInfoDiagnostics = value;
20   }
21 
setWarnOnUnsupportedMainDexList(boolean value)22   public void setWarnOnUnsupportedMainDexList(boolean value) {
23     warnOnUnsupportedMainDexList = value;
24   }
25 
setDuplicateTypesDiagnosticsLevel(DiagnosticsLevel level)26   public void setDuplicateTypesDiagnosticsLevel(DiagnosticsLevel level) {
27     duplicateTypesLevel = level;
28   }
29 
30   @Override
modifyDiagnosticsLevel(DiagnosticsLevel level, Diagnostic diagnostic)31   public DiagnosticsLevel modifyDiagnosticsLevel(DiagnosticsLevel level, Diagnostic diagnostic) {
32     if (warnOnUnsupportedMainDexList
33         && diagnostic instanceof UnsupportedMainDexListUsageDiagnostic) {
34       return DiagnosticsLevel.WARNING;
35     }
36     if (diagnostic instanceof DuplicateTypeInProgramAndLibraryDiagnostic) {
37       level = duplicateTypesLevel;
38       if ((level == DiagnosticsLevel.WARNING || level == DiagnosticsLevel.ERROR) &&
39           isBenignDuplicateType((DuplicateTypeInProgramAndLibraryDiagnostic) diagnostic)) {
40         level = DiagnosticsLevel.INFO;
41       }
42     }
43     if (!printInfoDiagnostics && level == DiagnosticsLevel.INFO) {
44       return DiagnosticsLevel.NONE;
45     }
46     return level;
47   }
48 
isBenignDuplicateType( DuplicateTypeInProgramAndLibraryDiagnostic diagnostic)49   private static boolean isBenignDuplicateType(
50       DuplicateTypeInProgramAndLibraryDiagnostic diagnostic) {
51     // The Jetpack annotation lib statically links duplicate Kotlin deps. As the annotation lib is
52     // unused at runtime, treat it as benign for now.
53     // TODO(b/326561340): Resolve this static duplication in Jetpack module updates.
54     final boolean isKotlinAnnotationDuplicate =
55         diagnostic.getType().getTypeName().startsWith("kotlin") &&
56         diagnostic.getLibraryOrigin().toString().contains("androidx.annotation_annotation");
57     return isKotlinAnnotationDuplicate;
58   }
59 }
60