Kristy Welsh
Kristy Welsh

Reputation: 8530

Kotlin extension property is recognized in some app modules and not others

I'm doing an application extension for my modularized android app (it has 4 modules). I'm using the following utility code to initialize Application extensions:

class ApplicationLazy<out T>(private val initializer: Application.() -> T) : ReadOnlyProperty<Application, T> {
   private var value: T? = null

   override fun getValue(thisRef: Application, property: KProperty<*>): T =
     (value ?: initializer(thisRef).also {
      value = it
   })
 }

 fun <T : Any> appLazy(initializer: Application.() -> T) = ApplicationLazy(initializer)

And in one module (call it core), I'm defining database as such:

val Application.db by appLazy {
   Database(
     AndroidSqliteDriver(Database.Schema, this, "luminary.db"),
     HomeItemDto.Adapter(SectionIdColumnAdapter, EnumColumnAdapter(), EnumColumnAdapter()),
     HomeSectionDto.Adapter(SectionIdColumnAdapter, EnumColumnAdapter()),
     MediaItem.Adapter(MediaIdColumnAdapter, LocalDateTimeColumnAdapter, LocalDateTimeColumnAdapter)
   )
}

I can "see" that from anywhere in my four modules, not just the "core" module. (Doing an Application.db) shows up in my list of choices in Android studio. However, in a different module (call it audioplayer) I'm defining an Exoplayer DataManager as such:

val Application.downloadManager: DownloadManager by appLazy {
  val downloadIndex = DefaultDownloadIndex(databaseProvider)
  upgradeActionFile(DOWNLOAD_ACTION_FILE, downloadIndex, false)
  upgradeActionFile(DOWNLOAD_TRACKER_ACTION_FILE, downloadIndex, true)
  val downloaderConstructorHelper = DownloaderConstructorHelper(downloadCache, dataSource(this, 
  SwaggerApiClient.userAgent))
  DownloadManager(this, downloadIndex, DefaultDownloaderFactory(downloaderConstructorHelper)).apply {
    requirements = Requirements(Requirements.NETWORK_UNMETERED)
  }
}

Calling application.downloadManager in another module when I compile, the compiler complains and says it is an unresolved reference. Why is this working when defined in one module and not the other?

EDIT Here are the dependencies for the module that is calling this extension.

dependencies { //core
api project(":network")
}

dependencies { //app
}

dependencies { //network
}

dependencies { //audioplayer
  implementation project(':core')
}

Upvotes: 0

Views: 406

Answers (1)

Giorgio Antonioli
Giorgio Antonioli

Reputation: 16214

You have to add the module containing the extension as a dependency of the module which wants to use it.

So, imagining the extension application.downloadManager is in the module :audioplayer, you should add the dependency with :audioplayer in the modules which need that extension.

e.g.

dependencies {
    // Or "api project" if you want to make that extension accessible from the modules which depend from this module.
    implementation project(":audioplayer")
    ...
}

Upvotes: 1

Related Questions