# 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)

*** ** * ** ***


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

<br />

*** ** * ** ***

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/androidx/annotation/keep/UsesReflectionToAccessMethod` |   |
| `https://developer.android.com/reference/androidx/annotation/keep/UsesReflectionToAccessField` |   |

## Summary

| ### Public methods |
|---|---|
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.reflect/-k-class/index.html<@https://developer.android.com/reference/androidx/annotation/NonNull ?>` | `https://developer.android.com/reference/androidx/annotation/keep/UsesReflectionToConstruct#getClassConstant()()` Class to be instantiated. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull https://developer.android.com/reference/java/lang/String.html` | `https://developer.android.com/reference/androidx/annotation/keep/UsesReflectionToConstruct#getClassName()()` Class to be instantiated. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull String[]` | `https://developer.android.com/reference/androidx/annotation/keep/UsesReflectionToConstruct#getParameterTypeNames()()` Defines which constructor to keep by specifying the parameter list types. |
| `final @https://developer.android.com/reference/androidx/annotation/NonNull KClass[]` | `https://developer.android.com/reference/androidx/annotation/keep/UsesReflectionToConstruct#getParameterTypes()()` Defines which constructor to keep by specifying the parameter list types. |

## Public methods

### getClassConstant

```
public final @NonNull KClass<@NonNull ?> getClassConstant()
```

Class to be instantiated.

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

### getClassName

```
public final @NonNull String getClassName()
```

Class to be instantiated.

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

### getParameterTypeNames

```
public final @NonNull String[] getParameterTypeNames()
```

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/androidx/annotation/keep/UsesReflectionToConstruct#(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)`.

### getParameterTypes

```
public final @NonNull KClass[] getParameterTypes()
```

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/androidx/annotation/keep/UsesReflectionToConstruct#(kotlin.reflect.KClass,kotlin.String,kotlin.Array,kotlin.Array)`.