Hey everyone, so there's this "new" way to apply p...
# android
t
Hey everyone, so there's this "new" way to apply plugins in Gradle right?
Copy code
plugins {
    id "org.jetbrains.kotlin.android" version "$kotlin_version"
}
I love the fact that it doesn't require me to change the classpath in project-level build.gradle ...But, does it work for every plugin? If it does, how would I do this?
id "androidx.navigation.safeargs.kotlin" version "2.3.3"
(Plugin [id: 'androidx.navigation.safeargs.kotlin', version: '2.3.3'] was not found in any of the following sources:)
j
Have you add
google()
repo?
k
Don't cut off the list of sources - that way we can see which repo is missing
t
* Exception is: org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'androidx.navigation.safeargs.kotlin', version: '2.3.3'] 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 'androidx.navigation.safeargs.kotlinandroidx.navigation.safeargs.kotlin.gradle.plugin2.3.3')
@Javier Yes, google() and jcenter()
18:26 Gradle sync failed: Plugin [id: 'androidx.navigation.safeargs.kotlin', version: '2.3.3'] 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 'androidx.navigation.safeargs.kotlinandroidx.navigation.safeargs.kotlin.gradle.plugin2.3.3') Searched in the following repositories: Gradle Central Plugin Repository
j
copy all
Searched in the following repositories
t
It's just Gradle Central Plugin Repository
k
The plugin itself is
androidx.navigation:navigation-safe-args-gradle-plugin
1
j
if it is only appearing Gradle central plugin repository, then it is not searching in Google repo, so there is the problem
j
add to that build.gradle file, repositoreis { google() }
or add the root build.gradle file allProjects { repositories { google() } }
t
@Javier it's there in the project-level build.gradle... Both in
buildscript.repositories
and in
allprojects.repositories
j
check Kirill messages
k
You still need the right classpath in your
dependencies
block
t
Like this?
Copy code
id "androidx.navigation:navigation-safe-args-gradle-plugin" version "2.3.3"
t
Oh okay, so that one has to be in the classpath...
How is it different from those that don't need to be in classpath?
For example, I was able to remove
Copy code
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
when doing
Copy code
plugins {
    id "org.jetbrains.kotlin.android" version "$kotlin_version"
}
k
I'm not sure what you're asking. The plugin ID that you apply is not necessarily the same as the dependency ID which has that plugin
t
-> Now you can specify the version when applying the plugin; -> For some plugins, thats enough for it to work, and you can remove the
classpath
line Does it work for every plugin? How? Why not?
k
That would be better asked in a gradle forum
v
But I can answer it here if you like. 🙂
t
Please do haha
v
In your settings script you can define plugin repositories which are used to resolve plugins in
plugins { ... }
blocks, by default this is just
gradlePluginPortal()
. The repositories that you define in your build scripts or build script
buildscript
blocks are irrelevant for that. If you apply a plugin like with
plugins { id "androidx.navigation.safeargs.kotlin" version "2.3.3" }
, then it searches in the plugin repositories for an artifact at
androidx.navigation.safeargs.kotlin:androidx.navigation.safeargs.kotlin.gradle.plugin:2.3.3
(this is naming convention to be able to map id to artifact). This so-called marker artifact then has a dependency on the actual artifact that you add to
classpath
with the legacy way. Each plugin that is deployed to the Gradle plugin portal does have this marker artifact so you can simply use the new syntax (that also has other advantages, especially if you use Kotlin DSL build scripts). If you have plugins in other repositories like in
google()
there the marker artifacts might be missing, but you can compensate that in the setting script by a manual mapping. That would look something like
Copy code
pluginManagement {
    repositories {
        gradlePluginPortal()
        google()
    }

    resolutionStrategy {
        eachPlugin {
            when (target.id.id) {
                "androidx.navigation.safeargs.kotlin" ->
                    useModule("androidx.navigation:navigation-safe-args-gradle-plugin:${target.version}")
            }
        }
    }
}
This is in Kotlin DSL, but you should be able to translate it to Groovy DSL relatively easy. This setup allow for the advantages of the plugins block, while there are the marker artifacts missing.
🙌 1
t
Wow that was a fantastic answer, thank you so much! I had already tried adding google() to pluginManagement.repositories in settings.gradle but it still wouldn't find the plugin. Now it makes total sense, thank you again!
i
Filed https://issuetracker.google.com/179702637 for publishing a plugin marker artifact for Safe Args. Feel free to star that for updates (or comment there if I messed anything up or if there were more resources that would be helpful @Vampire)
👍 1
v
Looks fine to me from a quick look. While that is of course true for any plugin. That was just the one example that was asked about. :-)
You can even have multiple such marker artifacts pointing to the same code artifact if you have multiple plugins shipped in the same artifact.
i
That is indeed the case for Safe Args