2

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?

John
  • 6,433
  • 7
  • 47
  • 82

2 Answers2

3

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.

John
  • 6,433
  • 7
  • 47
  • 82
  • While this was a quick and repeatable solution, wilder modifications to these files as described by maklas below have got libgdx working with java8, which I think was the issue as when I was using IntelliJ 2017 I wasn't using 8's lambdas. – John May 07 '18 at 19:00
3

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
maklas
  • 332
  • 2
  • 9
  • @John. Hah, it's not that clean. I just written changes over default libgdx gradle configuration. Don't delete anything else – maklas May 07 '18 at 15:31
  • 1
    wow, thanks for all the effort that went into discovering this stuff. One thing I noticed, I needed to add `jcenter()` next to `google()`, not sure how I decided that amongst the various other repo's, but I was missing something to do with kotlin (possibly legacy from my previous intention to write this in kotlin) until I did. I didn't even have to delete any .gradle file/folder. – John May 07 '18 at 19:14