Muhammad Sufyan March 18, 2022

Your mobile/Android application can talk to any API with the low-code approach through client SDKs. Client SDKs are independent internal libraries for your application that focus on the end-to-end flow of an API. They cut down the manual process of writing a whole client and related components, providing you with the best developer experience, and increasing API adoption. You just have to import an auto-generated end-to-end SDK and focus on the core business logic in your application.

APIMatic is a developer experience platform that provides automatic code generation for API specifications, in multiple popular languages. In this blog, we will be discussing how to integrate Java SDKs into Android applications.

Why Java Libraries Instead of Android Native Libraries?

An AAR (i.e. Android ARchive) file can be included in the build process of an Android application similar to a JAR (i.e. Java ARchive) file. Both can serve the same purpose but the main difference between a JAR and an AAR is that AARs can include resources such as layouts, drawables, and more.

For an API client SDK, you don't need Android components and resources, but some classes, making the library lightweight that developers can quickly interact with to call your APIs, making a JAR library a better option than AAR.

Another advantage of using Java libraries is that JAR can be used for IDEs like eclipse and Android Studio without any complicated steps. Also, JAR, in contrast to AAR, can be used with any Java application which makes it technology-independent in terms of Java-specific applications like web, desktop, and Android apps.

Inside the Java SDK

APIMatic SDKs adhere to the best coding practices and standards and support the latest dependencies and versions for full coverage. The Java SDK uses Maven as its project management tool which allows it to adjust itself in the Maven ecosystem. 

In an Android application, the Gradle build system makes it easier to include external binaries and other library modules to your build as dependencies. These dependencies can be located on your machine or in a remote repository, and any transitive dependencies declared are automatically included as well.

Having Gradle support of adding Maven dependencies directly makes it easier for developers to include Maven projects like an APIMatic SDK as a dependency. This enables you to consume Maven libraries for any application built on top of Java or Kotlin languages. APIMatic Java SDKs have this ability and allow you to accomplish what you need with some clicks.

In this blog, we will use our public Calculator SDK as an example to demonstrate how to integrate Java SDKs into Android applications. The Calculator SDK contains simple operations like addition, multiplication, subtraction, division, and a README file that walks you through the setup installation and highlights the documentation of the modules involved. The published Java SDK has the following Maven identifiers:

Group-id: io.github.apimatic
Artifact-id: sample-calculator-sdk
Version: 1.0.0

How to Integrate Java SDKs into Android Applications?

There are two ways to integrate Java SDKs into Android applications:

  • Declare the dependency directly using groupId, artifact, and version
  • Utilize the GUI tool provided by Android Studio

The source of the artifact could be one of the following:

  • A Remote Artifact (Gradle will resolve the artifact from Maven central repository)
  • A Local Artifact (Gradle will resolve the artifact from the local Maven repository)

Import Remote Dependency

Note: A prerequisite to importing the remote dependency is that the Java SDK should already be published on the Maven repository. In our case, the calculator SDK is published on Maven Central under the group id io.github.apimatic .

Option 1: Add Dependency Using Android Studio

Follow these steps to include the dependency:

  1. In the Android project structure, select Dependencies from the left navigation section. Select the target module and click on Library Dependency as shown:  Dependencies
  2. In the subsequent dialog box, search for the group id io.github.apimatic. Configure your dependency if required (labeled 2) and click OK.Configure your dependency
  3. As the last step, click on Apply and OK. Gradle will start syncing.

Option 2: Adding the Dependency Directly 

You can also add the dependency directly in the respective module’s build.gradle file. This can be achieved by setting a lookup configuration for Maven Central in Dependency Resolution Management through the root level build file, i.e. build.gradle, shown below:

buildscript {
... repositories { ... MavenCentral() ... } ...
}

Add the artifact information like groupId, artifactId, and version in the dependencies section of the respective module, for example:

dependencies {
    ...
    implementation 'io.github.apimatic:sample-calculator-sdk:1.0.0'
    ...
}
Follow the proper format for Maven Central as prescribed in Gradle official documentation . Read more on Gradle Version Ranging .  

Import Local Dependency

Gradle can consume dependencies available in the local Maven repository (i.e. in-home directory under .m2 directory). The target artifact should already exist in the local Maven repository. In order to achieve this, use the following Maven build command to build the project and have a snapshot in the local repository:

mvn clean install

This will make the artifact locally available. Now, it can be added as a dependency or external JAR.

Option 1: Add Dependency as External JAR

  1. Locate the JAR file in the system. Copy and paste the SDK JAR file in libs directory of your application.Locate the JAR file
  2. Now right click on the copied JAR file and click Add As Library.Add As Library
  3. Once added, a popup will appear to confirm the target module, select the module and click OK.select the module

Option 2: Add Dependency Directly

Allow Gradle to search in the local Maven repository by adding mavenLocal() configuration in the settings.gradle file (i.e. project/root level Gradle settings).

buildscript {
    ...
    repositories {
        ...
        mavenLocal()
        ...
    }
    ...
}

Add the dependency in build.gradle (i.e. module-level Gradle settings) as shown:

dependencies {
    ...
    implementation 'io.github.apimatic:sample-calculator-sdk:1.0.0'
    ...
}
The dependency format is in the form of <groupId:artifactId:version> but it can be applied through different formats as prescribed on Gradle official documentation . Gradle also supports version ranging.

Once Gradle is synchronized, the application is ready to consume the added SDK dependency.

Using the Imported SDK in Java and Kotlin

Once the dependency resolution is done, you can start using the SDK components in your application. 

Java Code Sample

The following code sample in Java shows how easily you can initialize the client and use the Multiply endpoint of the integrated Calculator SDK in the Android application:

// client initialization in Java

APIMATICCalculatorClient client = new APIMATICCalculatorClient.Builder()
    .httpClientConfig(configBuilder -> configBuilder
            .timeout(0))
    .environment(Environment.PRODUCTION)
    .build();

// Preparing and Calling API Endpoint

GetCalculateInput getCalculateInput = new GetCalculateInput();
getCalculateInput.setOperation(OperationTypeEnum.MULTIPLY);
getCalculateInput.setX(222.14);
getCalculateInput.setY(165.14);

client.getSimpleCalculatorController()
        .getCalculateAsync(getCalculateInput)
            .thenAccept(result -> {
                // TODO success callback handler
            }).exceptionally(exception -> {
                // TODO failure callback handler
                return null;
            });

Kotlin Code Sample

The following code sample in Kotlin shows how easy it is to initialize the client and use the Multiply endpoint of the integrated Calculator SDK in your Android application:

// client initialization in Kotlin

var client: APIMATICCalculatorClient = APIMATICCalculatorClient.Builder()
    .httpClientConfig { configBuilder: HttpClientConfiguration.Builder ->
        configBuilder
            .timeout(0)
    }
    .environment(Environment.PRODUCTION)
    .build()

// Preparing and Calling API Endpoint

val getCalculateInput = GetCalculateInput()
getCalculateInput.operation = OperationTypeEnum.MULTIPLY
getCalculateInput.x = 222.14
getCalculateInput.y = 165.14

client.simpleCalculatorController.getCalculateAsync(getCalculateInput)
   .thenAccept { result ->
       // TODO success callback handler
   }
   .exceptionally {
       // TODO failure callback handler
       null
   }

Conclusion

APIMatic Java SDKs are fully compliant with the Android app environment making app development quicker. With APIMatic, you can focus on the business domain and leave the server or third-party communication to APIMatic SDKs. APIMatic excels in providing a complete developer experience with language-specific documentation, interactive API portals, and SDKs with end-to-end test coverage.