Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradle project sync is failing with Cobertura plugin in Android project #119

Open
thcuster opened this issue Sep 8, 2016 · 9 comments
Open

Comments

@thcuster
Copy link

thcuster commented Sep 8, 2016

I am trying to use the Cobertura plugin for creating coverage reports on an Android project. I followed the usage guide but Gradle fails to sync after I apply the cobertura plugin. Here is what my build.gradle looks like:

plugins {
    id 'net.saliman.cobertura' version '2.3.2'
}

apply plugin: 'com.android.application'
apply plugin: 'net.saliman.cobertura'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:3.1.2'
    testCompile 'org.khronos:opengl-api:gl1.1-android-2.1_r1'
    testCompile 'org.mockito:mockito-core:1.10.19'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.google.code.gson:gson:2.6.2'
}

Gradle fails with the the error message: "Error, the 'java' plugin has been applied, but it is not compatible with the Android plugins."

I am using Gradle 2.14.1

@stevesaliman
Copy link
Owner

I'm not sure what might be happening here. You are correctly applying the android plugin first, which should prevent the cobertura plugin from applying the java plugin.

I just released version 2.4.0 of the plugin, and it includes some additional messaging. When you run the Gradle build with the --info flag, does it say it is applying the plugin to a Java project, or an Android project?

@thcuster
Copy link
Author

thcuster commented Jan 3, 2017

Here is the full output of the Gradle build with the --info flag. This is using version 2.4.0:

./gradlew assembleDebug --info
Starting Build
Settings evaluated using settings file '/Users/thcuster/Projects/sw/android_app/settings.gradle'.
Projects loaded. Root project using build file '/Users/thcuster/Projects/sw/android_app/build.gradle'.
Included projects: [root project 'android_app', project ':app']
Evaluating root project 'android_app' using build file '/Users/thcuster/Projects/sw/android_app/build.gradle'.
Evaluating project ':app' using build file '/Users/thcuster/Projects/sw/android_app/app/build.gradle'.
Applying cobertura plugin to app
Creating cobertura extension for java project app
Creating configuration compile
Creating configuration apk
Creating configuration provided
Creating configuration wearApp
Creating configuration annotationProcessor
Creating configuration androidTestCompile
Creating configuration androidTestApk
Creating configuration androidTestProvided
Creating configuration androidTestWearApp
Creating configuration androidTestAnnotationProcessor
Creating configuration testCompile
Creating configuration testApk
Creating configuration testProvided
Creating configuration testWearApp
Creating configuration testAnnotationProcessor
Creating configuration debugCompile
Creating configuration debugApk
Creating configuration debugProvided
Creating configuration debugWearApp
Creating configuration debugAnnotationProcessor
Creating configuration testDebugCompile
Creating configuration testDebugApk
Creating configuration testDebugProvided
Creating configuration testDebugWearApp
Creating configuration testDebugAnnotationProcessor
Creating configuration releaseCompile
Creating configuration releaseApk
Creating configuration releaseProvided
Creating configuration releaseWearApp
Creating configuration releaseAnnotationProcessor
Creating configuration testReleaseCompile
Creating configuration testReleaseApk
Creating configuration testReleaseProvided
Creating configuration testReleaseWearApp
Creating configuration testReleaseAnnotationProcessor
Making :app:instrument depend on :app:classes
Making :app:cobertura depend on :app:test

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> The 'java' plugin has been applied, but it is not compatible with the Android plugins.

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Total time: 3.56 secs
Stopped 0 compiler daemon(s).

Looks like it's applying to a Java project based on the line "Creating cobertura extension for java project app"

Output of build with --stacktrace option:

./gradlew assembleDebug --stacktrace
FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> The 'java' plugin has been applied, but it is not compatible with the Android plugins.

* Try:
Run with --info or --debug option to get more log output.

* Exception is:
org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.
        at org.gradle.configuration.project.LifecycleProjectEvaluator.addConfigurationFailure(LifecycleProjectEvaluator.java:79)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:74)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.evaluate(LifecycleProjectEvaluator.java:61)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:540)
        at org.gradle.api.internal.project.AbstractProject.evaluate(AbstractProject.java:93)
        at org.gradle.execution.TaskPathProjectEvaluator.configureHierarchy(TaskPathProjectEvaluator.java:47)
        at org.gradle.configuration.DefaultBuildConfigurer.configure(DefaultBuildConfigurer.java:35)
        at org.gradle.initialization.DefaultGradleLauncher$2.run(DefaultGradleLauncher.java:124)
        at org.gradle.internal.Factories$1.create(Factories.java:22)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:91)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:53)
        at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:121)
        at org.gradle.initialization.DefaultGradleLauncher.access$200(DefaultGradleLauncher.java:32)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:98)
        at org.gradle.initialization.DefaultGradleLauncher$1.create(DefaultGradleLauncher.java:92)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:91)
        at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:63)
        at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:92)
        at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:83)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter$DefaultBuildController.run(InProcessBuildActionExecuter.java:99)
        at org.gradle.tooling.internal.provider.ExecuteBuildActionRunner.run(ExecuteBuildActionRunner.java:28)
        at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:48)
        at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:30)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:81)
        at org.gradle.launcher.exec.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:46)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:51)
        at org.gradle.launcher.exec.DaemonUsageSuggestingBuildActionExecuter.execute(DaemonUsageSuggestingBuildActionExecuter.java:28)
        at org.gradle.launcher.cli.RunBuildAction.run(RunBuildAction.java:43)
        at org.gradle.internal.Actions$RunnableActionAdapter.execute(Actions.java:173)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:239)
        at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(CommandLineActionFactory.java:212)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:35)
        at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(JavaRuntimeValidationAction.java:24)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:33)
        at org.gradle.launcher.cli.ExceptionReportingAction.execute(ExceptionReportingAction.java:22)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:205)
        at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(CommandLineActionFactory.java:169)
        at org.gradle.launcher.Main.doAction(Main.java:33)
        at org.gradle.launcher.bootstrap.EntryPoint.run(EntryPoint.java:45)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(ProcessBootstrap.java:55)
        at org.gradle.launcher.bootstrap.ProcessBootstrap.run(ProcessBootstrap.java:36)
        at org.gradle.launcher.GradleMain.main(GradleMain.java:23)
        at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:30)
        at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
        at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
Caused by: com.android.build.gradle.internal.BadPluginException: The 'java' plugin has been applied, but it is not compatible with the Android plugins.
        at com.android.build.gradle.BasePlugin.createAndroidTasks(BasePlugin.java:653)
        at com.android.build.gradle.BasePlugin$10.call(BasePlugin.java:608)
        at com.android.build.gradle.BasePlugin$10.call(BasePlugin.java:605)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:156)
        at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:120)
        at com.android.build.gradle.BasePlugin.lambda$createTasks$1(BasePlugin.java:603)
        at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:93)
        at org.gradle.internal.event.BroadcastDispatch$ActionInvocationHandler.dispatch(BroadcastDispatch.java:82)
        at org.gradle.internal.event.AbstractBroadcastDispatch.dispatch(AbstractBroadcastDispatch.java:44)
        at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:79)
        at org.gradle.internal.event.BroadcastDispatch.dispatch(BroadcastDispatch.java:30)
        at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
        at com.sun.proxy.$Proxy11.afterEvaluate(Unknown Source)
        at org.gradle.configuration.project.LifecycleProjectEvaluator.notifyAfterEvaluate(LifecycleProjectEvaluator.java:67)
        ... 44 more


BUILD FAILED

Total time: 3.482 secs

@stevesaliman
Copy link
Owner

The cobertura plugin needs to know if the android plugin has been applied so it can decide if it needs to configure itself for a java build or an android one, which is why it needs to be applied first. What is really fascinating about your output is that it looks like it is applying the cobertura plugin before the android plugin, even though you're clearly trying to apply android first. I haven't seen anything in the docs that would suggest that Gradle would apply plugins in any order other than the one you specify.

Out of curiousity, What happens if you add the following between the two "apply" lines of your build.gradle:

def gotDroid = project.plugins.hasPlugin('com.android.application')
println "Got android? ${gotDroid}"

@thcuster
Copy link
Author

thcuster commented Jan 5, 2017

Still a failure, the hasPlugin returns True though.

./gradlew assembleDebug --info
Starting Build
Settings evaluated using settings file '/Users/thcuster/Projects/sw/android_app/settings.gradle'.
Projects loaded. Root project using build file '/Users/thcuster/Projects/sw/android_app/build.gradle'.
Included projects: [root project 'android_app', project ':app']
Evaluating root project 'android_app' using build file '/Users/thcuster/Projects/sw/android_app/build.gradle'.
Evaluating project ':app' using build file '/Users/thcuster/Projects/sw/android_app/app/build.gradle'.
Applying cobertura plugin to app
Creating configuration compile
Creating configuration apk
Creating configuration provided
Creating configuration wearApp
Creating configuration annotationProcessor
Creating configuration androidTestCompile
Creating configuration androidTestApk
Creating configuration androidTestProvided
Creating configuration androidTestWearApp
Creating configuration androidTestAnnotationProcessor
Creating configuration testCompile
Creating configuration testApk
Creating configuration testProvided
Creating configuration testWearApp
Creating configuration testAnnotationProcessor
Creating configuration debugCompile
Creating configuration debugApk
Creating configuration debugProvided
Creating configuration debugWearApp
Creating configuration debugAnnotationProcessor
Creating configuration testDebugCompile
Creating configuration testDebugApk
Creating configuration testDebugProvided
Creating configuration testDebugWearApp
Creating configuration testDebugAnnotationProcessor
Creating configuration releaseCompile
Creating configuration releaseApk
Creating configuration releaseProvided
Creating configuration releaseWearApp
Creating configuration releaseAnnotationProcessor
Creating configuration testReleaseCompile
Creating configuration testReleaseApk
Creating configuration testReleaseProvided
Creating configuration testReleaseWearApp
Creating configuration testReleaseAnnotationProcessor
Got android? true
Making :app:instrument depend on :app:classes
Making :app:cobertura depend on :app:test

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':app'.
> The 'java' plugin has been applied, but it is not compatible with the Android plugins.

* Try:
Run with --stacktrace option to get the stack trace. Run with --debug option to get more log output.

BUILD FAILED

Total time: 4.881 secs
Stopped 0 compiler daemon(s).

@stevesaliman
Copy link
Owner

I just noticed that we are dealing with a multi project build. Is there anyway that the cobertura plugin could be applied before the android plugin? From the last log, it looks like cobertura is being applied to the app project before Gradle knows about the android project. What does app/build.gradle look like? Is there an allprojects or subprojects block in the main build.gradle?

@thcuster
Copy link
Author

thcuster commented Jan 9, 2017

Here's the top level build.gradle. Looks like there is an allprojects block:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

If I add the cobertura plugin there the build is successful:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

plugins {
    id 'net.saliman.cobertura' version '2.4.0'
}

apply plugin: 'net.saliman.cobertura'


allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

@stevesaliman
Copy link
Owner

I must confess that this leaves me even more confused than before :-)

Adding the cobertura plugin to the root project without adding android should cause the cobertura plugin to add java the root project. I would expect this to make the problem worse, not better...

Your allprojects block only defines a repository that should be available, so that shouldn't be effecting plugin application to any of the projects.

I feel like the root of the problem is that gradle appears to apply android plugin after cobertura even though you've specified that android should be applied first, but I'm not sure how to get around that problem.

@KhaledHelmy
Copy link

KhaledHelmy commented May 22, 2017

The reason for that behavior is:

plugins {
    id 'net.saliman.cobertura' version '2.4.0'
}

triggers the plugin without the need of apply plugin: 'net.saliman.cobertura'. It seems that it automatically does the applying thing.

And since defining a plugins section in gradle has to be done before apply plugin section, it's certain that applying cobertura is done before android.

plugins {
    id 'net.saliman.cobertura' version '2.4.0'
}

apply plugin: 'com.android.application'

I managed to get around this by using the 2nd method here: https://github.com/stevesaliman/gradle-cobertura-plugin/blob/master/usage.md#applying-the-plugin

buildscript {
    repositories {
        mavenCentral()
        // The next repo is only needed while using SNAPSHOT versions
        maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
    }
    dependencies {
        classpath "net.saliman:gradle-cobertura-plugin:2.4.0"
    }
}

apply plugin: 'com.android.application'
apply plugin: 'net.saliman.cobertura'

@stevesaliman
Copy link
Owner

Good eye @KhaledHelmy. I was so busy looking at the two "apply" statements, that I didn't notice that the Cobertura plugin was also in the "plugins" section.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants