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 field. 23 * 24 * This annotation should be used in code which accesses a field by reflection (or a similar 25 * indirect means, such as JNI). 26 * 27 * The generated keep rule will be conditional, which indicates to optimizers / shrinkers that it 28 * should only keep the target field in the final application if the annotated code is reachable in 29 * the final application. 30 * 31 * @see GenerateKeepForConstructor 32 * @see GenerateKeepForMethod 33 */ 34 @Retention(AnnotationRetention.BINARY) 35 @Repeatable 36 @Target( 37 AnnotationTarget.CLASS, 38 AnnotationTarget.FIELD, 39 AnnotationTarget.FUNCTION, 40 AnnotationTarget.CONSTRUCTOR, 41 ) 42 public annotation class GenerateKeepForField( 43 /** 44 * Class containing the field accessed by reflection. 45 * 46 * Mutually exclusive with [className]. 47 */ 48 val classConstant: KClass<*> = Unspecified::class, 49 50 /** 51 * Class name (or pattern) containing the field accessed by reflection. 52 * 53 * Mutually exclusive with [classConstant]. 54 */ 55 val className: String = "", 56 57 /** Field name (or pattern) accessed by reflection. */ 58 val fieldName: String, 59 60 /** 61 * Class of field accessed by reflection. 62 * 63 * Ignored if not specified. 64 * 65 * Mutually exclusive with [fieldClassName]. 66 */ 67 val fieldClass: KClass<*> = Unspecified::class, 68 69 /** 70 * Class (or class pattern) of field accessed by reflection. 71 * 72 * Ignored if not specified. 73 * 74 * Mutually exclusive with [fieldClass]. 75 */ 76 val fieldClassName: String = "", 77 ) 78