问题
After migrating to Android Studio from Eclipse, I was struggling at best with it. Problem was that most guides and tutorials you could find are for Eclipse , but if you can find one for Android Studio, it is missing most important parts - implementing libraries or 3rd party code. After week of problems and errors here is a guide
回答1:
Requirements: Google play services and Google Repostory (under Extras in SDK manager), BaseGameUtils
Firstly we need to add BaseGameUtils as Module:
then select Import Gradle Project and chose BaseGameUtil folder (do not copy BaseGameUtils in project before that step. Let Android Studio do that) After gradle is done with syncing selefct Build > Make Module 'BaseGameUtils'
NOTE if u have uses-sdk:minSdkVersion error while build check this click here. Build :BaseGameUtils again after this
Last step for integranting BaseGameUtils is to include it into android module: Sync project afterwards.
NOTE if you get uses-sdk:minSdkVersion error while build this will help - link
Now it's time to open your AndroidManifest file and include:
before application tag
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
NOTE: there is no need to add google play dependencies in android gradle script becouse it is already included in BaseGameUtils
in application, before activity
<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
Now you need to set app_id number in Srtings (android/res/values/Strings.xml)
<string name="app_id">928019648708</string>
Your app id is located in GooglePlay developer console under Game Services section (under game name)
For passing android native code to game code we will create ActionResolver interface in core module:
package com.mygame.test;
public interface ActionResolver {
public boolean getSignedInGPGS();
public void loginGPGS();
public void submitScoreGPGS(int score, String id);
public void unlockAchievementGPGS(String achievementId);
public void getLeaderboardGPGS();
public void getAchievementsGPGS();
}
Now we need to make changes in AndroidLauncher class (check imports if you have problems):
package com.mygame.test.android;
import android.os.Bundle;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.mygame.test.ActionResolver;
import com.mygame.test.MyGame;
public class AndroidLauncher extends AndroidApplication implements ActionResolver, GameHelper.GameHelperListener {
private GameHelper gameHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (gameHelper == null) {
gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
gameHelper.enableDebugLog(true);
}
gameHelper.setup(this);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MyGame(this), config);
}
@Override
public boolean getSignedInGPGS() {
return gameHelper.isSignedIn();
}
@Override
public void loginGPGS() {
if (!gameHelper.isSignedIn()) {
try {
runOnUiThread(new Runnable() {
public void run() {
gameHelper.beginUserInitiatedSignIn();
}
});
} catch (final Exception ex) {
Gdx.app.log("MainActivity", "Log in failed: " + ex.getMessage() + ".");
}
} else {
gameHelper.reconnectClient();
}
}
@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() {
if (gameHelper.isSignedIn()) {
startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(), "CgkI5MyZk4FbHAJQXQ"), 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();
}
}
@Override
public void onSignInFailed() {
gameHelper.getSignInError();
}
@Override
public void onSignInSucceeded() {
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
gameHelper.onActivityResult(requestCode, resultCode, data);
}
}
Create constructor in your main class (MyGame in this example):
public ActionResolver resolver;
public Base(ActionResolver ar) {
resolver = ar;
}
to connect with gplay services call:
if (!resolver.getSignedInGPGS())
resolver.loginGPGS();
unlock achievements and submit scores with:
if (resolver.getSignedInGPGS()){
if (currentScore == 0){
resolver.unlockAchievementGPGS("CgkI5MyZk4EbEAIQAg");
}
if (Cookie.bestScore >= 100){
resolver.unlockAchievementGPGS("CgkI5MyZk4EbEAIQAw");
}
resolver.submitScoreGPGS(Cookie.bestScore, "CgkI5MyZk4EbEAIQAQ");
}
note: use corresponding id valuses found in developer console for each achievement and for leaderbord
and call leaderboards and achievements with
resolver.getLeaderboardGPGS();
resolver.getAchievementsGPGS();
Lastly desktop, ios and html launcher can be resolved in the same way (example for desktop launcher)
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
ActionResolver resolver= new ActionResolver() {
@Override
public boolean getSignedInGPGS() {
return false;
}
@Override
public void loginGPGS() {
}
@Override
public void submitScoreGPGS(int score, String id) {
}
@Override
public void unlockAchievementGPGS(String achievementId) {
}
@Override
public void getLeaderboardGPGS() {
}
@Override
public void getAchievementsGPGS() {
}
};
new LwjglApplication(new MyGame(resolver), config);
}
来源:https://stackoverflow.com/questions/32982472/libgdx-google-play-game-services-in-android-studio