Proguard,如何使构造函数保持其参数具有一些注释?

Proguard, how to keep constructor that it's parameters has some annotations?

如果参数有我的自定义注释,那么构造函数应该保留。

嗯,自己用注释处理器解决了。

创建应用于 class 的注释,该注释应保留构造函数:

@Target(AnnotationTarget.CLASS)
@Retention(AnnotationRetention.BINARY)
annotation class KeepInit

用于项目构建的注释处理器:

fun enclosingType(el: Element): Element {
    var e: Element? = el
    var c: TypeElement?
    do {
        e = e?.enclosingElement
        c = e as? TypeElement
    } while (e != null && c == null)
    return c ?: el
}


val enclosing = enclosingType(elementOfCustomAnnotation)
if (enclosing.annotationMirrors.none { it.annotationType.toString() == "xxx.foo.bar.KeepInit" }) {
    throw Exception("must add @KeepInit to class: $enclosing")
}

Proguard 规则:

-keepclassmembers,allowobfuscation @xxx.foo.bar.KeepInit class * {
    <init>(...);
}

示例:

@KeepInit
class SomeClass(
    @SomeCustomAnnotation("id") private val id: String,
    @SomeCustomAnnotation("title") private val title: String
)