Google Play Game Services offers cross platform social leaderboards, achievements, and much more (realtime multiplayer, cloud saves, anti-piracy...).
AdMob has been rebuilt, leveraging the best of Google's ad platforms. It's designed for app developers, with an intuitive UI to help you start earning from your app quickly, create campaigns in minutes to acquire new users, and gain insights into your app's performance.
I've started implementing google play game services leaderboards, achievements and google AdMob in my LibGDX Android Games, and the purpose of this blog article is to walk through the process so you can do the same!
- I've published an example project on Google Play check it out!
- The example project is freely available on GitHub check that out too!
Continue reading after the jump to find out how to add these features to your own games!
Development environment:
Android studio 0.8.2
Android SDK 20
LibGDX 1.2.0
Other dependence library, please check the build.gradle
Create project:
- Download the gdx-setup.jar from http://libgdx.badlogicgames.com/nightlies/dist/gdx-setup.jar. The jar is also contained in all stable and nightly releases.
- Execute the jar by double clicking it or from the command line via
java -jar gdx-setup.jar
- Specify your project's configuration (Configuration Panel)
- Specify the libgdx stable/nightly release zip file or press one of the buttons to download the latest stable/nightly release (Library Selection Panel).
- Hit the "Generate projects" button (Generation Panel)
Import the project into Android studio
File->Import Project,
There is one Gradle version error,
Error:The project is using an unsupported version of the Android Gradle plug-in (0.10.4)
<a href="fixGradleElements">Fix plug-in version and re-import project</a>
Change the " classpath 'com.android.tools.build:gradle:0.10+' " to "classpath 'com.android.tools.build:gradle:0.12.+'" in the build.gradle
Now, you can run the android application in the Android studio.
Set up the game in the Google Play Developer Console
You can follow Google instruction to set up the game:
Need to know these things, we need to use them in the project.
1, Game application ID
2, Achievements ID
3, Leaderborads ID
4, Create a linked application, certificate fingerprint
You need input the correct certificate fingerprint, if you want test the debug apk, use the debug keystore. Don't forget to update to release certificate fingerprint when you release the apk to Google Play.
Open a terminal and run the the Keytool utility to get the SHA1 fingerprint of the certificate. You should get both the release and debug certificate fingerprints.
To get the release certificate fingerprint:
keytool -exportcert \
-alias <your-key-name> \
-keystore <path-to-production-keystore> \
-list -v
To get the debug certificate fingerprint:keytool -exportcert \
-alias androiddebugkey \
-keystore <path-to-debug-keystore> \
-list -v
5, Add test user
Add the account you login on the test device into the testing user list.
Setup AdMob
You can follow this instruction to create app and adunit.
https://support.google.com/admob/v2/topic/2745286?hl=en&ref_topic=2792286
LibGDX App Setup in Android Studio
1, Add Google play service dependence
Open the build.gradle, add
compile 'com.google.android.gms:play-services:5.0.77'
2, Modify the AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
b. Add meta-data
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data android:name="com.google.android.gms.games.APP_ID" android:value="@string/app_id" />
c. Add AdActivity for AdMob
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
3. Add Game App Id in the strings.xml
<string name="app_id">794485152673</string>
4. Add Admob ad in the game.
Check the AndroidLauncher.java for detail.
a, Create the ad view
private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
//adView.setId(12345); // this is an arbitrary id, allows for relative positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
b. Start request the ad
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
5. Add game service
a. Add GameHelper.java and GameHelperUtils.java into android project. These are game service wrapped class.
b. Add game service interface in the core project. Use this interface to communicate between game and application.
public interface ActionResolver {
public boolean getSignedInGPGS();
public void loginGPGS();
public void submitScoreGPGS(int score,String id);
public void unlockAchievementGPGS(String achievementId);
public void getLeaderboardGPGS(String id);
public void getAchievementsGPGS();
}
Then implement the interface in the application.
public class AndroidLauncher extends AndroidApplication implements GameHelper.GameHelperListener, ActionResolver {
....
@Override
public boolean getSignedInGPGS() {
return gameHelper.isSignedIn();
}
@Override
public void loginGPGS() {
try {
runOnUiThread(new Runnable(){
public void run() {
gameHelper.beginUserInitiatedSignIn();
}
});
} catch (final Exception ex) {
}
}
@Override
public void submitScoreGPGS(int score,String id) {
Games.Leaderboards.submitScore(gameHelper.getApiClient(), id, score);
}
@Override
public void unlockAchievementGPGS(String achievementId) {
Games.Achievements.unlock(gameHelper.getApiClient(), achievementId);
}
@Override
public void getLeaderboardGPGS(String id) {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), id), 100);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
@Override
public void getAchievementsGPGS() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), 101);
}
else if (!gameHelper.isConnecting()) {
loginGPGS();
}
}
....
}
c. Call the game service interface method in the game, report the score and unlock the achievement
private void gameOver() {
gameEnd = true;
winGameOver.setVisible(true);
if (!game.actionResolver.getSignedInGPGS())
game.actionResolver.loginGPGS();
else {
int bestscore = GamePreferences.instance.getBestScore();
game.actionResolver.submitScoreGPGS(bestscore, GamePreferences.instance.getLeaderboardid());
String achid = null;
//
if (bestscore > 10) {
achid = "CgkIobeF2I8XEAIQCA";
} else if (bestscore > 50) {
achid = "CgkIobeF2I8XEAIQCQ";
} else if (bestscore > 100) {
achid = "CgkIobeF2I8XEAIQCg";
} else if (bestscore > 150) {
achid = "CgkIobeF2I8XEAIQCw";
} else if (bestscore > 200) {
achid = "CgkIobeF2I8XEAIQDA";
}
if (achid != null)
game.actionResolver.unlockAchievementGPGS(achid);
}
}
That's all there is to it! Now all you need to do is build your apk, drop the apk on your device, install, and play :)
Do you have a code example demo?
ReplyDeletewhere do you declare/import/state what is Games. Where do you declare gameHelper? This "tutorial" is missing a lot ot things
ReplyDeleteIf you are looking for an excellent contextual advertising network, I suggest you check out Clicksor.
ReplyDeleteشركة مكافحة حشرات بالمدينة المنورة
ReplyDeleteشركة تسليك مجاري بالمدينة المنورة
شركة تنظيف مسابح بالمدينة المنورة
شركة صيانة افران بالمدينة المنورة
شركة نقل اثاث بالمدينة المنورة
شركة تنظيف بالمدينة المنورة
شركة كشف تسربات المياه بالمدينة المنورة
المجموعة المثالية