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

2

6

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?

Storytelling answered 8/12, 2015 at 0:0 Comment(0)
L
2

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.

Landwaiter answered 23/12, 2015 at 12:3 Comment(0)
R
0

As of JDK 9 (**) you will probably have better luck with the --release option (see What is the --release flag in the Java 9 compiler?) rather than the --source and/or --target options. The release option will ensure that the code is compiled in an environment with the correct (older) APIs, and that the generated bytecode will run in older environments too.

In Gradle this is exposed as compileJava.options.release, as in:

compileJava.options.release = 8

to build source and bytecode for a JDK1.8 compatible environment.

See the Gradle documentation for more details: https://docs.gradle.org/current/userguide/building_java_projects.html#sec:java_cross_compilation

(**) See the Warning in the Gradle documentation that a bug in JDK 9 means JDK 10 is effectively required.

Rubbish answered 2/6, 2022 at 5:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.