Why extension function is not visible in another module?
Asked Answered
K

2

6

My Android project has two modules:

app
common

In settings.gradle:

rootProject.name='My project'
include ':app'
include ':common'

In my build.gradle:

implementation project(':common')

In common package I has StringUtil.kt with the next extension function:

fun String.isEmailValid(): Boolean {
    return !TextUtils.isEmpty(this) && android.util.Patterns.EMAIL_ADDRESS.matcher(this).matches()
}

And in this class I can use extension function like this:

val str = ""
str.isEmailValid()

But in app module I has class

class RegistrationViewModel(application: Application) : AndroidViewModel(application) {

  fun doClickRegistration(email: String?, password: String?, retypePassword: String?) {
        val str = ""
        str.isEmailValid()
    }
}

But now I get compile error:

Unresolved reference: isEmailValid

Kreg answered 23/1, 2020 at 9:10 Comment(1)
You ever find out why this was the case, Alexei? I see the official answer explains that what you saw should not happen, and I have just had this exact problem on my side.Dimorph
C
7

If you do not specify any visibility modifier, public is used by default, which means that your declarations will be visible everywhere; (Source)

Since you didn't add any visibility modifier to isEmailValid it is regarded as public.

Please note that extension functions have to be imported.

import com.your.package.path.isEmailValid
Cubital answered 23/1, 2020 at 9:18 Comment(0)
B
0

In your app build.gradle add this:

implementation project(':common')
Baffle answered 23/1, 2020 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.