xref: /aosp_15_r20/tools/metalava/API-LINT.md (revision 115816f9299ab6ddd6b9673b81f34e707f6bacab)
1*115816f9SAndroid Build Coastguard Worker# API Lint
2*115816f9SAndroid Build Coastguard Worker
3*115816f9SAndroid Build Coastguard WorkerMetalava can optionally run warn about various API problems based on the Android
4*115816f9SAndroid Build Coastguard WorkerAPI Council's guidelines, described in go/android-api-guidelines.
5*115816f9SAndroid Build Coastguard Worker
6*115816f9SAndroid Build Coastguard WorkerThese rules are not always exact. For example, you should avoid using acronyms
7*115816f9SAndroid Build Coastguard Workerin names; e.g. a method should be named handleUri, not handleURI. But what about
8*115816f9SAndroid Build Coastguard WorkergetZOrder?  This is probably better than getZorder, but metalava can't tell
9*115816f9SAndroid Build Coastguard Workerwithout consulting a dictionary.
10*115816f9SAndroid Build Coastguard Worker
11*115816f9SAndroid Build Coastguard WorkerTherefore, there are cases where you want to say "ok, that's good advice in
12*115816f9SAndroid Build Coastguard Workergeneral, but wrong here". In order to avoid having this warningshow up again
13*115816f9SAndroid Build Coastguard Workerand again, there are two ways to mark an issue such that it is no longer
14*115816f9SAndroid Build Coastguard Workerflagged.
15*115816f9SAndroid Build Coastguard Worker
16*115816f9SAndroid Build Coastguard Worker(Note that metalava will not report issues on classes, methods and fields that
17*115816f9SAndroid Build Coastguard Workerare deprecated since these are presumably already known to be bad and are already
18*115816f9SAndroid Build Coastguard Workerdiscouraged.)
19*115816f9SAndroid Build Coastguard Worker
20*115816f9SAndroid Build Coastguard Worker## Suppressing with @Suppress
21*115816f9SAndroid Build Coastguard Worker
22*115816f9SAndroid Build Coastguard WorkerNext to an error message, metalava will include the issue id. For example,
23*115816f9SAndroid Build Coastguard Workerhere's a sample error message:
24*115816f9SAndroid Build Coastguard Worker
25*115816f9SAndroid Build Coastguard Worker    src/android/pkg/MyStringImpl.java:3: error: Don't expose your implementation details: MyStringImpl ends with Impl [EndsWithImpl]
26*115816f9SAndroid Build Coastguard Worker
27*115816f9SAndroid Build Coastguard WorkerHere the id is "EndsWithImpl". You can suppress this with the @SuppressLint
28*115816f9SAndroid Build Coastguard Workerannotation:
29*115816f9SAndroid Build Coastguard Worker
30*115816f9SAndroid Build Coastguard Worker    ...
31*115816f9SAndroid Build Coastguard Worker    import android.annotation.SuppressLint;
32*115816f9SAndroid Build Coastguard Worker    ...
33*115816f9SAndroid Build Coastguard Worker
34*115816f9SAndroid Build Coastguard Worker    @SuppressLint("EndsWithImpl")
35*115816f9SAndroid Build Coastguard Worker    public class MyStringImpl {
36*115816f9SAndroid Build Coastguard Worker        ...
37*115816f9SAndroid Build Coastguard Worker
38*115816f9SAndroid Build Coastguard Worker## Suppressing with baselines
39*115816f9SAndroid Build Coastguard Worker
40*115816f9SAndroid Build Coastguard WorkerMetalava also has support for "baselines", which are files which record all the
41*115816f9SAndroid Build Coastguard Workercurrent warnings and errors in the codebase. When metalava runs, it looks up the
42*115816f9SAndroid Build Coastguard Workerbaseline to see if a given issue is already listed in the baseline, and if so,
43*115816f9SAndroid Build Coastguard Workerit is silently ignored.
44*115816f9SAndroid Build Coastguard Worker
45*115816f9SAndroid Build Coastguard WorkerYou can pass a flag to metalava ("--update-baseline") to tell it to update the
46*115816f9SAndroid Build Coastguard Workerbaseline files with any new errors it comes across instead of reporting
47*115816f9SAndroid Build Coastguard Workerthem. With soong, if you specify the baseline explicitly, like this:
48*115816f9SAndroid Build Coastguard Worker
49*115816f9SAndroid Build Coastguard Worker    --- a/Android.bp
50*115816f9SAndroid Build Coastguard Worker    +++ b/Android.bp
51*115816f9SAndroid Build Coastguard Worker    @@ -1678,6 +1678,7 @@ droidstubs {
52*115816f9SAndroid Build Coastguard Worker         },
53*115816f9SAndroid Build Coastguard Worker         api_lint: {
54*115816f9SAndroid Build Coastguard Worker             enabled: true,
55*115816f9SAndroid Build Coastguard Worker        ==>  baseline_filename: "api/baseline.txt", <==
56*115816f9SAndroid Build Coastguard Worker         }
57*115816f9SAndroid Build Coastguard Worker         jdiff_enabled: true,
58*115816f9SAndroid Build Coastguard Worker     }
59*115816f9SAndroid Build Coastguard Worker
60*115816f9SAndroid Build Coastguard Workerthen the build system will automatically supply `--update-baseline` on your
61*115816f9SAndroid Build Coastguard Workerbehalf to a generated file in the out/ folder, and if there's a failure metalava
62*115816f9SAndroid Build Coastguard Workerwill tell you where to find the updated baseline which you can then copy into
63*115816f9SAndroid Build Coastguard Workerplace:
64*115816f9SAndroid Build Coastguard Worker
65*115816f9SAndroid Build Coastguard Worker    ...
66*115816f9SAndroid Build Coastguard Worker    93 new API lint issues were found. See tools/metalava/API-LINT.md for how to handle these.
67*115816f9SAndroid Build Coastguard Worker    ************************************************************
68*115816f9SAndroid Build Coastguard Worker    Your API changes are triggering API Lint warnings or errors.
69*115816f9SAndroid Build Coastguard Worker    To make these errors go away, you have two choices:
70*115816f9SAndroid Build Coastguard Worker
71*115816f9SAndroid Build Coastguard Worker    1. You can suppress the errors with @SuppressLint("<id>")
72*115816f9SAndroid Build Coastguard Worker    2. You can update the baseline by executing the following
73*115816f9SAndroid Build Coastguard Worker       command:
74*115816f9SAndroid Build Coastguard Worker           cp \
75*115816f9SAndroid Build Coastguard Worker           out/soong/.intermediates/frameworks/base/system-api-stubs-docs/android_common/api/system-baseline.txt \
76*115816f9SAndroid Build Coastguard Worker           frameworks/base/api/system-baseline.txt
77*115816f9SAndroid Build Coastguard Worker       To submit the revised baseline.txt to the main Android
78*115816f9SAndroid Build Coastguard Worker       repository, you will need approval.
79*115816f9SAndroid Build Coastguard Worker    ************************************************************
80*115816f9SAndroid Build Coastguard Worker
81*115816f9SAndroid Build Coastguard Worker
82*115816f9SAndroid Build Coastguard Worker
83*115816f9SAndroid Build Coastguard WorkerThen re-run the build and you should now see diffs to the baseline file; git
84*115816f9SAndroid Build Coastguard Workerdiff to make sure you're really only marking the issues you intended to include.
85*115816f9SAndroid Build Coastguard Worker
86*115816f9SAndroid Build Coastguard Worker    $ git status
87*115816f9SAndroid Build Coastguard Worker    ...
88*115816f9SAndroid Build Coastguard Worker    Untracked files:
89*115816f9SAndroid Build Coastguard Worker      (use "git add <file>..." to include in what will be committed)
90*115816f9SAndroid Build Coastguard Worker
91*115816f9SAndroid Build Coastguard Worker          baseline.txt
92*115816f9SAndroid Build Coastguard Worker
93*115816f9SAndroid Build Coastguard Worker## Copying File Manually
94*115816f9SAndroid Build Coastguard Worker
95*115816f9SAndroid Build Coastguard WorkerIn the near future the build system will not allow source files to be modified
96*115816f9SAndroid Build Coastguard Workerby the build. At that point you'll need to manually copy in the file instead.
97*115816f9SAndroid Build Coastguard WorkerDuring the build, before failing, metalava will emit a message like this:
98*115816f9SAndroid Build Coastguard Worker
99*115816f9SAndroid Build Coastguard Worker    ...
100*115816f9SAndroid Build Coastguard Worker    metalava wrote updated baseline to out/something/baseline.txt
101*115816f9SAndroid Build Coastguard Worker    ...
102*115816f9SAndroid Build Coastguard Worker
103*115816f9SAndroid Build Coastguard WorkerAt that point you can copy the file to where you need:
104*115816f9SAndroid Build Coastguard Worker
105*115816f9SAndroid Build Coastguard Worker```sh
106*115816f9SAndroid Build Coastguard Worker$ cp out/something/baseline.txt frameworks/base/api/baseline.txt
107*115816f9SAndroid Build Coastguard Worker```
108*115816f9SAndroid Build Coastguard Worker
109*115816f9SAndroid Build Coastguard Worker## Existing Issues
110*115816f9SAndroid Build Coastguard Worker
111*115816f9SAndroid Build Coastguard WorkerYou can view the exact set of existing issues (current APIs that get flagged by
112*115816f9SAndroid Build Coastguard WorkerAPI lint) by inspecting the baseline files in the source tree, but to get a
113*115816f9SAndroid Build Coastguard Workersense of the types of issues that are more likely to have a false positive,
114*115816f9SAndroid Build Coastguard Workerhere's the existing distribution as of early 2019:
115*115816f9SAndroid Build Coastguard Worker
116*115816f9SAndroid Build Coastguard Worker    Count Issue Id                       Rule Severity
117*115816f9SAndroid Build Coastguard Worker    --------------------------------------------------
118*115816f9SAndroid Build Coastguard Worker
119*115816f9SAndroid Build Coastguard Worker      932 ProtectedMember                M7   error
120*115816f9SAndroid Build Coastguard Worker      321 OnNameExpected                      warning
121*115816f9SAndroid Build Coastguard Worker      288 ArrayReturn                         warning
122*115816f9SAndroid Build Coastguard Worker      269 ActionValue                    C4   error
123*115816f9SAndroid Build Coastguard Worker      266 NoByteOrShort                  FW12 warning
124*115816f9SAndroid Build Coastguard Worker      221 ExecutorRegistration           L1   warning
125*115816f9SAndroid Build Coastguard Worker      211 AllUpper                       C2   error
126*115816f9SAndroid Build Coastguard Worker      206 GetterSetterNames              M6   error
127*115816f9SAndroid Build Coastguard Worker      185 BannedThrow                         error
128*115816f9SAndroid Build Coastguard Worker      172 SamShouldBeLast                     warning
129*115816f9SAndroid Build Coastguard Worker      159 NoClone                             error
130*115816f9SAndroid Build Coastguard Worker      159 ParcelNotFinal                 FW8  error
131*115816f9SAndroid Build Coastguard Worker      119 NotCloseable                        warning
132*115816f9SAndroid Build Coastguard Worker      105 ListenerLast                   M3   warning
133*115816f9SAndroid Build Coastguard Worker       81 ConcreteCollection             CL2  error
134*115816f9SAndroid Build Coastguard Worker       78 StaticUtils                         error
135*115816f9SAndroid Build Coastguard Worker       76 IntentName                     C3   error
136*115816f9SAndroid Build Coastguard Worker       74 VisiblySynchronized            M5   error
137*115816f9SAndroid Build Coastguard Worker       72 GenericException               S1   error
138*115816f9SAndroid Build Coastguard Worker       65 KotlinOperator                      warning
139*115816f9SAndroid Build Coastguard Worker       57 AcronymName                    S1   warning
140*115816f9SAndroid Build Coastguard Worker       55 ParcelCreator                  FW3  error
141*115816f9SAndroid Build Coastguard Worker       54 ParcelConstructor              FW3  error
142*115816f9SAndroid Build Coastguard Worker       53 UseIcu                              warning
143*115816f9SAndroid Build Coastguard Worker       48 Enum                           F5   error
144*115816f9SAndroid Build Coastguard Worker       41 RethrowRemoteException         FW9  error
145*115816f9SAndroid Build Coastguard Worker       37 AutoBoxing                     M11  error
146*115816f9SAndroid Build Coastguard Worker       35 StreamFiles                    M10  warning
147*115816f9SAndroid Build Coastguard Worker       28 IntentBuilderName              FW1  warning
148*115816f9SAndroid Build Coastguard Worker       27 ServiceName                    C4   error
149*115816f9SAndroid Build Coastguard Worker       26 ListenerInterface              L1   error
150*115816f9SAndroid Build Coastguard Worker       25 ContextFirst                   M3   error
151*115816f9SAndroid Build Coastguard Worker       25 InterfaceConstant              C4   error
152*115816f9SAndroid Build Coastguard Worker       24 CallbackInterface              CL3  error
153*115816f9SAndroid Build Coastguard Worker       24 RegistrationName               L3   error
154*115816f9SAndroid Build Coastguard Worker       23 IllegalStateException          S1   warning
155*115816f9SAndroid Build Coastguard Worker       22 EqualsAndHashCode              M8   error
156*115816f9SAndroid Build Coastguard Worker       22 PackageLayering                FW6  warning
157*115816f9SAndroid Build Coastguard Worker       18 MinMaxConstant                 C8   warning
158*115816f9SAndroid Build Coastguard Worker       18 SingletonConstructor                error
159*115816f9SAndroid Build Coastguard Worker       17 MethodNameUnits                     error
160*115816f9SAndroid Build Coastguard Worker       15 MissingBuildMethod                  warning
161*115816f9SAndroid Build Coastguard Worker       15 UserHandleName                      warning
162*115816f9SAndroid Build Coastguard Worker       14 UserHandle                          warning
163*115816f9SAndroid Build Coastguard Worker       13 ResourceFieldName                   error
164*115816f9SAndroid Build Coastguard Worker       12 ManagerLookup                       error
165*115816f9SAndroid Build Coastguard Worker       11 ManagerConstructor                  error
166*115816f9SAndroid Build Coastguard Worker        9 CallbackMethodName             L1   error
167*115816f9SAndroid Build Coastguard Worker        9 ParcelableList                      warning
168*115816f9SAndroid Build Coastguard Worker        8 CallbackName                   L1   warning
169*115816f9SAndroid Build Coastguard Worker        7 HeavyBitSet                         error
170*115816f9SAndroid Build Coastguard Worker        7 ResourceValueFieldName         C7   error
171*115816f9SAndroid Build Coastguard Worker        6 CompileTimeConstant                 error
172*115816f9SAndroid Build Coastguard Worker        6 SetterReturnsThis              M4   warning
173*115816f9SAndroid Build Coastguard Worker        4 EndsWithImpl                        error
174*115816f9SAndroid Build Coastguard Worker        4 TopLevelBuilder                     warning
175*115816f9SAndroid Build Coastguard Worker        4 UseParcelFileDescriptor        FW11 error
176*115816f9SAndroid Build Coastguard Worker        3 MentionsGoogle                      error
177*115816f9SAndroid Build Coastguard Worker        3 StartWithLower                 S1   error
178*115816f9SAndroid Build Coastguard Worker        2 AbstractInner                       warning
179*115816f9SAndroid Build Coastguard Worker        2 BuilderSetStyle                     warning
180*115816f9SAndroid Build Coastguard Worker        2 OverlappingConstants           C1   warning
181*115816f9SAndroid Build Coastguard Worker        2 PairedRegistration             L2   error
182*115816f9SAndroid Build Coastguard Worker        2 SingularCallback               L1   error
183*115816f9SAndroid Build Coastguard Worker        2 StartWithUpper                 S1   error
184*115816f9SAndroid Build Coastguard Worker        1 ContextNameSuffix              C4   error
185*115816f9SAndroid Build Coastguard Worker        1 KotlinKeyword                       error
186*115816f9SAndroid Build Coastguard Worker    --------------------------------------------------
187*115816f9SAndroid Build Coastguard Worker     4902
188*115816f9SAndroid Build Coastguard Worker
189*115816f9SAndroid Build Coastguard Worker(This is generated when metalava is invoked with both --verbose and --update-baseline.)
190