Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin - Intermittent "bad class file" error

Starting today, when I attempt to build my Kotlin Android app, I am met with the following error in my Gradle build:

Error:cannot access Baz
bad class file: /Users/me/projects/site/android/app/build/tmp/kapt/debug/classFileStubs/com/company/foo/Bar$Baz.class
bad RuntimeInvisibleParameterAnnotations attribute: Baz(FragmentManager)
Please remove or make sure it appears in the correct subdirectory of the classpath.

It is pointing to an inner class Baz which extends android.support.v4.app.FragmentStatePagerAdapter. I am able to temporarily get around the error by commenting out the class, and any references to it in the outer class, and rebuilding. The error goes away, but obviously the class no longer exists so other things break at runtime. Then, if I uncomment it and build, it will work for a few builds. Then the error comes back. Rinse and repeat. I think closing the Genymotion emulator may trigger it.

Anyone else run into this, or have any ideas?

Here is the offending code:

class Bar : Fragment() {

    @Inject
    lateinit var api:ApiRequester
    var data : ArrayList<Data> = ArrayList()

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        //[...] view creation code
        //[...] data population code
        viewPager.adapter = Baz(childFragmentManager)
        //[...] more view creation code
    }

    inner class Baz(fm:FragmentManager) : FragmentStatePagerAdapter(fm) {
        override fun getCount(): Int {
            return data.count()
        }

        override fun getItem(position: Int): Fragment? {
            var jf = FooFragment()
            var bundle = Bundle()
            bundle.putParcelable("data", data[position])
            jf.arguments = bundle
            return jf
        }
    }

EDIT: Apologies, Baz extends FragmentStatePagerAdapter, not Fragment as I initially stated. I am using Dagger2, which could totally have an effect here.

like image 252
John D. Avatar asked Jan 19 '16 01:01

John D.


1 Answers

I have two options to help you.

1- try upgrading proguard. Use THIS

2- Use Java 7, change your path. export JAVA_HOME=/usr/libexec/java_home -v 1.7

like image 182
josedlujan Avatar answered Nov 06 '22 09:11

josedlujan