title: Google Play Achievements "IOException: Address already in use"
body: <p><strong>New Errors</strong></p>
  
  <p>Have app working (loads but states that the google play services is incorrectly set up).  Looking at the monitor it states <code>E/ViewRootImpl: sendUserActionEvent() mView == null</code></p>
  
  <p>And also see the below erorr when trying to award achievement:</p>
  
  <pre><code>07-19 20:51:42.696 16515-16515/com.gfaiers.hangman E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.gfaiers.hangman, PID: 16515
  java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.google.android.gms.common.api.GoogleApiClient.isConnected()' on a null object reference
  at com.gfaiers.hangman.HangmanActivity.endGameQuestion(HangmanActivity.java:842)
  at com.gfaiers.hangman.HangmanActivity$2.run(HangmanActivity.java:682)
  at android.os.Handler.handleCallback(Handler.java:739)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5951)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
  </code></pre>
  
  <p><strong>Tldr:</strong></p>
  
  <ul>
  <li>I've made <a href="https://play.google.com/store/apps/details?id=com.gfaiers.hangman&amp;hl=en_GB" rel="nofollow noreferrer">Hangman</a> game for Android in Android Studio. <a href="https://github.com/gfaiers/Hangman/blob/master/app/src/main/java/com/gfaiers/hangman/MainActivity.java" rel="nofollow noreferrer">Code here</a></li>
  <li>I want to use Achievements in the game.</li>
  <li>Following <a href="https://developers.google.com/games/services/android/init" rel="nofollow noreferrer">googles guides</a> hasn't worked for me so far.</li>
  <li>What to do now?</li>
  </ul>
  
  <p><strong>Answer</strong></p>
  
  <ul>
  <li>Simple typo. Seriously.</li>
  </ul>
  
  <p><strong>FREQUENTLY UPDATED AT BOTTOM</strong></p>
  
  <p>Before I begin; I'm new to Android development and this is my first time doing this - <em>imagine I know very little!</em> I've been stuck on this for a few days and have done a lot of reading about it - mainly on articles that are all very similar and say the same things.  Also, all of those articles from what I can tell are quite dated?</p>
  
  <p><strong>I've seen these</strong></p>
  
  <ul>
  <li><a href="https://developers.google.com/games/services/android/init" rel="nofollow noreferrer">https://developers.google.com/games/services/android/init</a></li>
  <li><a href="https://developers.google.com/games/services/android/achievements" rel="nofollow noreferrer">https://developers.google.com/games/services/android/achievements</a></li>
  <li><a href="https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples" rel="nofollow noreferrer">https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples</a></li>
  </ul>
  
  <p>I keep on falling over right at the start... After another day I've got a little further though...</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/16854760/setup-google-play-games-services">Setup Google Play Games Services</a></li>
  <li><a href="https://stackoverflow.com/questions/34749299/gradle-version-2-2-is-required-current-version-is-2-10">Gradle version 2.2 is required. Current version is 2.10</a></li>
  <li><a href="https://stackoverflow.com/questions/19272127/sdk-location-not-found-android-studio-gradle">SDK Location not found Android Studio + Gradle</a></li>
  </ul>
  
  <p>When using the samples from <a href="https://github.com/playgameservices/android-basic-samples/tree/master/BasicSamples" rel="nofollow noreferrer">here</a> firstly they all say on them </p>
  
  <blockquote>
    <p>Updating samples to not use PLUS scope. Also de-linted.</p>
  </blockquote>
  
  <p><em>Unsure what this actually means, but it makes me think they're out dated? Knowing how often everything moves on, they've not been edited for 4 months now (is that a long time for stuff like this?).</em></p>
  
  <p>In the samples when trying to use them I get the following errors (<em>when loading TrivialQuest which is the one that's linked to Achievements</em>):</p>
  
  <pre><code>Error:Gradle version 2.2 is required. Current version is 2.10. If using the gradle wrapper, try editing the distributionUrl in D:\Users\Geoff\AndroidStudioProjects\android-basic-samples-master\BasicSamples\TrivialQuest\gradle\wrapper\gradle-wrapper.properties to gradle-2.2-all.zip.
  </code></pre>
  
  <p>When using the wrapper:</p>
  
  <pre><code>Error:(23, 0) SDK location not found. Define location with sdk.dir in the local.properties file or with an ANDROID_HOME environment variable.
  </code></pre>
  
  <p>
  
  <p>Which in the other topics it states for this I need to change the directory in <code>local.properties</code> which is already correct.</p>
  
  <p><strong>Where I want help:</strong></p>
  
  <p>I have the app published on the <a href="https://play.google.com/store/apps/details?id=com.gfaiers.hangman&amp;hl=en_GB" rel="nofollow noreferrer">PlayStore</a> already, I have drafted achievements into it, and I just want to get them working with it.</p>
  
  <p>I need help finding out how to get BaseGameUtils working in my app, I have imported the module, and have followed <a href="https://developers.google.com/games/services/android/init" rel="nofollow noreferrer">this guide</a> up till "Accessing additional Google Play services APIs".  </p>
  
  <p>When trying to run the app in the emulator (Nexus 5, SDK23) through Android Studio it prompts me to update google play services (which can't be done on an emulator). I don't know where this message is even coming from, as there isn't a string for it throughout the whole application, it must be from google play services it's self.</p>
  
  <p>When trying it on a Galaxy S7edge (6.0.2) it crashes instantly with the following error:</p>
  
  <pre><code>06-26 14:46:05.080 2240-2240/com.gfaiers.hangman:background_crash E/InstantRun: IO Error creating local socket at com.gfaiers.hangman java.io.IOException: Address already in use
  at android.net.LocalSocketImpl.bindLocal(Native Method)
  at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:306)
  at android.net.LocalServerSocket.&lt;init&gt;(LocalServerSocket.java:48)
  at com.android.tools.fd.runtime.Server.&lt;init&gt;(Server.java:94)
  at com.android.tools.fd.runtime.Server.create(Server.java:88)
  at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:358)
  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1036)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6321)
  at android.app.ActivityThread.access$1800(ActivityThread.java:222)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1861)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:158)
  at android.app.ActivityThread.main(ActivityThread.java:7229)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
  </code></pre>
  
  <p>When trying it on a Galaxy S4 (5.0.1) it hangs for a while with a white screen, then crashes out and the monitor says:</p>
  
  <pre><code>06-30 23:56:50.718 2074-2074/com.gfaiers.hangman E/UncaughtException: java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
  at com.google.android.gms.common.internal.zzd$zza.zzc(Unknown Source)
  at com.google.android.gms.common.internal.zzd$zza.zzx(Unknown Source)
  at com.google.android.gms.common.internal.zzd$zze.zzasb(Unknown Source)
  at com.google.android.gms.common.internal.zzd$zzd.handleMessage(Unknown Source)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5951)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
  </code></pre>
  
  <p>... then it says</p>
  
  <pre><code>06-30 23:56:52.160 2074-2074/com.gfaiers.hangman E/AndroidRuntime: FATAL EXCEPTION: main
  Process: com.gfaiers.hangman, PID: 2074
  java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
  at com.google.android.gms.common.internal.zzd$zza.zzc(Unknown Source)
  at com.google.android.gms.common.internal.zzd$zza.zzx(Unknown Source)
  at com.google.android.gms.common.internal.zzd$zze.zzasb(Unknown Source)
  at com.google.android.gms.common.internal.zzd$zzd.handleMessage(Unknown Source)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5951)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
  06-30 23:56:52.290 3331-3331/com.gfaiers.hangman:background_crash D/ResourcesManager: creating new AssetManager and set to /data/app/com.gfaiers.hangman-1/base.apk
  06-30 23:56:52.700 3331-3331/com.gfaiers.hangman:background_crash E/InstantRun: IO Error creating local socket at com.gfaiers.hangman
  java.io.IOException: Address already in use
  at android.net.LocalSocketImpl.bindLocal(Native Method)
  at android.net.LocalSocketImpl.bind(LocalSocketImpl.java:306)
  at android.net.LocalServerSocket.&lt;init&gt;(LocalServerSocket.java:52)
  at com.android.tools.fd.runtime.Server.&lt;init&gt;(Server.java:94)
  at com.android.tools.fd.runtime.Server.create(Server.java:88)
  at com.android.tools.fd.runtime.BootstrapApplication.onCreate(BootstrapApplication.java:358)
  at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1020)
  at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5122)
  at android.app.ActivityThread.access$1600(ActivityThread.java:177)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1510)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:145)
  at android.app.ActivityThread.main(ActivityThread.java:5951)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
  </code></pre>
  
  <p>My code for the application is all available on <a href="https://github.com/gfaiers/Hangman/blob/master/app/src/main/java/com/gfaiers/hangman/MainActivity.java" rel="nofollow noreferrer">GitHub (MainActivity - <em>where the problems are</em>)</a>. </p>
  
  <p>Code that I've done so far from the guide (where I presume the issue is):</p>
  
  <pre><code>private GoogleApiClient mGoogleApiClient;
  private static int RC_SIGN_IN = 9001;
  private boolean mResolvingConnectionFailure = false;
  private boolean mAutoStartSignInFlow = true;
  private boolean mSignInClicked = false;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // Create the Google Api Client with access to the Play Games services
      mGoogleApiClient = new GoogleApiClient.Builder(this)
              .addConnectionCallbacks(this)
              .addOnConnectionFailedListener(this)
              .addApi(Games.API).addScope(Games.SCOPE_GAMES)
              // add other APIs and scopes here as needed
              .build();
      ...
  @Override
  public void onConnected(Bundle connectionHint) {
      // The player is signed in. Hide the sign-in button and allow the
      // player to proceed.
  }
  
  @Override
  public void onConnectionFailed(ConnectionResult connectionResult) {
      if (mResolvingConnectionFailure) {
          // already resolving
          return;
      }
  
      // if the sign-in button was clicked or if auto sign-in is enabled,
      // launch the sign-in flow
      if (mSignInClicked || mAutoStartSignInFlow) {
          mAutoStartSignInFlow = false;
          mSignInClicked = false;
          mResolvingConnectionFailure = true;
  
          // Attempt to resolve the connection failure using BaseGameUtils.
          // The R.string.sign_in_other_error value should reference a generic
          // error string in your strings.xml file, such as "There was
          // an issue with sign-in, please try again later."
          if (!BaseGameUtils.resolveConnectionFailure(this,
                  mGoogleApiClient, connectionResult,
                  RC_SIGN_IN, getResources().getString(R.string.sign_in_other_error))) {
              mResolvingConnectionFailure = false;
          }
      }
  
      // Put code here to display the sign-in button
  }
  
  @Override
  public void onConnectionSuspended(int i) {
      // Attempt to reconnect
      mGoogleApiClient.connect();
  }
  
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent intent) {
      if (requestCode == RC_SIGN_IN) {
          mSignInClicked = false;
          mResolvingConnectionFailure = false;
          if (resultCode == RESULT_OK) {
              mGoogleApiClient.connect();
          } else {
              // Bring up an error dialog to alert the user that sign-in
              // failed. The R.string.sign_in_failure should reference an error
              // string in your strings.xml file that tells the user they
              // could not be signed in, such as "Unable to sign in."
              BaseGameUtils.showActivityResultError(this,
                      requestCode, resultCode, R.string.sign_in_failure);
          }
      }
  }
  
  // Call when the sign-in button is clicked
  private void signInClicked() {
      mSignInClicked = true;
      mGoogleApiClient.connect();
  }
  
  // Call when the sign-out button is clicked
  private void signOutClicked() {
      mSignInClicked = false;
      Games.signOut(mGoogleApiClient);
  }
  </code></pre>
  
  <p>Code on GitHub all updated.</p>
  
  <p>I've also added to the BaseGameUtils build.gradle file:</p>
  
  <pre><code>...buildToolsVersion android_version
  
  defaultConfig {
      minSdkVersion 17
  }
  </code></pre>
  
  <p>This has seemed to be a solution for getting around build fail saying minSdkVersion 1 is too low for library version 7 etc etc.</p>
  
  <p><strong>Latest update</strong></p>
  
  <p>After doing a bit more reading, I found something which looks to be very simple as solution in the AndroidManifest of the app (not the lib).</p>
  
  <pre><code>&lt;!--&lt;meta-data android:name="com.gooogle.android.gms.games.APP_ID" android:value="@string/app_id"/&gt;--&gt;
  </code></pre>
  
  <p>I took out the comment marks, and it still displayed the above error "A fatal developer error".  I changed it to:</p>
  
  <pre><code>&lt;meta-data android:name="com.gooogle.android.gms.games.com.gfaiers.hangman" android:value="@string/app_id"/&gt;
  </code></pre>
  
  <p>Completely unsure if this is what I was meant to do, and it still did the same error.</p>
  
  <p>But reading into this further... it states:</p>
  
  <pre><code>07-01 22:15:02.196 24277-24277/com.gfaiers.hangman D/FirebaseCrashApiImpl: throwable java.lang.IllegalStateException: A fatal developer error has occurred. Check the logs for further information.
  </code></pre>
  
  <p>which would make me think it's the AdMob which is causing the crash?!</p>
  
  <p><strong>Requested local.properties file</strong></p>
  
  <pre><code>ndk.dir=D\:\\Users\\Geoff\\AppData\\Local\\Android\\Sdk\\ndk-bundle
  sdk.dir=D\:\\Users\\Geoff\\AppData\\Local\\Android\\Sdk
  </code></pre>
  
  <p><strong>Updates from answers so far</strong></p>
  
  <p>Changed dependencies to:</p>
  
  <pre><code>dependencies {
  compile fileTree(include: ['*.jar'], dir: 'libs')
  testCompile 'junit:junit:4.12'
  compile project(':BaseGameUtils')
  compile 'com.android.support:appcompat-v7:24.0.0'
  compile 'com.android.support:support-v4:24.0.0'
  compile 'com.google.firebase:firebase-ads:9.2.0'
  compile 'com.google.android.gms:play-services-ads:9.2.0'
  compile 'com.google.android.gms:play-services-auth:9.2.0'
  compile 'com.google.android.gms:play-services-gcm:9.2.0'
  compile 'com.google.android.gms:play-services-games:9.2.0'
  </code></pre>
  
  <p>Errors now appear as:</p>
  
  <pre><code>07-02 09:36:16.061 2133-7644/? E/Ads: Error transporting the ad response
  java.io.IOException: write failed: EPIPE (Broken pipe)
  at libcore.io.IoBridge.write(IoBridge.java:502)
  at java.io.FileOutputStream.write(FileOutputStream.java:186)
  at java.io.DataOutputStream.write(DataOutputStream.java:98)
  at java.io.OutputStream.write(OutputStream.java:82)
  at com.google.android.gms.ads.internal.request.ac.run(:com.google.android.gms:150)
  at java.lang.Thread.run(Thread.java:818)
  Caused by: android.system.ErrnoException: write failed: EPIPE (Broken pipe)
  at libcore.io.Posix.writeBytes(Native Method)
  at libcore.io.Posix.write(Posix.java:223)
  at libcore.io.BlockGuardOs.write(BlockGuardOs.java:313)
  at libcore.io.IoBridge.write(IoBridge.java:497)
  at java.io.FileOutputStream.write(FileOutputStream.java:186) 
  at java.io.DataOutputStream.write(DataOutputStream.java:98) 
  at java.io.OutputStream.write(OutputStream.java:82) 
  at com.google.android.gms.ads.internal.request.ac.run(:com.google.android.gms:150)
  at java.lang.Thread.run(Thread.java:818) 
  </code></pre>