Chapter 11. Third-party Services

Well, we have made it. This is the last chapter of this book. I know, I am sad too, but after this chapter, I hope I leave you with a sense of accomplishment and enough knowledge that you can go off on your own and start making games in LibGDX.

Before you go, I would like to cover one last thing and that is how to integrate your game with third-party services. For example, there are social media platforms out there that have game-based integration. Whether it is achievements or leader boards that you are after, they will have them. In this chapter, we are going to look at how to integrate the game with a fictitious social media platform that provides high scores. This fictitious platform, which we will call as FriendFace, provides an Android library and we will use this in our integration. We will cover the following topics in this chapter:

  • How to use platform-dependent libraries
  • Keeping the game cross-platform-friendly

How to use platform-dependent libraries

In your quest to integrate third-party services, you will find that the providers very often do not provide you with similar-level tools to use their platform. For example, a user may have a library on a Maven repository somewhere and all you have to do is add the Gradle reference and Gradle will do the rest. Other users might have a Gradle-enabled project that you need to download and add to your project manually or just a simple JAR file that you need to download and add, and nothing else. We will quickly cover how you can go about importing the dependency into your project.

Now, the thing to bear in mind here is that, if you are using an Android library, then the project needs to solely exist on the Android subproject. The core project knows nothing about Android and has no idea about any of the Android classes that may be required to compile your project once you add the reference.

The nice way – via Maven

If the third-party you are interested in using has its library Mavenised—it is in a public access Maven repository—then all we have to do is edit our build.gradle build, for the whole project, and add an entry.

So, if we carry on with the use of an Android library as an example, we need to update the Android subproject entry as:

project(":android") {
  apply plugin: "android"
  configurations { natives }
  dependencies {
    compile project(":core")
    compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"
    compile 'com.madeup.friendface:friendface-android-sdk:1.0.0'
    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-x86"
  }
}

As you can see, adding the dependency to the project is just one simple line:

compile 'com.madeup.friendface:friendface-android-sdk:1.0.0'

When you run the program, bear in mind that this is fake and doesn't exist—this is for when you have a real-life dependency. At that time, Gradle will go off and resolve it for you and download the library, which will be add to the projects classpath. Then, if FriendFace releases a new version, we just change the build number at the end of the program and it will get updated.

Pretty nifty I think!

An alternative to Maven – A project/JAR file

OK, so the next third-party provider isn't as helpful, to us at least. For this, you have to visit their website and discover that they have a ZIP file that you need to download. This archive contains a Gradle project that you need to place into yours. Generally, I place these inside a directory in the subproject that it belongs to. Next, I update the settings.gradle file of the main project to include this. Say the file is in the Android subproject, I have the following:

include 'desktop', 'android', 'core'
include ':android:libs:thirdparty'

You will then need to reference it in a similar way to what we did for the Maven dependency:

compile project(":android:libs:thirdparty")

However, there are downsides to this approach. Firstly, you now have enlarged the size of your project; if you work in a team, this project is now very much tied to your code base. Secondly, it is not as easy as the Maven dependency when it comes to updating the library. Finally, back in the team scenario, if you don't package this with your project and ask other team members to download it, they might download the wrong version and cause inconsistencies with your code base.

Adding just a JAR file in Gradle is actually really simple. You could create a directory, I call it "libs", in your subproject and place your JAR file there. Then, in the Gradle build file, use the following line in your dependencies:

compile fileTree(dir: 'libs', include: ['*.jar'])

This line is actually a catchall. Any JAR files in this directory will get picked up, which is generally what you would want anyway.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.178.53