Git Product home page Git Product logo

cloud-pubsub-grpc-android's Introduction

Cloud PubSub (gRPC) with Android

Following the gRPC announcement for Cloud PubSub, this Android sample publishes into a topic, and pulls messages fom a subscription. It takes some guidelines from here. The code can be executed on an Android device.

Setup

  1. Android Studio

  2. Gradle 2.12

  3. Android plugin for gradle

  4. Protobuf plugin for gradle 0.7.7

Important to have Gradle and the protobuf plugin exactly in the versions above (2.12 and 0.7.7 respectively). protoc must be in version 3. 7

PubSub gRPC dependencies

The following dependency needs to be added to build.gradle:

compile 'com.google.api.grpc:grpc-pubsub-v1:0.0.2'

Authentication

Authentications using gloud-java libraries are described here. Remember to set prereqs and enable PubSub API on your Google Cloud project.

Basically you have two authentication options from Android:

  • Use credentials of a service account: This requires to generate a JSON file from the console and add it to your apk. This has some security concerns, since anyone unpacking your apk would have access to the private key of your service account. If you still would like to proceed, you should place your the corresponding JSON file into assets/ directory and use the following code to get credentials.

    AssetManager am = mContext.getAssets();
    InputStream isCredentialsFile = am.open( YOUR_JSON_FILE_INSIDE_ASSETS_DIRECTORY );
    
    GoogleCredentials credential = GoogleCredentials.fromStream(isCredentialsFile);
    credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));
  • Use OAuth2, specifying the scopes as described here. You get a token without compromising security by saving any key inside the apk. The token should be generated as follows:

    String scopesString = "https://www.googleapis.com/auth/pubsub";
    String SCOPE = "oauth2:" + scopesString;
    
    token = GoogleAuthUtil.getToken(
            mContext,     // Context of your Main activity
            mAccount,     // Account name with permissions to PubSub and your cloud proyect
            SCOPE         // String scope
    );
    
    GoogleCredentials credential =  new GoogleCredentials( new AccessToken( token, null) );
    credential = credential.createScoped(Arrays.asList("https://www.googleapis.com/auth/pubsub"));

Java protobuf nano implementation with Pubsub

By default, the protobuf plugin for gradle always uses javanano implementation in Android. Pubsub.proto uses proto3 built-in types (empty.proto and annotations.proto) which are not included on the javanano implementation. This is properly handled by the plugin, however, it causes some duplicated dependencies which needed to be resolved by excluding the duplications on the packagingOptions block on build.gradle

Screenshots

Main activity:

alt text

alt text

cloud-pubsub-grpc-android's People

Contributors

rafaelsf80 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.