1

I have made a gaming app and I now want to add google achievement xp to my app how should I do that I have added achievements using this guide but there is no guide on internet that I can find that should add player's earned xp.

Here is my code to unlock achievement but its not unlocking xp :

void giveAchievements(int counter) {
    if (counter == 10) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_first_10_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-  eXOwZsFEAIQBg", counter);
        }
    }
    if (counter == 50) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_50_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-eXOwZsFEAIQBg", counter);
        }
    }
    if (counter == 100) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_100_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 250) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_250_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-eXOwZsFEAIQBg", counter);
        }
    }
    if (counter == 500) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_500_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 1000) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_1000_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 1500) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_1500_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 3000) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_3000_clicks));
            Games.Leaderboards.submitScoreImmediate(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
    if (counter == 5000) {
        if (getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(),
                    getString(R.string.achievement_5000_clicks));
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_leaderboard), counter);
        }
    }
}
Simon Schubert
  • 2,010
  • 19
  • 34
superhuman
  • 47
  • 2
  • 12
  • If my answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – Simon Schubert May 31 '16 at 09:27
  • @SimonSchubert you are truly a life saver thanks man! – superhuman May 31 '16 at 09:41
  • Great that it works! – Simon Schubert May 31 '16 at 09:45
  • @SimonSchubert but it is now crashing – superhuman May 31 '16 at 09:52
  • @SimonSchubert it is showing this error now Error sending crash report ait: Server did not receive report: Origin Error message: Mobile Crash and Performance Reporting API has not been used in project admob-app-id-6652174517-638fe before or it is disabled. – superhuman May 31 '16 at 09:54
  • But this has nothing to do with the xp calculation. At which code line does it crash? It has something to do with you admob integration. Did you change anything? – Simon Schubert May 31 '16 at 10:03
  • @SimonSch@SimonSchubert it is showing this error now Error sending crash report java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference ubert – superhuman May 31 '16 at 10:06
  • At which line do you get the NullPointerException? – Simon Schubert May 31 '16 at 10:09
  • @SimonSchubert at these lines :- – superhuman May 31 '16 at 10:11
  • at android.content.ContextWrapper.getPackageName(ContextWrapper.java:139) at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:375) at android.preference.PreferenceManager.getDefaultSharedPreferences at com.org.superhuman.freexpandlevelup.MainActivity.getXp – superhuman May 31 '16 at 10:11
  • @SimonSchubert it is at your getXp() method – superhuman May 31 '16 at 10:14
  • OK my solution was more like a hint, not a copy-paste solution. I edited my answer. I hope it works now :) – Simon Schubert May 31 '16 at 10:19
  • @SimonSchubert thanks again! should i add these lines to every counter possible value? int xp = getXp(); xp += 500; saveXp(xp); to get the user xp as of now it shows xp to me but on google play store app it doesn't add up to get to the main xp. – superhuman May 31 '16 at 10:37
  • Yes. And it will add xp to your google+ profile, maybe not now because you use a developer version. – Simon Schubert May 31 '16 at 11:00
  • @SimonSchubert one last thing buddy when i publish the release version of this app will the achievements will show on the google game play app and because it is not showing now and i haven't re-uploaded this app-release apk on the google play store. – superhuman May 31 '16 at 11:06
  • @SimonSchubert and i am using app-release apk on my phone and its not updating my google game xp profile. – superhuman May 31 '16 at 11:10
  • It will show your progress in the google play app after you released the version on google play. – Simon Schubert May 31 '16 at 11:13
  • Why did you unmark the answer? I solved your problem. The other bugs have nothing todo with your question. – Simon Schubert May 31 '16 at 16:06
  • @SimonSchubert check again i marked and upvoted your answer. – superhuman Jun 01 '16 at 05:51

1 Answers1

2

The XP isn't based on your game it's based on your Google+ Profile. Let's say you unlock an achievement in the game Orc Genocide and you get 500xp to your Google+ profile. After that you unlock an achievement in your game and earn 1000xp. So your Google+ profile has 1500xp in total.

But you can easily calculate the XP by yourself and store it in SharedPreferences :)

void giveAchievements(int counter) {
    if (counter == 10) {   
        int xp = getXp();
        xp += 500;
        saveXp(xp);
        if(getApiClient().isConnected()) {
            Games.Achievements.unlock(getApiClient(), getString(R.string.achievement_first_10_clicks));   
            Games.Leaderboards.submitScoreImmediate(getApiClient(), "CgkI-  eXOwZsFEAIQBg", counter);
        }
    }
}

private int getXp() {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    return sharedPref.getInt("xp", 0);
}

private void saveXp(int xp) {
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putInt("xp", xp);
    editor.apply();
}

https://support.google.com/googleplay/answer/3129940?hl=en

When you get in-game achievements, you can earn experience points (XP) and levels on your Play Games profile.

When you're playing a game, you'll see a notification when you've earned XP or enough points to level up your Play Games profile. When you level up, you'll also see a notification in your mobile device's notification shade near the top of your screen and your Play Games Inbox menu.

You can view your XP and individual achievements on games you use with Play Games on your Play Games profile page.

For more information on how to make your Game Profile public or private, go to Play games on your device.

Simon Schubert
  • 2,010
  • 19
  • 34