How can I detect if a gradle project is of an Application or Library?

Viewed 676

I'm writing am Android Studio Gradle plugin and one of the things I need to do is iterate over the projects and detect if they are of type Android Application or library (or a non android project like general java libs).

I tried to use the following code but it doesn't work properly (detects the application but not libraries):

import com.android.build.gradle.AppPlugin
import com.android.build.gradle.LibraryPlugin

...

public void getVariants(){
...
    def hasApp = p.plugins.withType(AppPlugin)
    def hasLib = p.plugins.withType(LibraryPlugin) 
    
    ...
}

Are there any other proper ways in Groovy script to detect of a project is of type library ?

2 Answers
project.afterEvaluate {
    plugins.withId('com.android.application') {
        println("==> module application")
    }
    plugins.withId('com.android.library') {
        println("==> module library")
    }
    plugins.withId('java') {
        println("==> module pure java")
    }
}

I have a flavor.gradle defined which is getting imported into application & library modules. I need to add applicationSuffixId only for application flavor. So I done it like this.

def isApplication = plugins.hasPlugin("com.android.application")

android {

    flavorDimensions "version"

    productFlavors {
        dev {
            dimension "version"
            if (isApplication) {
                applicationIdSuffix ".dev"
            }
        }
        qa {
            dimension "version"
            if (isApplication) {
                applicationIdSuffix ".qa"
            }
        }
        beta {
            dimension "version"
            if (isApplication) {
                applicationIdSuffix ".beta"
            }
        }
        prod {
            dimension "version"
        }
    }
}
Related