7

Mirgating from Java to Kotlin I try to use static function with Data Binding:

  <data>
    <import type="com.package.domain.tools.helper.StringValidator"/>
    ...
</data>

Then I call function hideNumber:

 <com.hastee.pay.ui.view.Text
        ...
        android:text='@{StringValidator.hideNumber(account.number)}'
        app:layout_constraintRight_toRightOf="@+id/number"
        app:layout_constraintTop_toBottomOf="@+id/number" />

Using databinding here causes error:

   [kapt] An exception occurred: 
android.databinding.tool.util.LoggedErrorException: Found data binding 
errors.
 ****/ data binding error ****msg:cannot find method 
 hideNumber(java.lang.String) in class 
 com.package.domain.tools.helper.StringValidator....

Here's this object:

 object StringValidator {
  ...
fun hideNumber(number: String): String {
    return "****" + number.substring(number.length - 4)
}
}

How can I reach this function using Kotlin and Data Binding?

1
  • 5
    try adding an @JvmStatic to hideNumber Commented May 24, 2018 at 11:58

1 Answer 1

18

The data binding compiler is looking for a static method.

Since a named object alone is not enough to make all methods inside that object static, you need an additional @JvmStatic annotation on your hideNumber-method:

@JvmStatic
fun hideNumber(number: String): String {
    return "****" + number.substring(number.length - 4)
}

see also: https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#static-methods

Sign up to request clarification or add additional context in comments.

1 Comment

Cool! Thanks for explanation!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.