Here’s a forum discussion on the matter, also mentioning iOS specific things.

    Sometimes it is necessary to access platform specific APIs, e.g., adding advertisement services or leaderboard functionality provided by frameworks such as Swarm.

    This can be achieved by allowing specific implementation to be defined through a common API interface; so called interface class.

    The following example is pure fiction and assumes we want to use a very simple leaderboard API that is only available on Android. For other targets we simply want to log invocations or provide mock return values.

    The Android API looks like this:

    1. /** Let's assume this is the API provided by Swarm **/
    2. public class LeaderboardServiceApi {
    3. public void submitScore(String user, int score) { ... }
    4. }

    The first step is to create an abstraction of the API in form of an interface.

    The interface is put into the core project (see Project Setup, Running & Debugging):

    1. public interface Leaderboard {
    2. public void submitScore(String user, int score);
    3. }

    Next we create concrete implementations for each platform and put these into their respective projects.

    The following would go into the Android project:

    1. /** Android implementation, can access LeaderboardServiceApi directly **/
    2. public class AndroidLeaderboard implements Leaderboard {
    3. private final LeaderboardServiceApi service;
    4. public AndroidLeaderboard() {
    5. // Assuming we can instantiate it like this
    6. service = new LeaderboardServiceApi();
    7. }
    8. public void submitScore(String user, int score) {
    9. service.submitScore(user, score);
    10. }
    11. }

    The following would go into the desktop project:

    1. /** Desktop implementation, we simply log invocations **/
    2. public class DesktopLeaderboard implements Leaderboard {
    3. public void submitScore(String user, int score) {
    4. Gdx.app.log("DesktopLeaderboard", "would have submitted score for user " + user + ": " + score);
    5. }
    6. }

    The following would go into the HTML5 project:

    1. /** Html5 implementation, same as DesktopLeaderboard **/
    2. public class Html5Leaderboard implements Leaderboard {
    3. public void submitScore(String user, int score) {
    4. Gdx.app.log("Html5Leaderboard", "would have submitted score for user " + user + ": " + score);
    5. }
    6. }

    Next, the ApplicationListener gets a constructor to which we can pass the concrete Leaderboard implementation:

    1. public class MyGame implements ApplicationListener {
    2. private final Leaderboard leaderboard;
    3. public MyGame(Leaderboard leaderboard) {
    4. this.leaderboard = leaderboard;
    5. }
    6. // rest omitted for clarity
    7. }

    In each starter class we then simply instantiate MyGame, passing the corresponding Leaderboard implementation as an argument, e.g., on the desktop:

    1. public static void main(String[] argv) {
    2. LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
    3. new LwjglApplication(new MyGame(new DesktopLeaderboard()), config);
    4. }