简体   繁体   English

使用ProGuard的Kotlin抽象泛型

[英]Kotlin abstract generic with ProGuard

I have the following base class: 我有以下基类:

abstract class BaseFragment<T : BaseViewModel> : Fragment(), JobHolder {
    protected lateinit var viewModel: T
        private set

    protected fun provideViewModel(type: Class<T>) {
        viewModel = ViewModelProviders.of(this).get(type)
    }
}

and the following class: 和以下类:

class SubjectEditor : BaseFragment<SubjectEditorViewModel>() {
    override fun onAttach(context: Context?) {
        super.onAttach(context)
        provideViewModel(SubjectEditorViewModel::class.java)
    }
}

But ProGuard is complaining: 但是ProGuard抱怨:

Warning: cz.x.ui.subjects.SubjectEditor: can't find referenced method 'void setViewModel(cz.x.ui.BaseViewModel)' in program class cz.x.ui.subjects.SubjectEditor

I tried some -keep rules for ProGuard, but nothing worked. 我尝试了一些-keep规则ProGuard的,但毫无效果。 What's the correct solution? 什么是正确的解决方案?

I guess you want it not to be obfuscated, so in that case you should keep the methods in the abstract class: 我想您希望不要混淆它,因此在这种情况下,您应该将方法保留在抽象类中:

-keep public class {path to your class}.BaseFragment{
   private <methods>;
}

There's a really usefull ProguardGuide in this link After reading it you should be more confident on which rules you should use. 此链接中有一个非常有用的ProguardGuide。阅读它后,您应该对应该使用哪些规则更有信心。

I hope it helps, otherwise let me know! 希望对您有所帮助,否则请通知我!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM