Skip to content

Quickstart for integrating Approov with Android apps in Java that make API requests you wish to protect using GRPC.

License

Notifications You must be signed in to change notification settings

approov/quickstart-android-java-grpc

Repository files navigation

Approov Quickstart: Android Java GRPC

This quickstart is written specifically for native Android apps that are written in Java and use GRPC-Java for making the API calls that you wish to protect with Approov. If this is not your situation then check if there is a more relevant Quickstart guide available.

This page provides all the steps for integrating Approov into your app. Additionally, a step-by-step tutorial guide using our Shapes App Example is also available.

To follow this guide you should have received an onboarding email for a trial or paid Approov account.

ADDING APPROOV SERVICE DEPENDENCY

The Approov integration is available via jitpack. This allows inclusion into the project by simply specifying a dependency in the gradle files for the app.

Firstly, jitpack needs to be added to the end the repositories section in the build.gradle file at the top level of the project:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

Secondly, add the dependency in your app's build.gradle:

dependencies {
	 implementation 'com.github.approov:approov-service-android-java-grpc:3.2.2'
}

Make sure you do a Gradle sync (by selecting Sync Now in the banner at the top of the modified .gradle file) after making these changes.

This package is actually an open source wrapper layer that allows you to use Approov easily with GRPC-Java. It has a further dependency to the closed source Approov SDK.

MANIFEST CHANGES

The following app permissions need to be available in the manifest to use Approov:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

Note that the minimum SDK version you can use with the Approov package is 21 (Android 5.0).

Please read the Targeting Android 11 and Above section of the reference documentation if targeting Android 11 (API level 30) or above.

INITIALIZING APPROOV SERVICE

In order to use the ApproovService you must initialize it when your app is created, usually in the onCreate method:

import io.approov.service.grpc.ApproovService;

public class YourApp extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        ApproovService.initialize(getApplicationContext(), "<enter-your-config-string-here>");
    }
}

The <enter-your-config-string-here> is a custom string that configures your Approov account access. This will have been provided in your Approov onboarding email.

This initializes Approov when the app is first created. ApproovService is a static class which makes its methods available to call from anywhere in your code. All calls to ApproovService and the SDK itself are thread safe.

USING APPROOV SERVICE

You can then create secure pinned GRPC channels by using the ApproovChannelBuilder instead of the usual ManagedChannelBuilder:

import io.approov.service.grpc.ApproovChannelBuilder;
...

    // open GRPC managed channel
    String host = "grpc.example.com";
    int port = 443;
    ManagedChannel channel = ApproovChannelBuilder.forAddress(host, port).build();

Add Approov enabled remote procedure call stubs by adding an ApproovClientInterceptor which adds the Approov-Token header and may also substitute header values when using secrets protection:

import io.approov.service.grpc.ApproovClientInterceptor;
...

    // Get calling stub
    ExampleGrpc.ExampleBlockingStub stub = ExampleGrpc.newBlockingStub(channel);
    stub = stub.withInterceptors(new ApproovClientInterceptor(channel));

Approov errors will generate an ApproovException, which is a type of IOException. This may be further specialized into an ApproovNetworkException, indicating an issue with networking that should provide an option for a user initiated retry.

CHECKING IT WORKS

Initially you won't have set which API domains to protect, so the interceptor will not add anything. It will have called Approov though and made contact with the Approov cloud service. You will see logging from Approov saying UNKNOWN_URL.

Your Approov onboarding email should contain a link allowing you to access Live Metrics Graphs. After you've run your app with Approov integration you should be able to see the results in the live metrics within a minute or so. At this stage you could even release your app to get details of your app population and the attributes of the devices they are running upon.

NEXT STEPS

To actually protect your APIs and/or secrets there are some further steps. Approov provides two different options for protection:

  • API PROTECTION: You should use this if you control the backend API(s) being psrotected and are able to modify them to ensure that a valid Approov token is being passed by the app. An Approov Token is short lived crytographically signed JWT proving the authenticity of the call.

  • SECRETS PROTECTION: This allows app secrets, including API keys for 3rd party services, to be protected so that they no longer need to be included in the released app code. These secrets are only made available to valid apps at runtime.

Note that it is possible to use both approaches side-by-side in the same app.

See REFERENCE for a complete list of all of the ApproovService methods.

About

Quickstart for integrating Approov with Android apps in Java that make API requests you wish to protect using GRPC.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages