In android ,cant get inline reified parameter when minifyenable = true

31 views Asked by At
fun aaa() {
    BuildDsl<String>().let {
        it.adapter<MainRecyclerItemFragmentAdapterBinding> {
        }
    }
}
class BuildDsl<T> {
    inline fun <reified VB : ViewBinding> adapter(
        block: AAA<T, VB>.() -> Unit,
    ) = object : AAA<T, VB>() {}.apply {
        block()
    }
}
open class AAA<T, VB : ViewBinding> {
    init {
        this::class.java.getSuperGenericParam(ViewBinding::class.java) as Class<ViewBinding>
    }
    private fun Class<*>.getSuperGenericParam(expectedSuperClass: Class<*>): Class<*> {
        var clazz: Class<*>? = this
        var target: Class<*>? = null
        Log.e("actualTypeArguments02", "$clazz")
        while (clazz != null) {
            val type = clazz.genericSuperclass
            Log.e("actualTypeArguments03", "$type")
            if (type is ParameterizedType) {
                val actualTypeArguments = type.actualTypeArguments
                actualTypeArguments.forEach {
                    Log.e("actualTypeArguments04", "$it")
                }
                actualTypeArguments.firstOrNull {
                    it is Class<*> && expectedSuperClass.isAssignableFrom(it)
                }?.let {
                    target = it as Class<*>
                }
                if (target != null) {
                    break
                } else {
                    clazz = clazz.superclass
                }
            } else {
                clazz = clazz.superclass
            }
        }
        return target
            ?: throw IllegalArgumentException("'$this' or its parent classes must have a generic parameter implemented from '${expectedSuperClass}'")
    }
}

here is my code. and I aleady keep the AAA class and the BuildDsl class.

it works when minifyenable = false,but when minifyenable = true,i cannot get the actualTypeArguments, what should i do now? Thanks to all of you.

0

There are 0 answers