12

I'm getting an error right after I installed Android studio and Created a simple app.

Steps followed:

  1. Fresh download & installed Android studio.
  2. Created a new project.

When the project loaded, The gradle failed with error:

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

Module Gradle File:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "info.ankitjc.happybirthday"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.1.0'
    testCompile 'junit:junit:4.12'
}

Project Gradle File:

// 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.3'

        // 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
}

I searched for possible solutions here.

After File > Invalidate Cache/Restart After File > Invalidate Cache/Restart

16
  • 1
    share gradle file
    – Pztar
    Commented Jan 13, 2017 at 20:20
  • Your links seem valid Remove the line with apply plugin: 'java' Commented Jan 13, 2017 at 20:28
  • @Pztar shared gradle file.
    – ajc
    Commented Jan 13, 2017 at 20:38
  • @cricket_007 I dnt have that line in my gradle file.
    – ajc
    Commented Jan 13, 2017 at 20:39
  • @ajc, try doing a Rebuild if that doesn't work then try going to File -> Invalidate Caches/Restart-> Invalidate and Restart
    – Pztar
    Commented Jan 13, 2017 at 20:51

7 Answers 7

2

I think path has not been set as enviorment variable.

check have you declared the java sdk path . set the path as environment variable.

type "javac" in cmd(cmd must have admin previlliage).

If compile is not successfful

1 . Open the jdk location and copy the path of "bin"

2 . Open system properties in control panel.

3 . Advanced system settings -> then select "environment variables"

4 . click on "new"

5 . set variable name as "path" and variable value copied address

then try again

1
  • I hope you mean "javac"? Yes, its there and working, Environment variable JAVA_HOME is set to right path. I did this many times now. Is there anything else u cna think of?
    – ajc
    Commented Jan 24, 2017 at 17:29
1

This means the Java plugin is being applied on top of the Android plugin. I don't see anything that stands out in your build files though. Some things to try:

  • Try doing a clean build from command-line. That will tell you if it's an Android Studio problem.
  • Create an empty app from a clean slate and see if it builds.
  • Update Android SDK to latest
  • Update Android Studio to the latest
  • Remove Android Studio project files and re-import
  • Make sure correct Java version is in your path and JAVA_HOME is set correctly
  • Check if there is any jars in your local lib folder that could be conflicting
  • Try using beta version of Android plugin compile 'com.android.tools.build:gradle:2.3.0-beta2'
2
  • I downloaded a fresh copy of Android SDK. Made sure correct java version is in my classpath and JAVA_HOME. What local lib folder did you suggest?
    – ajc
    Commented Jan 24, 2017 at 17:27
  • The line compile fileTree(dir: 'libs', include: ['*.jar']) includes any jars from the {app build directory}/lib. Commented Jan 24, 2017 at 19:14
1

I experienced this error while updating from KAPT to KSP. The docs say to add

id 'org.jetbrains.kotlin.jvm'

to the plugins block, but it appears that adding the Kotlin jvm plugin to the mix was causing my java plugin error. Remove the reference and it compiles. FYI the linked docs are more figurative than literal.

0

Not sure if this would help, but maybe you can try to explicitly add the java compilation option:

android {
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
4
  • @ajc, at this point, if I were you, I'd start a brand new project and add your dependencies and code slowly until you find something that breaks it. If you find that a clean project does not work, it means something is wrong with your setup. Commented Jan 19, 2017 at 15:36
  • You realise that Android Java isn't compatible with desktop Java? Google made some design changes in the early days that deviated from desktop Java. For this reason, you can't add desktop Java (Oracle/OpenJDK) libraries into your Android project. Commented Jan 24, 2017 at 13:06
  • @ChrisDennett so the regular jdk is not good for android projects?
    – ajc
    Commented Jan 24, 2017 at 18:02
  • Correct :) Unless you're using something like LibGDX, which will bridge the gaps on export to Android Commented Jan 24, 2017 at 19:55
0

Try removing these two lines of code from your build.gradle

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })

and

testCompile 'junit:junit:4.12'
0

Try this one Procedure :-

1.right click on project ->Open Module Settings or F4

2.then goto SDK Location

3.after that Check on use embedded jdk(recommended)

you can see image given below

enter image description here

build and run project.

You can also add this in your build.gradle file

compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
1
  • I dnt have the NDK bundle! Will that make any difference?
    – ajc
    Commented Jan 24, 2017 at 17:30
-1

check if more than one jdk is installed.

1
  • 1
    How do we check that? Commented Apr 23, 2018 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.