1*115816f9SAndroid Build Coastguard Worker# Signature Formats 2*115816f9SAndroid Build Coastguard Worker 3*115816f9SAndroid Build Coastguard WorkerThis document describes the signature file format created and used by metalava, 4*115816f9SAndroid Build Coastguard Workerdoclava, apicheck, etc. 5*115816f9SAndroid Build Coastguard Worker 6*115816f9SAndroid Build Coastguard WorkerThere are currently 3 versions of this format: 7*115816f9SAndroid Build Coastguard Worker 8*115816f9SAndroid Build Coastguard Worker1. ~~The format emitted by doclava, and used for Android's signature files up 9*115816f9SAndroid Build Coastguard Worker through Android P. Note that this isn't actually a single format; it evolved 10*115816f9SAndroid Build Coastguard Worker over time, so older signature files vary a bit (many of these changes were 11*115816f9SAndroid Build Coastguard Worker due to bugs getting fixed, such as type parameters missing from classes 12*115816f9SAndroid Build Coastguard Worker and methods until they start appearing), and some were deliberate changes, 13*115816f9SAndroid Build Coastguard Worker such as dropping the "final" modifier in front of every member if the 14*115816f9SAndroid Build Coastguard Worker containing class is final.~~ 15*115816f9SAndroid Build Coastguard Worker 16*115816f9SAndroid Build Coastguard Worker <br/>**This version is deprecated and can no longer be specified as an output 17*115816f9SAndroid Build Coastguard Worker format. It can still be consumed but there are no guarantees that it will 18*115816f9SAndroid Build Coastguard Worker behave as expected.**<br/><br/> 19*115816f9SAndroid Build Coastguard Worker 20*115816f9SAndroid Build Coastguard Worker2. The "new" format, which is described below, and is used in Android Q. This 21*115816f9SAndroid Build Coastguard Worker format adds new information, such as annotations, parameter names and default 22*115816f9SAndroid Build Coastguard Worker values, as well as cleans up a number of things (such as dropping 23*115816f9SAndroid Build Coastguard Worker java.lang. prefixes on types, etc) 24*115816f9SAndroid Build Coastguard Worker 25*115816f9SAndroid Build Coastguard Worker3. This is format v2, but with all nullness annotations replaced by a 26*115816f9SAndroid Build Coastguard Worker Kotlin-syntax, e.g. "?" for nullable types, "!" for unknown/platform types, 27*115816f9SAndroid Build Coastguard Worker and no suffix for non-nullable types. The initial plan was to include this 28*115816f9SAndroid Build Coastguard Worker in format v2, but it was deferred since type-use annotations introduces 29*115816f9SAndroid Build Coastguard Worker some complexities in the implementation. 30*115816f9SAndroid Build Coastguard Worker 31*115816f9SAndroid Build Coastguard Worker 32*115816f9SAndroid Build Coastguard Worker## Motivation 33*115816f9SAndroid Build Coastguard Worker 34*115816f9SAndroid Build Coastguard WorkerWhy did we change from the historical doclava signature format (v1) 35*115816f9SAndroid Build Coastguard Workerto a new format? 36*115816f9SAndroid Build Coastguard Worker 37*115816f9SAndroid Build Coastguard WorkerIn order to support Kotlin better (though this will also benefit Java 38*115816f9SAndroid Build Coastguard Workerdevelopers), we'd like to have nullness annotations (as well as some other 39*115816f9SAndroid Build Coastguard Workerannotations) be a formal part of the SDK. 40*115816f9SAndroid Build Coastguard Worker 41*115816f9SAndroid Build Coastguard WorkerThat means the annotations should be part of the signature files too -- such 42*115816f9SAndroid Build Coastguard Workerthat we can not just record explicitly what the API contract is, but also 43*115816f9SAndroid Build Coastguard Workerenforce that changes are not only deliberate changes but also compatible 44*115816f9SAndroid Build Coastguard Workerchanges. (For example, you can change the return value of a final method from 45*115816f9SAndroid Build Coastguard Workernullable to non null, but not the other way around.) 46*115816f9SAndroid Build Coastguard Worker 47*115816f9SAndroid Build Coastguard WorkerAnd if we were going to change the signature format, we might as well make some 48*115816f9SAndroid Build Coastguard Workerother changes too. 49*115816f9SAndroid Build Coastguard Worker 50*115816f9SAndroid Build Coastguard Worker 51*115816f9SAndroid Build Coastguard Worker### Comments 52*115816f9SAndroid Build Coastguard Worker 53*115816f9SAndroid Build Coastguard WorkerIn v2, line comments (starting with //) are allowed. This allows us to leave 54*115816f9SAndroid Build Coastguard Workerreminders and other issues with the signature source (though the update-api task 55*115816f9SAndroid Build Coastguard Workerwill generally blow these away, so use sparingly.) 56*115816f9SAndroid Build Coastguard Worker 57*115816f9SAndroid Build Coastguard Worker### Header 58*115816f9SAndroid Build Coastguard Worker 59*115816f9SAndroid Build Coastguard WorkerNew signature files (v2+) generally include a file header comment which states 60*115816f9SAndroid Build Coastguard Workerthe version number. This makes it possible for tools to more safely interpret 61*115816f9SAndroid Build Coastguard Workersignature files. For example, in v3 the type "String" means "@NonNull String", 62*115816f9SAndroid Build Coastguard Workerbut in v2 "String" means "String with unknown nullness". 63*115816f9SAndroid Build Coastguard Worker 64*115816f9SAndroid Build Coastguard WorkerThe header looks like this: 65*115816f9SAndroid Build Coastguard Worker 66*115816f9SAndroid Build Coastguard Worker``` 67*115816f9SAndroid Build Coastguard Worker// Signature format: 2.0 68*115816f9SAndroid Build Coastguard Worker``` 69*115816f9SAndroid Build Coastguard Worker 70*115816f9SAndroid Build Coastguard WorkerHere "2" is the major format version; the .0 allows for compatible minor 71*115816f9SAndroid Build Coastguard Workervariations of the format. 72*115816f9SAndroid Build Coastguard Worker 73*115816f9SAndroid Build Coastguard Worker### Include Annotations 74*115816f9SAndroid Build Coastguard Worker 75*115816f9SAndroid Build Coastguard WorkerThe new signature format now includes annotations; not all annotations (such as 76*115816f9SAndroid Build Coastguard Worker@Override etc); only those which are significant for the API, such as nullness 77*115816f9SAndroid Build Coastguard Workerannotations, etc. 78*115816f9SAndroid Build Coastguard Worker 79*115816f9SAndroid Build Coastguard WorkerAnnotations are included on the same line as the class/field/method, right 80*115816f9SAndroid Build Coastguard Workerbefore the modifiers. 81*115816f9SAndroid Build Coastguard Worker 82*115816f9SAndroid Build Coastguard WorkerHere's how this looks: 83*115816f9SAndroid Build Coastguard Worker 84*115816f9SAndroid Build Coastguard Worker 85*115816f9SAndroid Build Coastguard Worker``` 86*115816f9SAndroid Build Coastguard Worker method @Nullable public static Integer compute1(@Nullable java.util.List<java.lang.String>); 87*115816f9SAndroid Build Coastguard Worker``` 88*115816f9SAndroid Build Coastguard Worker 89*115816f9SAndroid Build Coastguard Worker 90*115816f9SAndroid Build Coastguard Worker(Notice how the annotations are not using fully qualified name; that's discussed 91*115816f9SAndroid Build Coastguard Workerbelow.) 92*115816f9SAndroid Build Coastguard Worker 93*115816f9SAndroid Build Coastguard WorkerThe annotations to be included are annotations for annotation types that are not 94*115816f9SAndroid Build Coastguard Workerhidden, and have class file or runtime retention. 95*115816f9SAndroid Build Coastguard Worker 96*115816f9SAndroid Build Coastguard WorkerThe annotations should be sorted alphabetically by fully qualified name. 97*115816f9SAndroid Build Coastguard Worker 98*115816f9SAndroid Build Coastguard Worker 99*115816f9SAndroid Build Coastguard Worker### Use Special Syntax or Nullness Annotations 100*115816f9SAndroid Build Coastguard Worker 101*115816f9SAndroid Build Coastguard Worker(Note: Only in version format 3+) 102*115816f9SAndroid Build Coastguard Worker 103*115816f9SAndroid Build Coastguard WorkerAs a special optimization, since we eventually want **all** APIs to have 104*115816f9SAndroid Build Coastguard Workerexplicit nullness, use Kotlin's syntax for nullness. That means that for 105*115816f9SAndroid Build Coastguard Workernullable elements, we add "?" after the type, for unknown nullness we add "!", 106*115816f9SAndroid Build Coastguard Workerand otherwise there's no suffix. In other words: 107*115816f9SAndroid Build Coastguard Worker 108*115816f9SAndroid Build Coastguard Worker 109*115816f9SAndroid Build Coastguard Worker<table> 110*115816f9SAndroid Build Coastguard Worker <tr> 111*115816f9SAndroid Build Coastguard Worker <td> 112*115816f9SAndroid Build Coastguard Worker </td> 113*115816f9SAndroid Build Coastguard Worker <td>Java Type 114*115816f9SAndroid Build Coastguard Worker </td> 115*115816f9SAndroid Build Coastguard Worker <td>Signature File Type 116*115816f9SAndroid Build Coastguard Worker </td> 117*115816f9SAndroid Build Coastguard Worker </tr> 118*115816f9SAndroid Build Coastguard Worker <tr> 119*115816f9SAndroid Build Coastguard Worker <td>Nullable 120*115816f9SAndroid Build Coastguard Worker </td> 121*115816f9SAndroid Build Coastguard Worker <td>@Nullable String 122*115816f9SAndroid Build Coastguard Worker </td> 123*115816f9SAndroid Build Coastguard Worker <td>String? 124*115816f9SAndroid Build Coastguard Worker </td> 125*115816f9SAndroid Build Coastguard Worker </tr> 126*115816f9SAndroid Build Coastguard Worker <tr> 127*115816f9SAndroid Build Coastguard Worker <td>Not nullable 128*115816f9SAndroid Build Coastguard Worker </td> 129*115816f9SAndroid Build Coastguard Worker <td>@NonNull String 130*115816f9SAndroid Build Coastguard Worker </td> 131*115816f9SAndroid Build Coastguard Worker <td>String 132*115816f9SAndroid Build Coastguard Worker </td> 133*115816f9SAndroid Build Coastguard Worker </tr> 134*115816f9SAndroid Build Coastguard Worker <tr> 135*115816f9SAndroid Build Coastguard Worker <td>Unknown nullability 136*115816f9SAndroid Build Coastguard Worker </td> 137*115816f9SAndroid Build Coastguard Worker <td>String 138*115816f9SAndroid Build Coastguard Worker </td> 139*115816f9SAndroid Build Coastguard Worker <td>String! 140*115816f9SAndroid Build Coastguard Worker </td> 141*115816f9SAndroid Build Coastguard Worker </tr> 142*115816f9SAndroid Build Coastguard Worker</table> 143*115816f9SAndroid Build Coastguard Worker 144*115816f9SAndroid Build Coastguard Worker 145*115816f9SAndroid Build Coastguard WorkerThe above signature line is turned into 146*115816f9SAndroid Build Coastguard Worker 147*115816f9SAndroid Build Coastguard Worker 148*115816f9SAndroid Build Coastguard Worker``` 149*115816f9SAndroid Build Coastguard Worker method public Integer? compute1(java.util.List<java.lang.String!>?); 150*115816f9SAndroid Build Coastguard Worker``` 151*115816f9SAndroid Build Coastguard Worker 152*115816f9SAndroid Build Coastguard Worker 153*115816f9SAndroid Build Coastguard Worker### Clean Up Terminology 154*115816f9SAndroid Build Coastguard Worker 155*115816f9SAndroid Build Coastguard WorkerFormat v2 also cleans up some of the terminology used to describe the class 156*115816f9SAndroid Build Coastguard Workerstructure in the signature file. For example, in v1, an interface is called an 157*115816f9SAndroid Build Coastguard Worker"abstract interface"; an interface extending another interface is said to 158*115816f9SAndroid Build Coastguard Worker"implement" it instead of "extend"-ing it, etc; enums and annotations are just 159*115816f9SAndroid Build Coastguard Workerreferred to as classes that extend java.lang.Enum, or java.lang.Annotation etc. 160*115816f9SAndroid Build Coastguard Worker 161*115816f9SAndroid Build Coastguard WorkerWith these changes, these lines from v1 signature files: 162*115816f9SAndroid Build Coastguard Worker 163*115816f9SAndroid Build Coastguard Worker 164*115816f9SAndroid Build Coastguard Worker``` 165*115816f9SAndroid Build Coastguard Worker public abstract interface List<E> implements java.util.Collection { ... } 166*115816f9SAndroid Build Coastguard Worker public class TimeUnit extends java.lang.Enum { ... } 167*115816f9SAndroid Build Coastguard Worker public abstract class SuppressLint implements java.lang.annotation.Annotation { ... } 168*115816f9SAndroid Build Coastguard Worker``` 169*115816f9SAndroid Build Coastguard Worker 170*115816f9SAndroid Build Coastguard Worker 171*115816f9SAndroid Build Coastguard Workerare replaced by 172*115816f9SAndroid Build Coastguard Worker 173*115816f9SAndroid Build Coastguard Worker 174*115816f9SAndroid Build Coastguard Worker``` 175*115816f9SAndroid Build Coastguard Worker public interface List<E> extends java.util.Collection<E> { ... } 176*115816f9SAndroid Build Coastguard Worker public enum TimeUnit { ... } 177*115816f9SAndroid Build Coastguard Worker public @interface SuppressLint { ... } 178*115816f9SAndroid Build Coastguard Worker``` 179*115816f9SAndroid Build Coastguard Worker 180*115816f9SAndroid Build Coastguard Worker 181*115816f9SAndroid Build Coastguard Worker 182*115816f9SAndroid Build Coastguard Worker### Use Generics Everywhere 183*115816f9SAndroid Build Coastguard Worker 184*115816f9SAndroid Build Coastguard WorkerThe v1 signature files uses raw types in some places but not others. Note that 185*115816f9SAndroid Build Coastguard Workerin the above it was missing from super interface Collection: 186*115816f9SAndroid Build Coastguard Worker 187*115816f9SAndroid Build Coastguard Worker 188*115816f9SAndroid Build Coastguard Worker``` 189*115816f9SAndroid Build Coastguard Worker public abstract interface List<E> implements java.util.Collection { ... } 190*115816f9SAndroid Build Coastguard Worker``` 191*115816f9SAndroid Build Coastguard Worker 192*115816f9SAndroid Build Coastguard Worker 193*115816f9SAndroid Build Coastguard Worker whereas in the v2 format it's included: 194*115816f9SAndroid Build Coastguard Worker 195*115816f9SAndroid Build Coastguard Worker 196*115816f9SAndroid Build Coastguard Worker``` 197*115816f9SAndroid Build Coastguard Worker public interface List<E> extends java.util.Collection<E> { ... } 198*115816f9SAndroid Build Coastguard Worker``` 199*115816f9SAndroid Build Coastguard Worker 200*115816f9SAndroid Build Coastguard Worker 201*115816f9SAndroid Build Coastguard WorkerSimilarly, v1 used erasure in throws clauses. For example, for this method: 202*115816f9SAndroid Build Coastguard Worker 203*115816f9SAndroid Build Coastguard Worker 204*115816f9SAndroid Build Coastguard Worker``` 205*115816f9SAndroid Build Coastguard Worker public <X extends Throwable> T orElseThrow(Supplier<? extends X> exceptionSupplier) throws X 206*115816f9SAndroid Build Coastguard Worker``` 207*115816f9SAndroid Build Coastguard Worker 208*115816f9SAndroid Build Coastguard Workerv1 used this signature: 209*115816f9SAndroid Build Coastguard Worker 210*115816f9SAndroid Build Coastguard Worker 211*115816f9SAndroid Build Coastguard Worker``` 212*115816f9SAndroid Build Coastguard Worker method public <X extends java.lang.Throwable> T orElseThrow(java.util.function.Supplier<? extends X>) throws java.lang.Throwable; 213*115816f9SAndroid Build Coastguard Worker``` 214*115816f9SAndroid Build Coastguard Worker 215*115816f9SAndroid Build Coastguard WorkerNote how that's "throws Throwable" instead of "throws X". This results in b/110302703. 216*115816f9SAndroid Build Coastguard Worker 217*115816f9SAndroid Build Coastguard WorkerIn the v2 format we instead use the correct throws type: 218*115816f9SAndroid Build Coastguard Worker 219*115816f9SAndroid Build Coastguard Worker``` 220*115816f9SAndroid Build Coastguard Worker method public <X extends java.lang.Throwable> T orElseThrow(java.util.function.Supplier<? extends X>) throws X; 221*115816f9SAndroid Build Coastguard Worker``` 222*115816f9SAndroid Build Coastguard Worker 223*115816f9SAndroid Build Coastguard Worker 224*115816f9SAndroid Build Coastguard Worker### Support Annotations 225*115816f9SAndroid Build Coastguard Worker 226*115816f9SAndroid Build Coastguard WorkerThe old format was completely missing annotation type methods: 227*115816f9SAndroid Build Coastguard Worker 228*115816f9SAndroid Build Coastguard Worker``` 229*115816f9SAndroid Build Coastguard Worker public static abstract class ViewDebug.ExportedProperty implements java.lang.annotation.Annotation { 230*115816f9SAndroid Build Coastguard Worker } 231*115816f9SAndroid Build Coastguard Worker``` 232*115816f9SAndroid Build Coastguard Worker 233*115816f9SAndroid Build Coastguard WorkerWe need to include annotation member methods, as well as their default values 234*115816f9SAndroid Build Coastguard Workersince those are API-significant. Here's how this looks in the v2 file format 235*115816f9SAndroid Build Coastguard Worker(also applying the @interface terminology change described above) : 236*115816f9SAndroid Build Coastguard Worker 237*115816f9SAndroid Build Coastguard Worker 238*115816f9SAndroid Build Coastguard Worker``` 239*115816f9SAndroid Build Coastguard Worker public static @interface ViewDebug.ExportedProperty { 240*115816f9SAndroid Build Coastguard Worker method public abstract String category() default ""; 241*115816f9SAndroid Build Coastguard Worker method public abstract boolean deepExport() default false; 242*115816f9SAndroid Build Coastguard Worker method public abstract android.view.ViewDebug.FlagToString[] flagMapping() default {}; 243*115816f9SAndroid Build Coastguard Worker method public abstract boolean formatToHexString() default false; 244*115816f9SAndroid Build Coastguard Worker method public abstract boolean hasAdjacentMapping() default false; 245*115816f9SAndroid Build Coastguard Worker method public abstract android.view.ViewDebug.IntToString[] indexMapping() default {}; 246*115816f9SAndroid Build Coastguard Worker method public abstract android.view.ViewDebug.IntToString[] mapping() default {}; 247*115816f9SAndroid Build Coastguard Worker method public abstract String prefix() default ""; 248*115816f9SAndroid Build Coastguard Worker method public abstract boolean resolveId() default false; 249*115816f9SAndroid Build Coastguard Worker } 250*115816f9SAndroid Build Coastguard Worker``` 251*115816f9SAndroid Build Coastguard Worker 252*115816f9SAndroid Build Coastguard Worker 253*115816f9SAndroid Build Coastguard Worker### Support Kotlin Modifiers 254*115816f9SAndroid Build Coastguard Worker 255*115816f9SAndroid Build Coastguard WorkerThis doesn't currently apply to the SDK, but the signature files are also used 256*115816f9SAndroid Build Coastguard Workerin the support library, and some of these are written in Kotlin and exposes 257*115816f9SAndroid Build Coastguard WorkerKotlin-specific APIs. 258*115816f9SAndroid Build Coastguard Worker 259*115816f9SAndroid Build Coastguard WorkerThat means the v2 format can express API-significant aspects of Kotlin. This 260*115816f9SAndroid Build Coastguard Workerincludes special modifiers, such as sealed, inline, operator, infix, etc: 261*115816f9SAndroid Build Coastguard Worker 262*115816f9SAndroid Build Coastguard Worker``` 263*115816f9SAndroid Build Coastguard Worker method public static operator int get(android.graphics.Bitmap, int x, int y); 264*115816f9SAndroid Build Coastguard Worker method public static infix android.graphics.Rect and(android.graphics.Rect, android.graphics.Rect r); 265*115816f9SAndroid Build Coastguard Worker``` 266*115816f9SAndroid Build Coastguard Worker 267*115816f9SAndroid Build Coastguard Worker### Support Kotlin Properties 268*115816f9SAndroid Build Coastguard Worker 269*115816f9SAndroid Build Coastguard WorkerKotlin's Java support means that it wil take a Kotlin property and compile it 270*115816f9SAndroid Build Coastguard Workerinto getters and setters which you can call from Java. But you cannot calls 271*115816f9SAndroid Build Coastguard Workerthese getters and setters from Kotlin; you **must** use the property 272*115816f9SAndroid Build Coastguard Workersyntax. Therefore, we need to also capture properties in the signature files. If 273*115816f9SAndroid Build Coastguard Workeryou have this Kotlin code: 274*115816f9SAndroid Build Coastguard Worker 275*115816f9SAndroid Build Coastguard Worker 276*115816f9SAndroid Build Coastguard Worker``` 277*115816f9SAndroid Build Coastguard Worker var property2: String? = "initial" 278*115816f9SAndroid Build Coastguard Worker``` 279*115816f9SAndroid Build Coastguard Worker 280*115816f9SAndroid Build Coastguard Workerit will get recorded in the signature files like this: 281*115816f9SAndroid Build Coastguard Worker 282*115816f9SAndroid Build Coastguard Worker``` 283*115816f9SAndroid Build Coastguard Worker property public java.lang.String? property2 = "initial"; 284*115816f9SAndroid Build Coastguard Worker method public java.lang.String? getProperty2(); 285*115816f9SAndroid Build Coastguard Worker method public void setProperty2(java.lang.String? p); 286*115816f9SAndroid Build Coastguard Worker``` 287*115816f9SAndroid Build Coastguard Worker 288*115816f9SAndroid Build Coastguard WorkerThe last two elements are "redundant"; they could be computed from the property 289*115816f9SAndroid Build Coastguard Workername (and included if the property declaration uses special annotations to name 290*115816f9SAndroid Build Coastguard Workerthe getters and setters away from the defaults), but it's helpful to be explicit 291*115816f9SAndroid Build Coastguard Worker(and this allows us to specify the default value). 292*115816f9SAndroid Build Coastguard Worker 293*115816f9SAndroid Build Coastguard Worker### Support Named Parameters 294*115816f9SAndroid Build Coastguard Worker 295*115816f9SAndroid Build Coastguard WorkerKotlin supports default values for parameters, and these are a part of the API 296*115816f9SAndroid Build Coastguard Workercontract, so we need to include them in the signature format. 297*115816f9SAndroid Build Coastguard Worker 298*115816f9SAndroid Build Coastguard WorkerHere's an example: 299*115816f9SAndroid Build Coastguard Worker 300*115816f9SAndroid Build Coastguard Worker``` 301*115816f9SAndroid Build Coastguard Worker method public static void edit(android.content.SharedPreferences, boolean commit); 302*115816f9SAndroid Build Coastguard Worker``` 303*115816f9SAndroid Build Coastguard Worker 304*115816f9SAndroid Build Coastguard WorkerIn v1 files we only list type names, but in v2 we allow an optional parameter 305*115816f9SAndroid Build Coastguard Workername to be specified; "commit" in the above. 306*115816f9SAndroid Build Coastguard Worker 307*115816f9SAndroid Build Coastguard WorkerNote that this isn't just for Kotlin. Just like there are special nullness 308*115816f9SAndroid Build Coastguard Workerannotations to mark up the null contract for an element, we will also have a 309*115816f9SAndroid Build Coastguard Workerspecial annotation to explicitly name a Java parameter: 310*115816f9SAndroid Build Coastguard Worker@android.annotation.ParameterName (which is hidden). This obviously isn't usable 311*115816f9SAndroid Build Coastguard Workerfrom Java, but Kotlin client code can now reference the parameter. 312*115816f9SAndroid Build Coastguard Worker 313*115816f9SAndroid Build Coastguard WorkerTherefore, the following Java code (not signature code) will also produce 314*115816f9SAndroid Build Coastguard Workerexactly the same signature as the above: 315*115816f9SAndroid Build Coastguard Worker 316*115816f9SAndroid Build Coastguard Worker``` 317*115816f9SAndroid Build Coastguard Worker public static void edit(SharedPreferences prefs, @ParameterName("commit") boolean ct) {…} 318*115816f9SAndroid Build Coastguard Worker``` 319*115816f9SAndroid Build Coastguard Worker 320*115816f9SAndroid Build Coastguard Worker(Note how the implementation parameter doesn't have to match the public, API 321*115816f9SAndroid Build Coastguard Workername of the parameter.) 322*115816f9SAndroid Build Coastguard Worker 323*115816f9SAndroid Build Coastguard Worker### Support Default Values 324*115816f9SAndroid Build Coastguard Worker 325*115816f9SAndroid Build Coastguard WorkerIn addition to named parameters, Kotlin also supports default values. These are 326*115816f9SAndroid Build Coastguard Workeralso be part of the v2 signature since (as an example) removing a default value 327*115816f9SAndroid Build Coastguard Workeris a compile-incompatible change. 328*115816f9SAndroid Build Coastguard Worker 329*115816f9SAndroid Build Coastguard WorkerTherefore, the v2 format allows default values to be specified after the type 330*115816f9SAndroid Build Coastguard Workerand/or parameter name: 331*115816f9SAndroid Build Coastguard Worker 332*115816f9SAndroid Build Coastguard Worker``` 333*115816f9SAndroid Build Coastguard Worker method public static void edit(SharedPreferences, boolean commit = false); 334*115816f9SAndroid Build Coastguard Worker``` 335*115816f9SAndroid Build Coastguard Worker 336*115816f9SAndroid Build Coastguard WorkerFor Kotlin code, the default parameter values are extracted automatically, and 337*115816f9SAndroid Build Coastguard Workerfor Java, just as with parameter names, you can specify a special annotation to 338*115816f9SAndroid Build Coastguard Workerrecord the default value for usage from languages that support default parameter 339*115816f9SAndroid Build Coastguard Workervalues: 340*115816f9SAndroid Build Coastguard Worker 341*115816f9SAndroid Build Coastguard Worker``` 342*115816f9SAndroid Build Coastguard Worker public static void edit(SharedPreferences prefs, @DefaultValue("false") boolean ct) {…} 343*115816f9SAndroid Build Coastguard Worker``` 344*115816f9SAndroid Build Coastguard Worker 345*115816f9SAndroid Build Coastguard Worker 346*115816f9SAndroid Build Coastguard Worker### Include Inherited Methods 347*115816f9SAndroid Build Coastguard Worker 348*115816f9SAndroid Build Coastguard WorkerConsider a scenario where a public class extends a hidden class, and that hidden 349*115816f9SAndroid Build Coastguard Workerclass defines a public method. 350*115816f9SAndroid Build Coastguard Worker 351*115816f9SAndroid Build Coastguard WorkerDoclava did not include these methods in the signature files, but they **were** 352*115816f9SAndroid Build Coastguard Workerpresent in the stub files (and therefore part of the API). In the v2 signature 353*115816f9SAndroid Build Coastguard Workerfile format, we include these. 354*115816f9SAndroid Build Coastguard Worker 355*115816f9SAndroid Build Coastguard WorkerAn example of this is StringBuilder#setLength. According to the old signature 356*115816f9SAndroid Build Coastguard Workerfiles, that method does not exist, but clearly it's there in the SDK. The reason 357*115816f9SAndroid Build Coastguard Workerthis happens is that StringBuilder is a public class which extends hidden class 358*115816f9SAndroid Build Coastguard WorkerAbstractStringBuilder, which defines the public method setLength. 359*115816f9SAndroid Build Coastguard Worker 360*115816f9SAndroid Build Coastguard Worker 361*115816f9SAndroid Build Coastguard Worker### No Hardcoded Enum Methods 362*115816f9SAndroid Build Coastguard Worker 363*115816f9SAndroid Build Coastguard WorkerDoclava always inserted two special methods in the signature files for every 364*115816f9SAndroid Build Coastguard Workerenum: values() and valueOf(): 365*115816f9SAndroid Build Coastguard Worker 366*115816f9SAndroid Build Coastguard Worker``` 367*115816f9SAndroid Build Coastguard Worker public static final class CursorJoiner.Result extends java.lang.Enum { 368*115816f9SAndroid Build Coastguard Worker method public static android.database.CursorJoiner.Result valueOf(java.lang.String); 369*115816f9SAndroid Build Coastguard Worker method public static final android.database.CursorJoiner.Result[] values(); 370*115816f9SAndroid Build Coastguard Worker enum_constant public static final android.database.CursorJoiner.Result BOTH; 371*115816f9SAndroid Build Coastguard Worker enum_constant public static final android.database.CursorJoiner.Result LEFT; 372*115816f9SAndroid Build Coastguard Worker enum_constant public static final android.database.CursorJoiner.Result RIGHT; 373*115816f9SAndroid Build Coastguard Worker } 374*115816f9SAndroid Build Coastguard Worker``` 375*115816f9SAndroid Build Coastguard Worker 376*115816f9SAndroid Build Coastguard WorkerIt didn't do that in stubs, because you can't: those are special methods 377*115816f9SAndroid Build Coastguard Workergenerated by the compiler. There's no reason to list these in the signature 378*115816f9SAndroid Build Coastguard Workerfiles since they're entirely implied by the enum, you can't change them, and 379*115816f9SAndroid Build Coastguard Workerit's just extra noise. 380*115816f9SAndroid Build Coastguard Worker 381*115816f9SAndroid Build Coastguard WorkerIn the new v2 format these are no longer present: 382*115816f9SAndroid Build Coastguard Worker 383*115816f9SAndroid Build Coastguard Worker``` 384*115816f9SAndroid Build Coastguard Worker public static enum CursorJoiner.Result { 385*115816f9SAndroid Build Coastguard Worker enum_constant public static final android.database.CursorJoiner.Result BOTH; 386*115816f9SAndroid Build Coastguard Worker enum_constant public static final android.database.CursorJoiner.Result LEFT; 387*115816f9SAndroid Build Coastguard Worker enum_constant public static final android.database.CursorJoiner.Result RIGHT; 388*115816f9SAndroid Build Coastguard Worker } 389*115816f9SAndroid Build Coastguard Worker``` 390*115816f9SAndroid Build Coastguard Worker 391*115816f9SAndroid Build Coastguard Worker### Remove "deprecated" Modifier 392*115816f9SAndroid Build Coastguard Worker 393*115816f9SAndroid Build Coastguard WorkerThe old signature file format used "deprecated" as if it was a modifier. In the 394*115816f9SAndroid Build Coastguard Workernew format, we instead list these using annotations, @Deprecated. 395*115816f9SAndroid Build Coastguard Worker 396*115816f9SAndroid Build Coastguard Worker### Standard Modifier Order 397*115816f9SAndroid Build Coastguard Worker 398*115816f9SAndroid Build Coastguard WorkerDoclava had a "random" (but stable) order of modifiers. 399*115816f9SAndroid Build Coastguard Worker 400*115816f9SAndroid Build Coastguard WorkerIn the new signature format, we're using the standard modifier order for Java 401*115816f9SAndroid Build Coastguard Workerand Kotlin, wihch more closely mirrors what is done in the source code. 402*115816f9SAndroid Build Coastguard Worker 403*115816f9SAndroid Build Coastguard WorkerVersion format 1 order: 404*115816f9SAndroid Build Coastguard Worker 405*115816f9SAndroid Build Coastguard Worker``` 406*115816f9SAndroid Build Coastguard Workerpublic/protected/private default static final abstract synchronized transient volatile 407*115816f9SAndroid Build Coastguard Worker``` 408*115816f9SAndroid Build Coastguard Worker 409*115816f9SAndroid Build Coastguard WorkerVersion format 2 order: 410*115816f9SAndroid Build Coastguard Worker 411*115816f9SAndroid Build Coastguard Worker``` 412*115816f9SAndroid Build Coastguard Workerpublic/protected/internal/private abstract default static final transient volatile synchronized 413*115816f9SAndroid Build Coastguard Worker``` 414*115816f9SAndroid Build Coastguard Worker 415*115816f9SAndroid Build Coastguard WorkerThe above list doesn't include the Kotlin modifiers, which are inserted 416*115816f9SAndroid Build Coastguard Workeraccording to the Kotlin language style guide: 417*115816f9SAndroid Build Coastguard Workerhttps://kotlinlang.org/docs/reference/coding-conventions.html#modifiers 418*115816f9SAndroid Build Coastguard Worker 419*115816f9SAndroid Build Coastguard Worker### Sort Classes By Fully Qualified Names 420*115816f9SAndroid Build Coastguard Worker 421*115816f9SAndroid Build Coastguard WorkerIn "extends" lists, the signature file can list a comma separated list of 422*115816f9SAndroid Build Coastguard Workerclasses. The classes are listed by fully qualified name, but in v1 it was sorted 423*115816f9SAndroid Build Coastguard Workerby simple name. In the v2 format, we sort by fully qualified name instead. 424*115816f9SAndroid Build Coastguard Worker 425*115816f9SAndroid Build Coastguard Worker### Use Wildcards Consistently 426*115816f9SAndroid Build Coastguard Worker 427*115816f9SAndroid Build Coastguard WorkerDoclava (v1) would sometimes use the type bound <?> and other times use <? 428*115816f9SAndroid Build Coastguard Workerextends Object>. These are equivalent. In the v2 format, <? extends Object> is 429*115816f9SAndroid Build Coastguard Workeralways written as <?>. 430*115816f9SAndroid Build Coastguard Worker 431*115816f9SAndroid Build Coastguard Worker### Annotation Simple Names 432*115816f9SAndroid Build Coastguard Worker 433*115816f9SAndroid Build Coastguard WorkerWe have a number of annotations which are significant for the API -- not just 434*115816f9SAndroid Build Coastguard Workerthe nullness as deprecation ones (which are specially supported in v3 via the 435*115816f9SAndroid Build Coastguard Worker?/! Kotlin syntax and the deprecated "modifier"), but annotations for permission 436*115816f9SAndroid Build Coastguard Workerrequirements, range constraints, valid constant values for an integer, and so 437*115816f9SAndroid Build Coastguard Workeron. 438*115816f9SAndroid Build Coastguard Worker 439*115816f9SAndroid Build Coastguard WorkerIn the codebase, these are typically in the android.annotation. package, 440*115816f9SAndroid Build Coastguard Workerreferencing annotation classes that are generally **not** part of the API. When 441*115816f9SAndroid Build Coastguard Workerwe generate the SDK, we translate these into publicly known annotations, 442*115816f9SAndroid Build Coastguard Workerandroidx.annotation, such that Studio, lint, the Kotlin compiler and others can 443*115816f9SAndroid Build Coastguard Workerrecognize the metadata. 444*115816f9SAndroid Build Coastguard Worker 445*115816f9SAndroid Build Coastguard WorkerThat begs the question: which fully qualified name should we put in the 446*115816f9SAndroid Build Coastguard Workersignature file? The one that appeared in the source (which is hidden, or in the 447*115816f9SAndroid Build Coastguard Workercase of Kotlin code, a special JetBrains nullness annotation), or the one that 448*115816f9SAndroid Build Coastguard Workerit gets translated into? 449*115816f9SAndroid Build Coastguard Worker 450*115816f9SAndroid Build Coastguard WorkerIn v2 we do neither: We use only the simple name of the annotations in the 451*115816f9SAndroid Build Coastguard Workersignature file, for annotations that are in the well known packages. In other 452*115816f9SAndroid Build Coastguard Workerwords, instead of any of these alternative declarations: 453*115816f9SAndroid Build Coastguard Worker 454*115816f9SAndroid Build Coastguard Worker``` 455*115816f9SAndroid Build Coastguard Worker method public void setTitleTextColor(@android.annotation.ColorInt int); 456*115816f9SAndroid Build Coastguard Worker method public void setTitleTextColor(@androidx.annotation.ColorInt int); 457*115816f9SAndroid Build Coastguard Worker``` 458*115816f9SAndroid Build Coastguard Worker 459*115816f9SAndroid Build Coastguard Workerin v2 we have simply 460*115816f9SAndroid Build Coastguard Worker 461*115816f9SAndroid Build Coastguard Worker``` 462*115816f9SAndroid Build Coastguard Worker method public void setTitleTextColor(@ColorInt int); 463*115816f9SAndroid Build Coastguard Worker``` 464*115816f9SAndroid Build Coastguard Worker 465*115816f9SAndroid Build Coastguard Worker### Simple Names in Java.lang 466*115816f9SAndroid Build Coastguard Worker 467*115816f9SAndroid Build Coastguard WorkerIn Java files, you can implicitly reference classes in java.lang without 468*115816f9SAndroid Build Coastguard Workerimporting them. In v2 offer the same thing in signature files. There are several 469*115816f9SAndroid Build Coastguard Workerclasses from java.lang that are used in lots of places in the signature file 470*115816f9SAndroid Build Coastguard Worker(java.lang.String alone is present in over 11,000 lines of the API file), and 471*115816f9SAndroid Build Coastguard Workerother common occurrences are java.lang.Class, java.lang.Integer, 472*115816f9SAndroid Build Coastguard Workerjava.lang.Runtime, etc. 473*115816f9SAndroid Build Coastguard Worker 474*115816f9SAndroid Build Coastguard WorkerThis basically builds on the same idea from having an implicit package for 475*115816f9SAndroid Build Coastguard Workerannotations, and doing the same thing for java.lang: Omitting it when writing 476*115816f9SAndroid Build Coastguard Workersignature files, and implicitly adding it back when reading in signature files. 477*115816f9SAndroid Build Coastguard Worker 478*115816f9SAndroid Build Coastguard WorkerThis only applies to the java.lang package, not any subpackages, so for example 479*115816f9SAndroid Build Coastguard Workerjava.lang.reflect.Method will **not** be shortened to reflect.Method. 480*115816f9SAndroid Build Coastguard Worker 481*115816f9SAndroid Build Coastguard Worker### Type Use Annotations 482*115816f9SAndroid Build Coastguard Worker 483*115816f9SAndroid Build Coastguard WorkerIn v3, "type use annotations" are supported which means annotations can appear 484*115816f9SAndroid Build Coastguard Workerwithin types. 485*115816f9SAndroid Build Coastguard Worker 486*115816f9SAndroid Build Coastguard Worker### Skipping some signatures 487*115816f9SAndroid Build Coastguard Worker 488*115816f9SAndroid Build Coastguard WorkerIf a method overrides another method, and the signatures are the same, the 489*115816f9SAndroid Build Coastguard Workeroverriding method is left out of the signature file. This basically compares the 490*115816f9SAndroid Build Coastguard Workermodifiers, ignoring some that are not API significant (such as "native"). Note 491*115816f9SAndroid Build Coastguard Workeralso that some modifiers are implicit; for example, if a method is implementing 492*115816f9SAndroid Build Coastguard Workera method from an interface, the interface method is implicitly abstract, so the 493*115816f9SAndroid Build Coastguard Workerimplementation will be included in the signature file. 494*115816f9SAndroid Build Coastguard Worker 495*115816f9SAndroid Build Coastguard WorkerIn v2, we take this one step further: If a method differs **only** from its 496*115816f9SAndroid Build Coastguard Workeroverridden method by "final", **and** if the containing class is final, then the 497*115816f9SAndroid Build Coastguard Workermethod is not included in the signature file. The same is the case for 498*115816f9SAndroid Build Coastguard Workerdeprecated. 499*115816f9SAndroid Build Coastguard Worker 500*115816f9SAndroid Build Coastguard Worker### Miscellaneous 501*115816f9SAndroid Build Coastguard Worker 502*115816f9SAndroid Build Coastguard WorkerSome other minor tweaks in v2: 503*115816f9SAndroid Build Coastguard Worker 504*115816f9SAndroid Build Coastguard Worker* Fix formatting for package private elements. These had two spaces of 505*115816f9SAndroid Build Coastguard Worker indentation; this is probably just a bug. The new format aligns their 506*115816f9SAndroid Build Coastguard Worker indentation with all other elements. 507*115816f9SAndroid Build Coastguard Worker* Don't add spaces in type bounds lists (e.g. Map<X,Y>, not Map<X, Y>.) 508*115816f9SAndroid Build Coastguard Worker 509*115816f9SAndroid Build Coastguard Worker## Historical API Files 510*115816f9SAndroid Build Coastguard Worker 511*115816f9SAndroid Build Coastguard WorkerMetalava can read and write these formats. To switch output formats, invoke it 512*115816f9SAndroid Build Coastguard Workerwith for example --format=v2. 513*115816f9SAndroid Build Coastguard Worker 514*115816f9SAndroid Build Coastguard WorkerThe Android source tree also has checked in versions of the signatures for all 515*115816f9SAndroid Build Coastguard Workerthe previous API levels. Metalava can regenerate these for a new format. 516*115816f9SAndroid Build Coastguard WorkerFor example, to update all the signature files to v3, run this command: 517*115816f9SAndroid Build Coastguard Worker 518*115816f9SAndroid Build Coastguard Worker``` 519*115816f9SAndroid Build Coastguard Worker$ metalava android-jars-to-signatures *<android source dir>* --format=v3 520*115816f9SAndroid Build Coastguard Worker``` 521