Configuring libGDX allows me to use Java 8, which crashes in Android

I am new to java / android, so I hope this is a pretty simple task.

I used the libGDX installation to create a project that I then imported into Android Studio. I did not know that the Supplier interface is available only for Java 8 and is used in the main module, which, as I understand it, is compiled into a library that is used by all versions of the game android / html / desktop / ios. The desktop version is working fine (I have Java 8 installed), but my Android application with a NoClassDefFoundError error (BTW, the error message indicated the name of the classes that I wrote, not Supplier , but the error disappeared as soon as I deleted every link to Supplier and returned when adding them, so obviously the error message does not show the actual class of the problem).

If I try to use Supplier in my android module, it will not even let me import the class, so it knows that it does not support this version of Java, but it will gladly allow me to use it without any warning. I am sure that removing links to Supplier will solve the problem, but this fix can subsequently lead to runtime errors if I inadvertently use any Java 8 features, so I cannot solve it.

I don’t know where in the project it is configured to use Java 8, and there seem to be a lot of .gradle, .xml, and .iml files that are apparently related to the configuration. Here is my build.gradle file in the main module:

 apply plugin: "java" sourceCompatibility = 1.6 [compileJava, compileTestJava]*.options*.encoding = 'UTF-8' sourceSets.main.java.srcDirs = [ "src/" ] eclipse.project { name = appName + "-core" } 
Android Studio shows the line sourceCompatibility = 1.6 highlighted in gray and says "Destination is not used" when I hover over it. So this may be part of the problem.

My core.iml file also contains this line:

 <component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6" inherit-compiler-output="false"> 

Also, here is the core section in the root build.gradle file:

 project(":core") { apply plugin: "java" dependencies { compile "com.badlogicgames.gdx:gdx:$gdxVersion" } } 

So what should I change so that it does not allow me to use Java 8 in my main project?

+6
source share
1 answer

I think you need to add targetCompatibility attribute with sourceCompatibility in gradle to target your java version.

  allprojects { apply plugin: 'java' sourceCompatibility = 1.6 targetCompatibility = 1.6 } 

You also need to check the java default path in the gradle.properties file with the version used by your project.

You need to check Cant to create Java 1.8 even when using the proper Java version and Gradle finds the wrong JAVA_HOME, although it is correctly installed to get more information about the java suite of the compiled version of gradle.

+1
source

All Articles