Skip to content

Instantly share code, notes, and snippets.

@tir38
Last active May 11, 2019 07:36
Show Gist options
  • Save tir38/7fe07ca1af9394a2bcb06e31038ea314 to your computer and use it in GitHub Desktop.
Save tir38/7fe07ca1af9394a2bcb06e31038ea314 to your computer and use it in GitHub Desktop.
Creating plugin (located in buildSrc/src/main/kotlin/com/example)

Watching this talk and I'm trying to mimic AndroidXPlugin. However I can't import all of the classes I need. Why is Android Studio saying android is unresolved reference?

I can reference com.android.build.gradle.* package in app/build.gradle:


import com.android.build.gradle.LibraryExtension // :eyes: this works just fine

class MyPlugin implements Plugin<Project> {
    @Override
    void apply(Project target) {
        project.plugins.all {
            LibraryExtension // blah blah
        }
    }
}

but not in buildSrc/build.gradle nor buildSrc/src/main/kotlin/com/example (see attached MyPlugin.kt)

package com.example
import org.gradle.api.Plugin
import org.gradle.api.Project
// :eyes: why is A/S saying 'android' is unresolved reference??
import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryExtension
import com.android.build.gradle.LibraryPlugin
class MyPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.plugins.all {
when (it) {
is LibraryPlugin -> {
val extension = project.extensions.getByType(LibraryExtension::class.java)
extension.configureLibrary()
}
is AppPlugin -> {
// ...
}
}
}
}
private fun LibraryExtension.configureLibrary() {
// start off by extracting just common lint settings
lintOptions {
...
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment