5

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

Alexei
  • 14,350
  • 37
  • 121
  • 240
  • 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. – Richard Le Mesurier Aug 18 '22 at 11:21

2 Answers2

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
Willi Mentzel
  • 27,862
  • 20
  • 113
  • 121
0

In your app build.gradle add this:

implementation project(':common')
Saeed Entezari
  • 3,685
  • 2
  • 19
  • 40