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

Community
  • 1
  • 1
ajc
  • 1,685
  • 14
  • 34

7 Answers7

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

Nazim ch
  • 834
  • 8
  • 20
  • 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 Jan 24 '17 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'
Kevin Brotcke
  • 3,765
  • 26
  • 34
  • 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 Jan 24 '17 at 17:27
  • The line `compile fileTree(dir: 'libs', include: ['*.jar'])` includes any jars from the {app build directory}/lib. – Kevin Brotcke Jan 24 '17 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.

tnJed
  • 838
  • 9
  • 12
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
    }
}
Doron Yakovlev Golani
  • 5,188
  • 9
  • 36
  • 60
  • @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. – Doron Yakovlev Golani Jan 19 '17 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. – Chris Dennett Jan 24 '17 at 13:06
  • @ChrisDennett so the regular jdk is not good for android projects? – ajc Jan 24 '17 at 18:02
  • Correct :) Unless you're using something like LibGDX, which will bridge the gaps on export to Android – Chris Dennett Jan 24 '17 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'
ChaitanyaAtkuri
  • 1,672
  • 11
  • 15
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
}
user1173706
  • 2,612
  • 3
  • 19
  • 29
vicky
  • 412
  • 4
  • 18
-1

check if more than one jdk is installed.

Akhil
  • 22
  • 2
  • 5