UsesReflectionToConstruct


@Retention(value = AnnotationRetention.BINARY)
@Repeatable
@Target(allowedTargets = [AnnotationTarget.CLASS, AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR])
public 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>")

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
}
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
}

Summary

Public constructors

UsesReflectionToConstruct(
    @NonNull KClass<@NonNull ?> classConstant,
    @NonNull String className,
    @NonNull KClass[] parameterTypes,
    @NonNull String[] parameterTypeNames
)

Public methods

final @NonNull KClass<@NonNull ?>

Class to be instantiated.

final @NonNull String

Class to be instantiated.

final @NonNull String[]

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

final @NonNull KClass[]

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

Public constructors

UsesReflectionToConstruct

public UsesReflectionToConstruct(
    @NonNull KClass<@NonNull ?> classConstant,
    @NonNull String className,
    @NonNull KClass[] parameterTypes,
    @NonNull String[] parameterTypeNames
)

Public methods

getClassConstant

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

Class to be instantiated.

Mutually exclusive with className.

getClassName

public final @NonNull String getClassName()

Class to be instantiated.

Mutually exclusive with classConstant.

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 parameterTypes.

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 parameterTypeNames.