Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set gradle.ext in settings.gradle.kts with Gradle Kotlin DSL

Here's code snippet from google/exoplayer - which is written in Groovy buildscript.

// settings.gradle
gradle.ext.exoplayerRoot = 'path/to/exoplayer'
gradle.ext.exoplayerModulePrefix = 'exoplayer-'
apply from: new File(gradle.ext.exoplayerRoot, 'core_settings.gradle')

How can I achieve this using Kotlin DSL?

I couldn't find any useful resources or documents. Any help would be appreciated.

like image 527
Tura Avatar asked Aug 22 '19 07:08

Tura


1 Answers

Found a solution after digging a while.

In Groovy, there's dynamic implementation of traits - So even if class A does not implement interface B in the class definition, it may implement it later in the future.

I didn't see Gradle internals so I can't properly explain this, but it seems to work. Hope this helps.

if (gradle is ExtensionAware) {
    val extension = gradle as ExtensionAware
    extension.extra["exoplayerRoot"] = "path/to/exoplayer"
    extension.extra["exoplayerModulePrefix"] = "exoplayer-"
    apply(from = File(extension.extra["exoplayerRoot"].toString(), "core_settings.gradle"))
}

For those who uses ExoPlayer, I created an issue for requesting some documents for Kotlin DSL users.

like image 161
Tura Avatar answered Oct 01 '22 02:10

Tura