robertoestivill
08/23/2019, 2:58 PMMarc Knaup
08/23/2019, 3:17 PMapply false
.
Only if a plugin is in the plugins
block synthetic accessors like android {}
will get created.robertoestivill
08/23/2019, 3:50 PMplugins {
id(Plugin.androidApp) apply false
}
right under the top level build.gradle
buildscript block
And error message in child module is slightly different
app/build.gradle.kts' line: 4
* What went wrong:
Plugin [id: 'com.android.application'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
Marc Knaup
08/23/2019, 3:51 PMMarc Knaup
08/23/2019, 3:51 PMrobertoestivill
08/23/2019, 3:54 PMMarc Knaup
08/23/2019, 3:55 PMapply false
, you merely load them.
Unless the Android plugin is loaded Gradle doesn’t know that it should create plugin-specific Kotlin accessors like android { … }
.robertoestivill
08/23/2019, 3:56 PMMarc Knaup
08/23/2019, 3:58 PMrobertoestivill
08/23/2019, 4:01 PMapp/
. So I'm wondering if I missing something else here 🤔Marc Knaup
08/23/2019, 4:02 PMMarc Knaup
08/23/2019, 4:03 PMandroid {}
in the parent with apply false
Marc Knaup
08/23/2019, 4:04 PMrobertoestivill
08/23/2019, 4:04 PMmbonnin
08/23/2019, 6:57 PMmbonnin
08/23/2019, 7:10 PMproject.android
in the root gradle file and not just android
?efemoney
08/25/2019, 8:30 AMrobertoestivill
08/27/2019, 9:18 AMbuild.gradle.kts
not finding the com.android.application
plugin for it to use, even though it's in the buildscript
block
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath(Classpath.androidGradle)
classpath(Classpath.kotlinGradle)
}
}
plugins {
id(Plugin.androidApp) version "3.4.2" apply false
id(Plugin.kotlinAndroid) version "1.3.41" apply false
}
project-root/build.gradle.kts' line: 22
* What went wrong:
Plugin [id: 'com.android.application', version: '3.4.2', apply: false] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.android.application:com.android.application.gradle.plugin:3.4.2')
Searched in the following repositories:
Gradle Central Plugin Repository
efemoney
08/27/2019, 12:25 PMplugins
block, it is evaluated first (in my opinion, even before buildscript block). As you can see Gradle tries to resolve the plugins from the core (/bundled) plugins and then from the gradle plugin repository. But the android (and kotlin) gradle plugin(s) is(are) not published to the gradle plugin portal 🙂. To aid Gradle in resolving the plugin you will need to add this block to the project-level settings.gradle(.kts)
file.
pluginManagement {
repositories {
google()
mavenCentral()
gradlePluginPortal() //
}
resolutionStrategy {
eachPlugin {
if (requested.id.id.startsWith("com.android.")) useModule(Plugin.androidApp)
// ... add declaration for kotlin gradle plugin too
}
}
}
Marc Knaup
08/27/2019, 12:33 PMrobertoestivill
08/27/2019, 1:18 PMrobertoestivill
08/27/2019, 1:20 PMBuild file '/application/build.gradle.kts' line: 4
An exception occurred applying plugin request [id: 'com.android.application', artifact: 'com.android.tools.build:gradle:3.4.2']
> Failed to apply plugin [class 'org.gradle.api.plugins.BasePlugin']
> Extension of type 'BaseExtension' does not exist. Currently registered extension types: [ExtraPropertiesExtension, DefaultArtifactPublicationSet]
Marc Knaup
08/27/2019, 2:19 PMbuildSrc
to load the Android Gradle Plugin, not buildscript
. No idea if there's a difference.efemoney
08/27/2019, 2:21 PMBasePlugin
class you need to check for is the android one 😄 and it has a generic type parameter (before agp version 3.6.0-alpha06) so
import com.android.build.gradle.BaseExtension
import com.android.build.gradle.BasePlugin
and then plugins.withType<BasePlugin<*>> { ... }
efemoney
08/27/2019, 2:21 PMrobertoestivill
08/27/2019, 3:38 PMapplication/ -> android application module (depends on all features)
buildSrc/ -> kotlin dsl enabled
feature-a/ -> android library
feature-b/ -> android library
feature-c/ -> android library
...
build.gradle -> parent gradle
settings.gradle
buildSrc
was created to expose kotlin constants with the dependencies coordinates, versions, android configuration, etc. No logic was included in buildSrc, just constants that we wanted to define in a single place.
All gradle files were written in Groovy until I started working on this.
The tricky part that triggered this conversation and the StackOverflow issue is in the parent build.gradle
.
This script configured all the children modules common android { }
blocks in a single place, allowing children modules to just apply plugins and dependencies, and have a single point of configuration for all ocurrences of the android plugin.
The current state of things was not intentionally decided or designed so I understand your concerns about how is this working.
However, this is more the result of the evolution of the codebase coming from people like me, not really experts on gradle, other than something we created from scratch or consciously decided on.
Now, I have migrated all the children modules build scripts to kotlin and that works fine as long as I don't migrate the parent groovy build.gradle, but I am struggling to replicate the behavior that the parent script had on it's equivalent kotlin version.
Sorry for the confusion, and thanks for the help so far 🙂efemoney
08/27/2019, 4:09 PMrobertoestivill
08/27/2019, 7:00 PMefemoney
08/27/2019, 7:05 PMrobertoestivill
08/27/2019, 7:07 PMgildor
08/28/2019, 12:37 AMAlso, this doesn't really solve the problem of generated accessorsbuildSrc also can help with it, if you move configuration to precompiled script plugin