1 /* 2 * Copyright 2025 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package androidx.annotation.keep 18 19 import kotlin.reflect.KClass 20 21 /** 22 * Generate a conditional keep rule for code that indirectly accesses a constructor of a class / 23 * interface, or it's subclasses / interface implementers. 24 * 25 * This annotation should be used in code which instantiates a class by reflection (or a similar 26 * indirect means, such as JNI). 27 * 28 * The generated keep rule will be conditional, which indicates to optimizers / shrinkers that it 29 * should only keep the target constructor in the final application if the annotated code is 30 * reachable in the final application. 31 * 32 * `@GenerateKeepForConstructor` is a convenience for `@GenerateKeepForMethod(methodName = 33 * "<init>")` 34 * 35 * @see GenerateKeepForMethod 36 * @see GenerateKeepForField 37 */ 38 @Retention(AnnotationRetention.BINARY) 39 @Repeatable 40 @Target( 41 AnnotationTarget.CLASS, 42 AnnotationTarget.FIELD, 43 AnnotationTarget.FUNCTION, 44 AnnotationTarget.CONSTRUCTOR, 45 ) 46 public annotation class GenerateKeepForConstructor( 47 /** 48 * Class to be instantiated. 49 * 50 * Mutually exclusive with [className]. 51 */ 52 val classConstant: KClass<*> = Unspecified::class, 53 54 /** 55 * Class to be instantiated. 56 * 57 * Mutually exclusive with [classConstant]. 58 */ 59 val className: String = "", 60 61 /** 62 * Defines which constructor to keep by specifying set of parameter classes passed. 63 * 64 * Defaults to `[ Unspecified::class ]`, which will keep all constructors. 65 * 66 * Mutually exclusive with [paramClassNames]. 67 */ 68 val params: Array<KClass<*>> = [Unspecified::class], 69 70 /** 71 * Defines which constructor to keep by specifying set of parameter classes passed. 72 * 73 * Defaults to `[""]`, which will keep all constructors. 74 * 75 * Mutually exclusive with [params] 76 */ 77 val paramClassNames: Array<String> = [""], 78 ) 79