Showing posts with label Google client API. Show all posts
Showing posts with label Google client API. Show all posts

Tuesday, November 22, 2016

Google Play services and Firebase for Android will support API level 14 at minimum

Posted by Doug Stevenson, Developer Advocate


Version 10.0.0 of the Google Play services client libraries, as well as the
Firebase client libraries for Android, will be the last version of these
libraries that support href="https://source.android.com/source/build-numbers.html">Android API
level 9 (Android 2.3, Gingerbread). The next scheduled release of these
libraries, version 10.2.0, will increase the minimum supported API level from 9
to 14 (Android 4.0.1, Ice Cream Sandwich). This change will happen in early
2017.


Why are we discontinuing support for Gingerbread and Honeycomb in Google
Play services?



The Gingerbread platform is almost six years old. Many Android developers have
already discontinued support for Gingerbread in their apps. This helps them
build better apps that make use of the newer capabilities of the Android
platform. For us, the situation is the same. By making this change, we will be
able to provide a more robust collection of tools for Android developers with
greater speed.


What this means for your Android app that uses Google Play services or
Firebase:



You may use version 10.0.0 of Google Play services and Firebase as you are
currently. It will continue to work with Gingerbread devices as it has in the
past.



When you choose to upgrade to the future version 10.2.0, and if your app
minimally supports API level 14 or greater (typically specified as
"minSdkVersion" in your build.gradle), you will not encounter any versioning
problems. However, if your app supports lower than API level 14, you will
encounter a problem at build time with an error that looks like this:


class="prettyprint">Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : uses-sdk:minSdkVersion 9 cannot be smaller than version 14 declared in library [com.google.android.gms:play-services:10.2.0]
Suggestion: use tools:overrideLibrary="com.google.android.gms:play_services" to force usage


Unfortunately, the stated suggestion will not help you successfully run your app
on older devices. In order to use Google Play services 10.2.0 and later, you
can choose one of the following options:


1. Target API level 14 as the minimum supported API level.



This is the recommended course of action. To discontinue support for API levels
that will no longer receive Google Play services updates, simply increase the
minSdkVersion value in your app's build.gradle to at least 14. If you update
your app in this way and publish it to the Play Store, users of devices with
less than that level of support will not be able to see or download the update.
However, they will still be able to download and use the most recently published
version of the app that does target their device.



A very small percentage of all Android devices are using API levels less than
14. You can href="https://developer.android.com/about/dashboards/index.html?utm_campaign=firebase_discussion_apilevel_112116&utm_source=anddev&utm_medium=blog">read more about
the current distribution of Android devices. We believe that many of these
old devices are not actively being used.



If your app still has a significant number of users on older devices, you can
use multiple APK support in Google Play to deliver an APK that uses Google Play
services 10.0.0. This is described below.


2. Build multiple APKs to support devices with an API level less than
14.



Along with some configuration and code management, you can href="https://developer.android.com/training/multiple-apks/api.html?utm_campaign=firebase_discussion_apilevel_112116&utm_source=anddev&utm_medium=blog">build
multiple APKs that support different minimum API levels, with different
versions of Google Play services. You can accomplish this with href="https://developer.android.com/studio/build/build-variants.html?utm_campaign=firebase_discussion_apilevel_112116&utm_source=anddev&utm_medium=blog">build
variants in Gradle. First, define build flavors for legacy and newer
versions of your app. For example, in your build.gradle, define two different
product flavors, with two different compile dependencies for the components of
Play Services you're using:


class="prettyprint">productFlavors {
legacy {
minSdkVersion 9
versionCode 901 // Min API level 9, v01
}
current {
minSdkVersion 14
versionCode 1401 // Min API level 14, v01
}
}

dependencies {
legacyCompile 'com.google.android.gms:play-services:10.0.0'
currentCompile 'com.google.android.gms:play-services:10.2.0'
}


In the above situation, there are two product flavors being built against two
different versions of the Google Play services client libraries. This will work
fine if only APIs are called that are available in the 10.0.0 library. If you
need to call newer APIs made available with 10.2.0, you will have to create a
compatibility library for the newer API calls so that they are only built into
the version of the application that can use them:


  • Declare a Java interface that exposes the higher-level functionality you
    want to perform that is only available in current versions of Play services.
  • Build two Android libraries that implement that interface. The "current"
    implementation should call the newer APIs as desired. The "legacy"
    implementation should no-op or otherwise act as desired with older versions of
    Play services. The interface should be added to both libraries.
  • Conditionally compile each library into the app using "legacyCompile" and
    "currentCompile" dependencies.
  • In the app's code, call through to the compatibility library whenever newer
    Play APIs are required.


After building a release APK for each flavor, you then publish them both to the
Play Store, and the device will update with the most appropriate version for
that device. Read more about href="https://developer.android.com/google/play/publishing/multiple-apks.html?utm_campaign=firebase_discussion_apilevel_112116&utm_source=anddev&utm_medium=blog">multiple
APK support in the Play Store.

Thursday, December 3, 2015

API Updates for Sign-In with Google

Posted by Laurence Moroney



With the release of Google Play services 8.3, we’ve made a lot of improvements to Sign-In with Google. In the first blog post of this ongoing series, we discussed the user interface improvements. Today, we will look further into the changes to the API to make building apps that Sign-In with Google easier than ever before.



Changes to basic sign in flow


When building apps that sign in with Google, you’ll notice that there are a lot of changes to make your code easier to understand and maintain.



Prior to this release, if you built an app that used Sign-In with Google, you would build one that attempted to connect to a GoogleApiClient. At this point the user was presented with an account picker and/or a permissions dialog, both of which would trigger a connection failure. You would have to handle these connection failures in order to sign in. Once the GoogleApiClient connected, then the user was considered to be signed in and the permissions could be granted. The process is documented in a CodeLab here.



Now, your code can be greatly simplified.



The process of signing in and connecting the GoogleApiClient are handled separately. Signing in is achieved with a GoogleSignInOptions object, on which you specify the parameters of the sign in, such as scopes that you desire. Here’s a code example:



 GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)  
.requestEmail()
.build();


Once you have a GoogleSignInOptions object, you can use it to configure the GoogleApiClient:



Here’s where your code will diverge greatly in the new API. Now, if you want to connect with a Google Account, instead of handling errors on the GoogleApiClient, you’ll instead use an intent that is initialized using the client.



Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);


Starting the intent will give you the account picker, and the scopes permission dialog if your GoogleSignInOptions requested anything other than basic scope. Once the user has finished interacting with the dialogs, an OnActivityResult callback will fire, and it will contain the requisite sign-in information.




 @Override  
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleSignInResult(result);
}
}



You can learn more about this code in the Integrating Google Sign-In quickstart, or by looking at the sample code.



Silent Sign-In


To further reduce friction for users in a multi-device world, the API supports silent sign in. In this case, if your user has given authorization to the app on a different device, the details will follow their account, and they don’t need to re-give them on future devices that they sign into, unless they deauthorize. An existing sign-in is also cached and available synchronously on the current device is available.



Using it is as simple as calling the silentSignIn method on the API.



OptionalPendingResult opr = 
Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);


Then, you can check the isDone() method on the pending result -- and if it returns true, the user is signed in, and you can get their status from the PendingResult. If it isn’t then you have to wait for a callback with the SignInResult




 if (pendingResult.isDone()) {  
doStuffWith(pendingResult.get());
} else {
// There's no immediate result ready, displays some progress indicator and waits for the
// async callback.
showProgressIndicator();
pendingResult.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(@NonNull GoogleSignInResult result) {
updateButtonsAndStatusFromSignInResult(result);
hideProgressIndicator();
}
});
}


Customizing the Sign-In Button


When building apps that Sign-In with Google, we provide a SignInButton object that has the Google branding, and which looks like this:






You can customize this button with a number of properties and constants that are documented in the API here.



The branding guidelines are available here, and they include versions of the buttons in PNG, SVG, EPS and other formats in many resolutions, including all the different states of the button. These files may be used for localization of the button, and if you need to match the style of the button to your app, guidelines are provided.



This only deals with the Android side of your app. You’ll likely have a server back end, and you may want to use the credentials on that securely.



In the next blog post, we’ll discuss how to use Sign-In with Google in server-side scenarios, including how to securely pass your credentials to your own server, and how to use Google back-end services, such as Google Drive, with the permission and credentials from users.