Proguard, как сохранить конструктору, чтобы его параметры имели некоторые аннотации? - PullRequest
0 голосов
/ 05 мая 2018

Если у параметров есть мои пользовательские аннотации, тогда конструктор должен быть сохранен.

1 Ответ

0 голосов
/ 22 января 2019

Ну, решил сам, используя процессор аннотаций.

Создайте аннотацию, которая применяется к классу, который должен содержать конструктор:

@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
)
...