22

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.

John D.
  • 2,521
  • 3
  • 24
  • 45
  • 2
    Could you please post a sample that is causing trouble? I suspect it may be related to annotation processing so any additional information about libraries used (i.e. ButterKnife or Dagger) could come handy – miensol Jan 19 '16 at 06:37
  • 2
    Which version of Kotlin are you using? – yole Jan 19 '16 at 08:06
  • According to Android Studio, I am using Version: 1.0.0-beta-4584-IJ141-13 – John D. Jan 19 '16 at 18:25
  • 1
    I only get this when using the `inner` modifier on a class. However, not using it is a rather large inconvenience. I hope you find a solution (I'll be looking too). – bclymer Jan 19 '16 at 22:49
  • have you considered the possibility you don't have proguard set up? "RuntimeInvisibleParameterAnnotations Specifies the annotations that are visible at compile-time, for method parameters. Compilers and annotation processors may use these annotations." from http://proguard.sourceforge.net/manual/attributes.html – activedecay Jan 22 '16 at 02:10
  • I never really resolved the problem at hand, but I got around it by making Baz a static inner class. I think ProGuard may have been causing the problem, but refactoring worked. – John D. Jan 25 '16 at 18:20
  • I got the same problem while trying to convert Java project to Kotlin here: https://github.com/gfx/Android-Helium/pull/42 – FUJI Goro Mar 01 '16 at 13:09

1 Answers1

2

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

josedlujan
  • 5,357
  • 2
  • 27
  • 49