John
John

Reputation: 6648

How to develop with libGDX on both Android and Desktop (IntelliJ 2018 CE /Gradle) with Java 8

Slowly learning gradle. It is essential (to me) to rule out IDE bugs and run my project from the command line. This lead me to discover my path was referring to a JRE instead of JDK; I got an error message stating that tools.jar was not on the path and that was that.

However I still cannot get past about 80-something percent when I execute gradlew desktop:debug.

Hence this question is concerned with IntelliJ that I recently upgraded to the 2018 version. Version 2017 was working fine, with a big disclaimer that I wasn't, in fact using Java8 lambda's at that time. Moving to 2018 I felt it easier to create a new libGDX (best way I know to target multiple platforms) project to dump all my files into rather than debug all the (often unhelpful) stack traces I was getting.

Out of the box I performed minimal further modifications to gradle to get the desktop version working. I really don't want to repeat that as I opt for a new name which means a lot of editing and breaking. When it came to deploying on Android I got stuck.

What are the modifications needed to Gradle versions and gradle files to get Android builds working? For bonus points, why are there apparently 4 different versions of gradle involved?

Upvotes: 2

Views: 857

Answers (2)

maklas
maklas

Reputation: 332

I spent a lot of time to make libgdx compile in java8. And I did it. Works to me and my friends. If you wish to try it and it fails right away, give Intellij a couple of restarts and cache invalidations. Might need to delete .gradle from %userdir% (not necessarily), but in the end it worked stable.

Here is my config:

Intellij IDEA 18+ or Android studio 3.0+

wrapper.properties:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5-bin.zip

Project build.gradle:

repositories {
    google() //add google repo
}
dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1' //higher versions were not compiling right
}

Andoird build.gradle:

android{
    buildToolsVersion "27.0.3"
    compileSdkVersion 27

    defaultConfig {
        minSdkVersion 16 //16 is good enough for 2018
        targetSdkVersion 27
        //Forget about Jack if you were using it. 
    }

    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}
eclipse { 

    jdt {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
}

and for the Desktop build.gradle

sourceCompatibility = 1.8

Upvotes: 3

John
John

Reputation: 6648

In my top level build.gradle, by default I had

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'
    }
...

which I had to change to

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
    }
...

and extend:

buildscript {
...
    repositories {
        ...
        maven {
            url 'https://maven.google.com/'
            name 'Google'
        }
    }

Next, Lint and other sources led me to change a line of gradle/wrapper/gradle-wrapper.properties from

distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip

to

distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip

To my android level build.gradle add either:

android {
...
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
...

or

android {
...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
...

I'm posting this answer not because it's elegant or particularly enlightening but merely because it is the easiest way I know to switch my development between Android and desktop (it works in reverse). The error's I'm getting from IntelliJ and gradle were far from helpful and took a lot of research which is distilled here.

Update

I got stuck going back to Desktop and was getting a massive untraceable dump that I want to include for future reference:

java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203)
        at com.android.build.gradle.BasePlugin.lambda$configureProject$1(BasePlugin.java:436)
...

What that meant is that I hadn't followed the instructions I had confused the configurations described above.

Upvotes: 3

Related Questions