为什么我在 minify 为真时遇到 Parcelable 问题?

Why I'm facing Parcelable issues just when minify is true?

我的应用程序在调试构建变体上运行良好,但是当我发布时,唯一的区别是混淆,应用程序运行不佳

 fun upsertPatient(patient: Patient, onCompletion: (Patient) -> Unit) {
        val px = PatSecHelper.patToN(patient, SecHelper.genKey())
        if (px != null) {
            val subscription = Single.fromCallable {
                val id = patientDao?.insertPatient(px)
                px.id = id
                px
            }
                    ?.subscribeOn(Schedulers.io())
                    ?.subscribe({
                        onCompletion(it!!)
                    }, {
                        BleLogHelper.writeError("Error inserting patient into database", it)
                    })
            subscriptions.add(subscription)
        }
    }

在调试模式下工作正常,但在发布时会在上述方法上引发异常。

Unable to find generated Parcelable class for io.b4c.myapp.a.f, verify that your class is configured properly and that the Parcelable class io.b4c.myapp.a.f$$Parcelable is generated by Parceler.

当 minify 为真时,还会使用重写名称的混淆器。要保留原始版本,请将其添加到 proguard.cfg 个文件中,例如:

-keep class com.example.Patient.** { *; }

您还可以使用以下规则使其更容易:

-keepnames class * implements android.os.Parcelable {
    public static final ** CREATOR;
}

它使所有 类 实现 Parcelable,使其 CREATOR 文件保持不混淆。看这里:

尽管文档说您必须将这些行添加到 Gradle:

compile 'org.parceler:parceler-api:1.1.6'
annotationProcessor 'org.parceler:parceler:1.1.6'

改为:

compile 'org.parceler:parceler-api:1.1.6'
kapt 'org.parceler:parceler:1.1.6'

确保您要使用的所有文件都带有@Parcel 注释。

我有 class First 和 class Second 变量,我忘了注释 class Second。这就是为什么从 annotationProcessor 更改为 apt 会给我一个构建错误。

此外,不要忘记在您的混淆文件中添加 -keep class com.example.Patient.** { *; }