Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libGDX setup allows me to use Java 8 which crashes in Android

I'm new to java/android so I'm hoping this is a pretty easy thing to solve.

I've used libGDX's setup to create a project, which I then imported to Android Studio. I didn't know the Supplier interface was only available to Java 8, and used it in the core module, which as far as I understand, it compiles into a library that is shared by all android/html/desktop/ios versions of the game. The desktop version runs fine (I have Java 8 installed), but my android app crashes with a NoClassDefFoundError error (BTW, the error message was showing the name of classes that I wrote, and not Supplier, but the error disappeared as soon as I removed every reference to Supplier and came back when I added them, so apparently the error message doesn't show the actual problem class).

If I try to use Supplier in my android module, it won't even let me import the class, so it knows it doesn't support that Java version, but it'll happily let me use it in the core module without any warning. I'm sure removing references to Supplier will solve the problem, but this fix may cause runtime errors later on if I inadvertently use any Java 8 features, so not a good way to solve it.

I don't know where in the project is it set to use Java 8, and there seem to be a bunch of .gradle, .xml and .iml files which apparently are all related to configuration. Here is my build.gradle file in the core 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 sourceCompatibility = 1.6 line grayed out and says "Assignment is not used" when I hover over it. So that may be part of the problem.

My core.iml file contains this line too:

<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 to prevent it from letting me use Java 8 in my core project?

like image 826
Juan Avatar asked Dec 08 '15 00:12

Juan


People also ask

Do you need Android SDK for LibGDX?

The Android mobile OS is one of LibGDX's supported target platforms. Before you can create Android applications, you have to download and install the Android SDK.

Can I use Java 8 in Android Studio?

Android does not support Java 8. It only supports up to Java 7 (if you have kitkat) and still it doesn't have invokedynamic, only the new syntax sugar. If you want to use lambdas, one of the major features of Java 8 in Android, you can use gradle-retrolamba.

Is Java compatible with Android?

Android Gradle plugin 3.0. 0 and later support all Java 7 language features and a subset of Java 8 language features that vary by platform version. When building your app using Android Gradle plugin 4.0. 0 and higher, you can use a number of Java 8 language APIs without requiring a minimum API level for your app.


1 Answers

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
}

Also need to check java default path in gradle.properties file with version your project is use.

You need to check Can’t build Java 1.8 even with proper Java version in use and Gradle finds wrong JAVA_HOME even though it's correctly set to get more idea about set java compile version gradle.

like image 179
pRaNaY Avatar answered Oct 20 '22 01:10

pRaNaY