LibGDX Tutorial - UPDATE and Upload to GitHub

As promised the full game has been uploaded to GitHub.

There have been some important changes.

  1.  LibGDX has released a new version, 1.9.9  
  2.  Android Studio has been updated to version 3.4.0
  3. Google Ads services has been updated to version 17.2.0

All the posts that need to cope with those changes have been updated. Don't forget to update your project to this LibGDX version and change the neccessary dependencies. It is important to summarize the changes here. 


Go to the " AndroidManifest.xml ", which now looks like:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.getest.game" >
 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/GdxTheme" >
        <activity
            android:name="com.getest.game.AndroidLauncher"
            android:label="@string/app_name"
            android:screenOrientation="landscape"
            android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
        <meta-data android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true" />
    </application>
</manifest>

The way the google ads services has to be declared changed a little bit and also we completely removed the line

   <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="28" />

from our file because that's not anymore needed!

Note that in order to display your real ads, if you have an admob account and a published game, then you have to change the following lines;

        <meta-data android:name="com.google.android.gms.ads.AD_MANAGER_APP"
            android:value="true" />

to

     <meta-data android:name="com.google.android.gms.ads.APPLCATION_ID"
            android:value="YOUR_ADMOB_APP_ID" />

where the ID of your app can be found in the Admob page after your log in with your account. If that's the case, you also have to change the " AndroidLauncher.java " in order to initialize the Mobile Ads SDK with your Admob app ID. This class now becomes;


 //Rest of the code
public void setupAds() { MobileAds.initialize(this, "YOUR_ADMOB_APP_ID"); bannerAd = new AdView(this); bannerAd.setVisibility(View.INVISIBLE); bannerAd.setBackgroundColor(0xff000000); // black bannerAd.setAdUnitId(BANNER_AD_UNIT_ID); bannerAd.setAdSize(AdSize.SMART_BANNER); interstitialAd = new InterstitialAd(this); interstitialAd.setAdUnitId(INTERSTITIAL_AD_UNIT_ID); rewardedVideoAd= MobileAds.getRewardedVideoAdInstance(this); AdRequest.Builder builder = new AdRequest.Builder(); AdRequest ad = builder.build(); interstitialAd.loadAd(ad); loadRewardedVideoAd(); } // Rest of the code

Then, open  " build.gradle (Module: Android) ". This should now look like this:

android {
    buildToolsVersion "28.0.3"
    compileSdkVersion 27
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }

    }
    packagingOptions {
        exclude 'META-INF/robovm/ios/robovm.xml'
    }
    defaultConfig {
        applicationId "com.getest.game"
        minSdkVersion 14
        targetSdkVersion 27
        multiDexEnabled true
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}


// called every time gradle gets executed, takes the native dependencies of// the natives configuration, and extracts them to the proper libs/ folders// so they get packed with the APK.
task copyAndroidNatives() { 
    file("libs/armeabi/").mkdirs();
    file("libs/armeabi-v7a/").mkdirs();
    file("libs/arm64-v8a/").mkdirs();
    file("libs/x86_64/").mkdirs();
    file("libs/x86/").mkdirs();

    configurations.natives.files.each { jar ->
        def outputDir = null
        if(jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
        if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")        
        if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
        if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
        if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
        if(outputDir != null) {
            copy {
                from zipTree(jar)
                into outputDir
                include "*.so"
            }
        }
    }
}

task run(type: Exec) {
    def path
    def localProperties = project.file("../local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { instr ->
            properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        if (sdkDir) {
            path = sdkDir
        } else {
            path = "$System.env.ANDROID_HOME"
        }
    } else {
        path = "$System.env.ANDROID_HOME"
    }

    def adb = path + "/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.getest.game/com.getest.game.AndroidLauncher'
}

// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
    // need to specify Java source sets explicitly, SpringSource Gradle Eclipse plugin    // ignores any nodes added in classpath.file.withXml
    sourceSets {
        main {
            java.srcDirs "src", 'gen'
        }
    }

    jdt {
        sourceCompatibility = 1.6 
        targetCompatibility = 1.6
    }

    classpath {
        plusConfigurations += [ project.configurations.compile ]        
        containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'
    }

    project {
        name = appName + "-android"
        natures 'com.android.ide.eclipse.adt.AndroidNature'
        buildCommands.clear();
        buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
        buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
        buildCommand "org.eclipse.jdt.core.javabuilder"
        buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
    }
}

// sets up the Android Idea project, using the old Ant based build.
idea {
    module {
        sourceDirs += file("src");
        scopes = [ COMPILE: [plus:[project.configurations.compile]]]        

        iml {
            withXml {
                def node = it.asNode()
                def builder = NodeBuilder.newInstance();
                builder.current = node;
                builder.component(name: "FacetManager") {
                    facet(type: "android", name: "Android") {
                        configuration {
                            option(name: "UPDATE_PROPERTY_FILES", value:"true")
                        }
                    }
                }
            }
        }
    }
}

We basically added the " multiDexEnabled true " line which is important for an error - free build. Don't forget to ALWAYS change the " targetSdkVersion " and " buildToolsVersion " and
" compileSdkVersion "  according to the latest updates of LibGDX. The last two can be found inside the same file, at another part of the code.

Finally, open " build.gradle (Project: Getest) ". It now becomes:

buildscript {
    

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        google()
        jcenter()

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

allprojects {
    apply plugin: "eclipse"
    apply plugin: "idea"
    version = '1.0'
    ext {
        appName = "getest"
        gdxVersion = '1.9.9'
        roboVMVersion = '2.3.5'
        box2DLightsVersion = '1.4'
        ashleyVersion = '1.7.0'
        aiVersion = '1.8.0'
    }

    repositories {
        mavenLocal()
        mavenCentral()
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
        maven { url "https://oss.sonatype.org/content/repositories/releases/" }
        maven { url "https://maven.google.com/"}
    }
}

project(":android") {
    apply plugin: "android"
    configurations { natives }

    dependencies {
        implementation project(":core")
        implementation 'com.google.android.gms:play-services-ads:17.2.0'
        implementation 'com.android.support:multidex:1.0.3'
        implementation "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86_64"
        implementation "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-box2d-platform:$gdxVersion:natives-x86_64"
        implementation "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-controllers-android:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-x86_64"
        implementation "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-armeabi-v7a"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-arm64-v8a"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-bullet-platform:$gdxVersion:natives-x86_64"
        implementation "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion"
     }
}

project(":core") {
    apply plugin: "java"

    dependencies {
        implementation "com.badlogicgames.gdx:gdx:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-box2d:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-controllers:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-freetype:$gdxVersion"
        implementation "com.badlogicgames.gdx:gdx-bullet:$gdxVersion"
        implementation "com.badlogicgames.box2dlights:box2dlights:$box2DLightsVersion" 
   }
}

tasks.eclipse.doLast {
    delete ".project"
}


We changed every " compile " to " implementation ", we added these lines

        implementation 'com.google.android.gms:play-services-ads:17.2.0'
        implementation 'com.android.support:multidex:1.0.3'

for the google ads and multidex, also this line

maven { url "https://maven.google.com/"}

for the google ads as well and finally we changed the

        google()
        jcenter()

" jcenter() " function's position to be underneath the " google() " function.

You may have also noticed that we changed the LibGDX and gradle versions which as i told you is important if you previously used an older version of LibGDX. If you also noticed that previously we targeted SdkVersion = 28 and now we target SdkVersion = 27 then you are right. We did that because LibGDX 1.9.9 targets the version 27. 

Let me clarify one last thing. Every time you update LibGDX you have to change all its dependencies versions. If you have just created the project with the latest LibGDX version you most probably don't need to update them.

It's time to build the project. If it prompts you to download the new google ads services, do it. DO NOT upgrade the gradle version from 3.2.0 though.

Feel free to ask any question!

Comments