# UsesReflectionToConstruct

Artifact: [androidx.annotation.keep:keep](https://developer.android.com/jetpack/androidx/releases/annotation-keep) [View Source](https://cs.android.com/search?q=file:androidx/annotation/keep/UsesReflectionToConstruct.kt+class:androidx.annotation.keep.UsesReflectionToConstruct) Added in [1.0.0-alpha01](https://developer.android.com/jetpack/androidx/releases/annotation-keep#1.0.0-alpha01)

*** ** * ** ***

Common/All
- Cmn

  ```
  @Retention(value = AnnotationRetention.BINARY)
  @Repeatable
  @Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR])
  annotation UsesReflectionToConstruct
  ```

*** ** * ** ***

The annotated code uses reflection to indirectly invoke a constructor of a class/interface, or its subclasses / interface implementers.

This annotation indicates to optimizers or shrinkers that the target constructor should be kept if the annotated code is reachable in the final application build.

`@UsesReflectionToConstruct()` is a convenience for `@UsesReflectionToAccessMethod(methodName = "<init>")`

```kotlin
import androidx.annotation.keep.UsesReflectionToConstruct

@UsesReflectionToConstruct(
    className = "com.example.PaymentProcessorImpl",
    parameterTypes = [], // default constructor, no parameters
)
fun constructMyType() {
    // impl class isn't always present, instantiate with reflection to attempt to discover
    Class.forName("com.example.PaymentProcessorImpl").getDeclaredConstructor().newInstance()
        as PaymentProcessor
}
```

```kotlin
import androidx.annotation.keep.UsesReflectionToConstruct

@UsesReflectionToConstruct(
    className = "com.example.PaymentProcessorImpl",
    parameterTypes = [TransactionAccess::class, Long::class],
)
fun constructMyType(transactionAccess: TransactionAccess, timeout: Long) {
    // impl class isn't always present, instantiate with reflection to attempt to discover
    Class.forName("com.example.PaymentProcessorImpl")
        .getDeclaredConstructor(TransactionAccess::class.java, Long::class.java)
        .newInstance() as PaymentProcessor
}
```

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToAccessMethod` |   |
| `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToAccessField` |   |

## Summary

| ### Public constructors |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#UsesReflectionToConstruct(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)( classConstant: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>, className: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html, parameterTypes: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>, parameterTypeNames: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html> )` | Cmn |

| ### Public properties |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>` | `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#classConstant()` Class to be instantiated. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html` | `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#className()` Class to be instantiated. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html>` | `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#parameterTypeNames()` Defines which constructor to keep by specifying the parameter list types. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-array/index.html<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<*>>` | `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#parameterTypes()` Defines which constructor to keep by specifying the parameter list types. | Cmn |

## Public constructors

### UsesReflectionToConstruct

Cmn

```
UsesReflectionToConstruct(
    classConstant: KClass<*> = Unspecified::class,
    className: String = "",
    parameterTypes: Array<KClass<*>> = [Unspecified::class],
    parameterTypeNames: Array<String> = [""]
)
```

## Public properties

### classConstant

Cmn

```
val classConstant: KClass<*>
```

Class to be instantiated.

Mutually exclusive with `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)`.

### className

Cmn

```
val className: String
```

Class to be instantiated.

Mutually exclusive with `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)`.

### parameterTypeNames

Cmn

```
val parameterTypeNames: Array<String>
```

Defines which constructor to keep by specifying the parameter list types.

If neither `parameterTypes` nor `parameterTypeNames` is specified then constructors with all parameter lists are kept.

Mutually exclusive with `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)`.

### parameterTypes

Cmn

```
val parameterTypes: Array<KClass<*>>
```

Defines which constructor to keep by specifying the parameter list types.

If neither `parameterTypes` nor `parameterTypeNames` is specified then constructors with all parameter lists are kept.

Mutually exclusive with `https://developer.android.com/reference/kotlin/androidx/annotation/keep/UsesReflectionToConstruct#(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)`.