126

I'm trying to compile my Android project and I'm getting this error

[INFO] Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0
[INFO]  at java.lang.ClassLoader.defineClass1(Native Method)
[INFO]  at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
[INFO]  at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
[INFO]  at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
[INFO]  at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
[INFO]  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
[INFO]  at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[INFO]  at java.security.AccessController.doPrivileged(Native Method)
[INFO]  at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
[INFO]  at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
[INFO]  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
[INFO]  at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
[INFO]  at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

I've been reading in other post that try to compile with Java 8 might cause that error but not my case, I have the following Java version:

java version "1.7.0_79"
OpenJDK Runtime Environment (IcedTea 2.5.5) (7u79-2.5.5-0ubuntu0.14.04.2)
OpenJDK 64-Bit Server VM (build 24.79-b02, mixed mode)

OS: Linux Mint (I'm not have Java 8 installed.) Build: MAVEN

Can anyone help me with this?

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Rafael Reyes
  • 2,615
  • 8
  • 34
  • 51

28 Answers28

236

I face this problem too when making new project from android studio.

I've been able to resolve this by downgrading buildToolsVersion in app gradle setting: change {module-name}/build.gradle line

buildToolsVersion "24.0.0 rc1"

to

buildToolsVersion "23.0.3"


@Edit:
In Android Studio 2.1 Go to File-> Project Structure->App -> Build Tool Version. Change it to 23.0.3

Do the method above, only when you are using java version 7 and for some reason do not want to upgrade to java 8 yet.

starball
  • 20,030
  • 7
  • 43
  • 238
Rudy Kurniawan
  • 2,428
  • 1
  • 9
  • 13
  • @RafaelReyes I don't know anything about building android project with maven. But since android use build tools anyway, cant you find what version your build tool version used in maven configuration? – Rudy Kurniawan Apr 12 '16 at 05:16
  • 1
    @RudyKurniawanHi, I guess is 4.1.1.4 – Rafael Reyes Apr 12 '16 at 15:29
  • @RafaelReyes maybe you should do/check what Taylor says in his comment. Is "mvn -v" returning Java version: 1.7..? if yes, can you check your library used on your project, is there any library that build using java 8 and downgrade to version that compatible with java 7 – Rudy Kurniawan Apr 13 '16 at 10:50
  • mvn -v returns 1.7.0_80, vendor: Oracle Corporation, I don't know how to do "can you check your library used on your project, is there any library that build using java 8" any help? @RudyKurniawan – Rafael Reyes Apr 13 '16 at 12:35
  • 2
    The newer version: buildToolsVersion "23.0.3" – XurajB Apr 26 '16 at 00:53
  • 1
    it helped thanks for the answer, but what should we do if we need build 24?? – Shridutt Kothari Aug 23 '16 at 14:12
  • 2
    for that, you should use java 8 to build your app – Rudy Kurniawan Aug 30 '16 at 12:33
  • Had same problem with Xamarin and Visual Studio, downgrading to build tools 23.0.2 solved the problem. – Gekon Sep 12 '16 at 19:33
  • How to change this in Eclipse ? – Ambar Jain Oct 04 '16 at 11:40
  • @AmbarJain I personally doesn't use eclipse but you can try accepted answer in http://stackoverflow.com/questions/21779398/android-sdk-build-tools – Rudy Kurniawan Oct 12 '16 at 09:45
  • I had the same issue with IBM MobileFirst in eclipse. Downgrading the build tools solved it!!! Thank you so much! – papakias Nov 04 '16 at 12:58
  • @Gekon how can you do that, downgrading to build tools in Xamarin project in visual studio ? – GuidoG Mar 08 '17 at 11:50
  • @GuidoG Find Android SDK folder, go to tools, run android.bat, find Tools unselect higher version of build tools and select lower one. – Gekon Mar 09 '17 at 12:05
43

Gradle Scripts >> build.gradle (Module app)

Change buildToolsVersion "24.0.0" to buildToolsVersion "23.0.3"

source : experience

FBRNugroho
  • 710
  • 6
  • 8
31

Initially I had downgraded buildToolsVersion from 24.0.0 rc3 to 23.0.3, as specified in Rudy Kurniawan's answer. Then I have noticed that I have jdk 7 specified in my project settings. I have changed it to jdk 8 and now build tools 24.0.0 rc3 work.

enter image description here

It's also important to have compile options set to java7:

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}
14

I installed Android Studio alongside to Xamarin Visual Studio (was already installed). After the installation (excluded SDK, because they were already installed) I started Android Studio and it downloaded, installed and added missing SDKs (after giving the path to the SDK location). As a result I got the error message on build

java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.0

As a solution I installed jdk-8u101-windows-i586.exe without JRE (because it was already installed). Before I tried the x64 version, but it disappeared ... Then you have to change the Java Development Kit location.

Change JDK location

This is done in Visual Studio by clicking Tools > Options > Xamarin > Android Settings. Here you can navigate to the correct location. In my case it was C:\Program Files (x86)\Java\jdk1.8.0_101.

Finally, clean your project and make a new build :-)

testing
  • 19,681
  • 50
  • 236
  • 417
  • "Or set the AndroidSdkBuildToolsVersion MSBuild property to an earlier version. For example, if you also have version 23.0.3 installed, you could add the following line within the top element in the Android project .csproj file: 23.0.3" **ref:** https://releases.xamarin.com/technical-bulletin-android-sdk-build-tools-24/ – samus Jun 08 '18 at 20:36
8

The problem specified:

Exception in thread "main" java.lang.UnsupportedClassVersionError: com/android/dx/command/Main : Unsupported major.minor version 52.

is caused when is declared inside the build.gradle, buildToolsVersion 24 and we don´t have Java 8 installed that is required for this version. To solve this problem we have to change from buildToolsVersion from 24 to maximum 23:

enter image description here

Java 8 must be required in the future for Android Studio, so we have to start using Java 8.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
6

I had the same problem with my IntelliJ Maven build. My "solution" was to go into the build tools and remove the build tools 24.0.0 folder. I found it in the {android-sdk-location}/build-tools/ directory. This is not a long term fix, but this should at least get your project building again. Upgrading to Java 8 as many have suggested will be better long term.

sotrh
  • 386
  • 5
  • 13
  • This is the first option that I could actually use, because I couldn't find the settings mentioned in other answers in Android Studio's UI. I deleted it and then created a new project and it worked. – Mitch Jul 01 '16 at 09:32
5

I also faced this problem when making a new project in eclipse.

  1. Open your eclipse installation directory
  2. Open the file eclipse.ini
  3. Modify

    Dosgi.requiredJavaVersion=1.6 
    

    to

    Dosgi.requiredJavaVersion=1.7
    

Hope this helps

timbre timbre
  • 12,648
  • 10
  • 46
  • 77
user6661437
  • 51
  • 1
  • 1
5

What no one here is saying is that with Build Tools 24.0.0, Java 8 is required and most people have either 1.6 or 1.7.

So yeah, setting the build tool to 23.x.x would 'solve' the problem but the root cause is the Java version on your system.

On the long term, you might want to upgrade your dev environment to use JDK8 to make use the new language enhancements and the jack compiler.

Jay Sidri
  • 6,271
  • 3
  • 43
  • 62
5

52 is the code for JDK 8. You can see that here: http://javarevisited.blogspot.com/2015/05/fixing-unsupported-majorminor-version.html. So Gradle is expecting you to have JDK 8 as the default Java version on your machine. Newest versions of Gradle for Android won't work with Java 7.

The solution is simple: make Java 8 the default jdk version on your machine.

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
  • How does one do that ? – GuidoG Mar 08 '17 at 11:54
  • @GuidoG You need to follow the installation instructions for your specific operating system. Just Google it, and you'll get the detailed instructions. The goal is to be able to run **java -version** from the commandline and see it as `1.8`. – IgorGanapolsky Mar 08 '17 at 14:40
4

I am on Android Studio 2.0 and facing the same problem. The solution provided by Rudy Kurniawan sorted out this issue. The affected file can be located by:

"Project" -> "Android" -> "Gradle Scripts" -> "build.gradle(Module:app)"

The "Project" pane is default on the left. To find it, inspect the name of the side tabs, click it, then use the top pull down box to get to "Android". Expand the tree in the pane to find the target file. Double-click it to edit.

coarist
  • 789
  • 9
  • 11
4

Sorry for the late reply.Hope it helps someone else

This problem is related with your SDK, not with your JDK. You can check your version information from

Help > About > Show Details

You will get something like

Xamarin.Android Version: 6.0.2.1 (Starter Edition) Android SDK: X:\Android\android-sdk

Supported Android versions:

4.0.3 (API level 15)

4.4 (API level 19)

6.0 (API level 23)

SDK Tools Version: 24.4.1

SDK Platform Tools Version: 23.0.1

SDK Build Tools Version: 24 rc2

Java SDK: X:\Program Files (x86)\Java\jdk1.7.0_71

java version "1.7.0_71"

Java(TM) SE Runtime Environment (build 1.7.0_71-b14)

Java HotSpot(TM) Client VM (build 24.71-b01, mixed mode, sharing)

If you are using preview tools for building ,then you will get similar errors all over.

What to do now?

goto

Tools -> SDK manager

  1. Select all items in preview channels including Android SDK build tools with rev 24rc2 or 24rc4(latest)

  2. Click on Delete 'n' packages

How to turn off preview channel

  1. In SDK manager, select

    Tools > options

  2. Uncheck Enable preview tools

And what

Back to your code Clean all and rebuild (In rare cases, you have to select build-tools from project settings page)

Habel Philip
  • 205
  • 3
  • 10
  • This worked for me fine. but before that I installed jdk 8 and changed the directory as described in another answer here, – Abdur Rahim Oct 24 '16 at 18:08
  • How is Xamarin relevant to OP's question? – IgorGanapolsky Dec 01 '16 at 16:48
  • "Or set the AndroidSdkBuildToolsVersion MSBuild property to an earlier version. For example, if you also have version 23.0.3 installed, you could add the following line within the top element in the Android project .csproj file: 23.0.3" – samus Jun 08 '18 at 20:35
2

Thanks everyone for solution.

I could solve my same problem using below solution.

In my project, I added jar which were created in Java8. And my project was referring to JRE 7. When i changed project JRE to 8. My problem solved.

Steps: In eclipse, right click on project name in project explorer > build path > Libraries > click on jre version > click edit > installed JRE > add > standerd VM > select JRE home click-path (path should be localePath\java\jdk1.8.0_25\jre) > provide name > save > select same JRE for project > finish > ok Refresh/build project once > try to run your java file. it should work.

haithem
  • 21
  • 1
2

Get this error message on the travis build server?

I fixed my travis build with buildToolsVersion "24.0.2" using this

# travis build 
language: android

jdk:
  - oraclejdk8

android:
  components:
    - tools
    - build-tools-24.0.2
    - android-23

    - add-on
    - extra

script:
  - jdk_switcher use oraclejdk8
  - ./gradlew assemble
k3b
  • 14,517
  • 7
  • 53
  • 85
  • thats correct. when i tried to fix my travis build i found this question wich did not help me. after i found out how to fix i added this to for others with the same problem – k3b Dec 02 '16 at 15:07
2

In my case the default jdk was 1.7 then I changed it to 1.8 by browsing to the exact folder where the updated jdk is found in.

Hedego
  • 276
  • 2
  • 12
1

From this question.

You can try to change your compiler level back to 1.7 in the IDE you're using.

  • If you're on Eclipse, go to Windows ---> Prefences then select Java and expand it then select Compiler and change the compliance level to 1.7.
  • If you're on Android Studio, you can add this to your build.gradle

    android {
        compileSdkVersion 19
        buildToolsVersion "19.0.0"
    
        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 19
        }
    
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_7
            targetCompatibility JavaVersion.VERSION_1_7
        }
    }
    

If this doesn't help, try checking other's answer in the link mentioned above and let us know what helped you.

Community
  • 1
  • 1
cristianorbs
  • 670
  • 3
  • 9
  • 16
1

I had the same problem and tried to change the version, sometimes is for the version of Android Studio in Project> Gradle Scripts> build.gradle(Module:app) you can change the version. For example:

android {
    compileSdkVersion **23**
    buildToolsVersion **"23.0.3"**

    defaultConfig {
        applicationId "com.example.android.myapplication"
        minSdkVersion **19**
        targetSdkVersion **23**
        versionCode **1**
        versionName **"1.0"**
    }
DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
1

Your Android build tools are not properly installed. Try installing some other version of build tools and give that version in the gradle file. or you can go to this directory

C:\Users\\AppData\Local\Android\sdk\build-tools

and see which build tools is installed. Try changing the build tool version in the gradle file and compile the app to see if it is working.

i had 22.0.1,23.0.02 and 24.0.0 versions of build tools and only the old 22.0.1 version worked.

source: i tried it myself and it worked for me.

Swathin
  • 516
  • 1
  • 8
  • 23
1

This problem stems from the fact that your build tool version and JDK won't work together. I was using JDK version 7 with build tool 24.0.1. Of course lowering build tool version may fix the problem but it is not a real fix, as upgrading your build tools in future will break it again.

You should make sure build tool version and JDK work together. I upgraded my build tool to latest (24.0.1) and installed jdk8. Make sure to set your JAVA_HOME to latest and then try clean build.

1

Got the same issue outside of Android Studio, when trying to build for Cordova using the sdk from command line (cordova build android).

The solution provided by Rudy works in this case too. You just don't downgrade the version of Build Tools from Android Studio but you do it using the android sdk manager ui.

Andrea Gherardi
  • 826
  • 1
  • 9
  • 18
0

I just encountered this problem.

Reverting from the "Preview Channel" versions of the SDK tools and build tools to the latest stable version fixed it.

Mike Playle
  • 387
  • 3
  • 6
0

Changing com.android.tools.build:gradle:2.2.0-beta1 to com.android.tools.build:gradle:2.1.2 fixed it for me.

My Android Studio version is 2.1.2, perhaps that's why

ericn
  • 12,476
  • 16
  • 84
  • 127
0

The Simplest solution is you have to update your JDK to latest, version . Because Cordova supports only latest JDK .

1. First of all you have to update your Android SDK. Then,

2. If you are not able to update through your terminal , Got to your Software center of Linux destro and search the Latest (which one you want to install) and install that one .

Then again run Cordova build .

It will solve the problem .

In ,my case it worked.

PyDevSRS
  • 1,715
  • 16
  • 17
0

You need to go into your SDK installation directory, and make sure that the /build-tools sub-directory matches the buildToolsVersion in your app's build.gradle file: enter image description here

IgorGanapolsky
  • 26,189
  • 23
  • 116
  • 147
0

Check that you have the latest version of the SDK installed in either C:\Program Files\Java or C:\Program Files (x86)\Java

Then go to VS2015 and follow the path tools - options - Xamarin - Android settings and in the section JDK location select change and go to look for the version you have either x64 or x32.

Daniel
  • 1,229
  • 14
  • 24
0

I am on cordova, here's what I've done:

- rename your android project, so that there's space for new one
- update JAVA Development Kit to 1.8 (latest one in time of writing this)
- run cordova platform add android

Should work without problems now. Your app hash will change, so if you are using it f.e. for facbeook login, you will need to update it

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
mate.gvo
  • 1,093
  • 14
  • 20
0

I'd had this issue for too long (SO etc hadn't helped) and only just solved it (using sdk 25).

-The local.properties file proguard.dir var was not applying with ant; I'd specified 5.3.2, but a command line call and signed export build (eclipse) was using 4.7, from the sdk dir(only 5+ supports java 8)

My solution was to replace the proguard jars (android-sdk/tools/proguard/libs directory) in the sdk with a current (5+) version of proguard.

rndStr
  • 189
  • 2
  • 4
0

unsupported-major-minor-version error can be because of unsupported JDK version. Update JDK, Go to Module Settings and change the JDK path to the new one. In most of the cases, it fixes the error.

-1

I had two versions of java jdk installed. Once I pointed to right version of java jdk in File->Project Structure -> SDK Location to the latest version which I had in JAVA_HOME environment variable, the issue went away.