gradle / kotlin-dsl-samples

Samples builds using the Gradle Kotlin DSL
https://gradle.org/kotlin/
Other
3.71k stars 434 forks source link

Android-related dependencies methods could not be resolved properly #770

Closed Archinamon closed 6 years ago

Archinamon commented 6 years ago

I'm migrating simple gradle project to kotlin-dsl and faced a problem: methods api, implementation, compileOnly, related to gradle 3.0.0+ api is not properly handling by kotlin-dsl even if AccessorsKt was generated well.

Screenshot illustrating my problem.

2018-03-21 18 49 02

As well, these methods not always even has been generated. As a workaround I can use add("api", "dependency:notation:...") or "api"("dependency:notation:...").

I have tried also launching gradle from command line with --recompile-scripts and no positive changes.

Expected Behavior

Proper handling android-related generated methods for DependencyHandlerScope.

Current Behavior

As I have been described above — these methods not always has been generated at all, or kts file simply not compiling nor from IDEA either from command line.

Context

Simple Android project generated by IDEA/Android Studio.

Steps to Reproduce (for bugs)

  1. Create new android-project
  2. Provide "com.android.tools.build:gradle:3.0.1" classpath dependency
  3. Try to use and api or implementation methods within dependencies closure block inside default app module.

Your Environment

eskatos commented 6 years ago

Hi @Archinamon, Thanks for the report. The availability of Kotlin extensions for dynamic parts of the Gradle model depend on how you apply your Gradle plugins. See https://github.com/gradle/kotlin-dsl/blob/master/doc/getting-started/Configuring-Plugins.md How are your plugins applied?

Archinamon commented 6 years ago

Hello! I'm using apply { plugin(<T>::class.java) } style. Does dsl stricts methods generating upon the way of declaring plugins? I'd found it a bit unobvoius =/

eskatos commented 6 years ago

For Kotlin extensions for dynamic parts of the Gradle model to be available your plugins must be either already applied (e.g. by a parent project) or applied using the plugins {} block. That's a known limitation. Please read the documentation linked above ☝️

Archinamon commented 6 years ago

Great, thanks!

Does it planning to be fixed? I'm glad you have point me my mistake, but I'd prefer using static types for plugin apply block. Which is not available for plugins {} block.

eskatos commented 6 years ago

You're welcome!

We can't fix this, apply {} is imperative, and could very well be nested in conditionals and such, so we just can't know what plugins would be applied before compiling/evaluating the scripts. The plugins {} block on the other hand is declarative, and allow us to provide those extensions. Some more background is available in Gradle's documentation: https://docs.gradle.org/current/userguide/plugins.html