question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

InflationInject throws error when a custom view is a kotlin class with @JvmOverloads marked constructor

See original GitHub issue

I defined this custom class with standard @JvmOverloads annotations to it’s constructor:

class CustomView @InflationInject @JvmOverloads constructor(
    @Inflated context: Context,
    @Inflated attrs: AttributeSet? = null,
    @Inflated defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr)

But when compiling the annotation processor throws this error: /android/ui/custom_views/CustomView.java:11: error: Multiple @InflationInject-annotated constructors found.

What is the proper way to do this in Kotlin? I can’t find any samples, so creating an issue here. Any help is appreciated, thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
roshanthejokercommented, Jul 19, 2021

This article by Dan Lew cleared things up for me a bit regarding view constructors: https://blog.danlew.net/2016/07/19/a-deep-dive-into-android-view-constructors/. @JakeWharton I suppose we can close this issue. Thanks for the help.

0reactions
Benny6731commented, Jun 10, 2022

I defined this custom class with standard @JvmOverloads annotations to it’s constructor:

class CustomView @InflationInject @JvmOverloads constructor(
    @Inflated context: Context,
    @Inflated attrs: AttributeSet? = null,
    @Inflated defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr)

But when compiling the annotation processor throws this error: /android/ui/custom_views/CustomView.java:11: error: Multiple @InflationInject-annotated constructors found.

What is the proper way to do this in Kotlin? I can’t find any samples, so creating an issue here. Any help is appreciated, thanks.

,

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom View constructor in Android 4.4 crashes on Kotlin ...
Best way is to have your class this way. class MyView : LinearLayout { @JvmOverloads constructor(context: Context, attrs: AttributeSet?
Read more >
Misconception about Kotlin @JvmOverloads for Android View ...
The root cause is that @JvmOverloads is a trick from Kotlin compiler that generates overload constructors, instead of the override constructors.
Read more >
@JvmOverloads for Android Views - zsmb.co
The @JvmOverloads annotation is a convenience feature in Kotlin for ... class CustomView : View { constructor(context: Context) ...
Read more >
Do not always trust @JvmOverloads | by Mateusz Młodawski
So always in our custom class, we will call the three-param constructor from TextInputEditText. Step 2: Understanding View's constructors. Now ...
Read more >
Android Custom View Tutorial - RayWenderlich.com
Create an Android Custom View in Kotlin and learn how to draw shapes on the canvas, make views responsive, create new XML attributes, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found