waseefakhtar
waseefakhtar

Reputation: 1423

Error: The 'java' plugin has been applied, but it is not compatible with the Android plugins

I'm trying to create a new library that will be shared across other modules in the projects. Unfortunately, though when I try to create one, I get the mentioned error before the gradle even builds:

The 'java' plugin has been applied, but it is not compatible with the Android plugins.

Here's my gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

I've tried reading several similar threads where people recommends to remove java plugin, but I don't see any in my new library. However I do see a java plugin in my root gradle:

configure(allprojects) {
  println "applying java plugin to $project"
  apply plugin: 'java-library'

  sourceSets {
    main {
      java {
        srcDirs = ["src"]
      }
      resources {
        srcDirs = ["src/resources"]
      }
    }
  }

  sourceCompatibility = 1.7
  targetCompatibility = 1.7
}

What do you think might be the issue?

Upvotes: 2

Views: 5540

Answers (2)

stkent
stkent

Reputation: 20128

This code:

configure(allprojects) {
  println "applying java plugin to $project"
  apply plugin: 'java-library'

  //...
}

declares that the Java library plugin should be applied to every Gradle project in your codebase. Note that the java plugins (application, library) are generally incompatible with the android plugins (application, library). You can think of the latter as being heavily modified versions of the former.

This incompatibility causes the error you see.

The best resolution depends on the purposes of your codebase as a whole and the Gradle projects within it. The least invasive options are likely:

  1. Remove apply plugin: 'java-library' and related configuration from the allprojects configuration and instead manually apply the plugin in each Gradle project that needs it;
  2. Add a special case in your allprojects configuration so that the java-library plugin is applied to all projects except the new one.

To expand on the second case, you could do something like:

configure(allprojects) {
  if (getPath() != "absolute/path/to/new/project") {
    println "applying java plugin to $project"
    apply plugin: 'java-library'

    // ...
  }
}

I used getPath here since getName is not guaranteed to be unique in general, but if it is in your particular case then that would be a friendlier conditional to write and read.

Upvotes: 1

waseefakhtar
waseefakhtar

Reputation: 1423

As @stkent mentions, we just have to get rid of either the java plugin, or exclude the library from configuration of allprojects in the root gradle. How I worked my way around it was by adding the following to the root gradle where the java plugin was applied:

configure(allprojects - project(':NewLibraryName')) {
    println "applying java plugin to $project"
    apply plugin: 'java-library'

    //...
}

Upvotes: 1

Related Questions