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(
    classConstant: KClass<*>,
    className: String,
    parameterTypes: Array<KClass<*>>,
    parameterTypeNames: Array<String>
)
Cmn

Public properties

KClass<*>

Class to be instantiated.

Cmn
String

Class to be instantiated.

Cmn
Array<String>

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

Cmn
Array<KClass<*>>

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

Cmn

Public constructors

UsesReflectionToConstruct

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

Public properties

classConstant

val classConstant: KClass<*>

Class to be instantiated.

Mutually exclusive with className.

className

val className: String

Class to be instantiated.

Mutually exclusive with classConstant.

parameterTypeNames

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

parameterTypes

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