Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin compiler: Data binding error, cannot find method

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?

like image 444
Igor Skryl Avatar asked May 24 '18 11:05

Igor Skryl


1 Answers

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

like image 58
Lovis Avatar answered Sep 20 '22 10:09

Lovis