【问题标题】:Why I'm facing Parcelable issues just when minify is true?为什么当 minify 为真时我面临 Parcelable 问题?
【发布时间】:2019-07-18 15:21:18
【问题描述】:

我的应用在调试版本变体上运行良好,但是当我发布时,唯一的区别是混淆,应用无法正常运行

 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.

【问题讨论】:

    标签: android proguard parcelable


    【解决方案1】:

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

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

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

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

    它使所有实现 Parcelable 的类,使其 CREATOR 文件保持非混淆。见这里:Do I need to 'keep' Parcelable in proguard rules while obfuscating

    【讨论】:

    • 感谢 marcinj 帮助我!我必须为我的所有对象设置此规则吗?
    • 很好,非常感谢您的帮助,谢谢!!
    【解决方案2】:

    虽然文档说您必须将这些行放入 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 进行注释。

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

    另外,不要忘记在你的 proguard 文件中添加-keep class com.example.Patient.** { *; }

    【讨论】:

    • 感谢 GreenROBO 帮助我!我必须为我的所有对象设置此规则吗? pron annotationProcessor 到 apt 有什么区别?
    • @GreenTOBO,我的意思是,我所有实现 Parcelable 的类都应该有这样的规则?
    • @Shermano 请参阅marcinj's answer below
    • @Shermano 如果对您有帮助,您可以投票并接受。所以以后会对其他人有所帮助。
    • 是的,我接受了,我接受了@macninj 的回答,但很抱歉。希望我能同时接受,你的回答也很棒
    猜你喜欢
    • 1970-01-01
    • 2022-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 2016-01-09
    • 1970-01-01
    相关资源
    最近更新 更多