Getting Started

Requirements

In order to be able to use the Anyline SDK, only two requirements have to be met:

  • An Android device with Android SDK Level >= 21

  • An Android device with decent camera functionality (recommended: 720p and adequate auto focus)

If you are migrating from an Anyline version lower than 43, please also see Migrating to Anyline 43.0.0.

The Anyline SDK Developer Examples

If you rather like to jump into some code than to walk through a Quick Start Guide, a good starting point for development with the Anyline SDK, is to clone the Developer Examples for Android GitHub Repository.

It includes the following:

  • The source code of the Anyline OCR Examples App

  • The third party licenses

  • A Readme

Example Sheets for Mobile Scanning can be found here: Anyline Example Sheets

Quick Start Guide

This guide is going to lead you through the first steps when implementing the Anyline SDK on Android.

Generate an Anyline License

In order to run the Anyline SDK in your app, you require a license key.

In order to create a license key for your application, you have to identify the applicationId of your Android app.

  • Every license is bound to an applicationId. If you change your applicationId, you will require a new license. This also ensures that your license key cannot be used in any other application.

How to identify the Application ID

In your build.gradle file

On Android, the applicationId can be found in the build.gradle file of your app.

It is listed there under android > defaultConfig > applicationId

plugins {
    id 'com.android.application'
}
android {
    compileSdk 33

    defaultConfig {
        applicationId "io.anyline.examples.snapshot"
        minSdk 21
        targetSdk 33
        versionCode 200
        versionName "3.0"
    }
}

In your AndroidManifest.xml

If the applicationId is not used in the build.gradle file, you may use the package name of your application that is defined in the AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.anyline.examples">
</manifest>
With the applicationId you are now able to Generate a License.

Add AnylineSDK as dependency

The Anyline SDK for Android is provided as an .aar library.

From maven

To incorporate the Anyline SDK into your Android project effortlessly, follow these steps:

Step 1: Update build.gradle

Navigate to the root section of your app-level build.gradle file and add the Anyline Maven repository:

repositories {
    // Add the Anyline Maven repository
    maven { url 'https://mobile-sdk-android.anyline.io/release/'}
}
Step 2: Specify Anyline SDK Dependency

Inside the dependencies block of your app-level build.gradle file, include the Anyline SDK as a dependency. Replace LATEST_SDK_VERSION with the desired version, or use 54.6.0 for the latest version:

dependencies {
    // Include Anyline SDK as a dependency
    implementation 'io.anyline:anylinesdk:LATEST_SDK_VERSION'

    //... Add your other dependencies
}

This approach simplifies version management, allowing you to switch between versions effortlessly without manually downloading the updated SDK.

Step 3: Sync Project

After updating your build.gradle file, sync your project to apply the changes.

Now, you have successfully added the Anyline SDK to your Android project, enabling you to leverage its features seamlessly.

If you require to include the Anyline SDK as a local library in your project, please see Adding Anyline Mobile SDK for Android as a local dependency instead.

Integrate the License Key

A License Key string will be required in order to run the Anyline Mobile SDK on your app.

Store the License Key

You can integrate your license key string in a way that best suits your development workflow.

As the integrator, you are responsible for managing your license key securely within your application. We strongly recommend using platform-specific secure storage solutions such as the Keychain for iOS and the Keystore for Android. Alternatively, consider implementing Dynamic Delivery to retrieve the license key at runtime from a secure server. These approaches help ensure the protection of your license key and prevent unauthorized access.

For simplicity reasons, the approach outlined below can be used in order to quickly get the SDK up and running.

Add the License Key as a Resource

You can add your License Key to a string resource file, or put it hard-coded into your Java file. However, we recommend to add a separate resource file (e.g. anyline_license_key.xml) to your res/values folder.

This should look like the following:

<resources>
	<string name="anyline_license_key" translatable="false">YOUR_LICENSE_KEY</string>
</resources>

Load the License Key in your Activity

In the Activity, where you initialise the Anyline SDK, you can access the License Key Resource the following way:

String licenseKey = getString(R.string.anyline_license_key);

Initialize the Android SDK with the License Key

Initialize the Anyline SDK before you do anything related to Anyline and handle the exception. For a full explanation of the available initialization strategies, please follow How to Initialize Anyline Mobile SDK.

  • Kotlin

  • Java

class MainApplication: Application() {
    override fun onCreate() {
        super.onCreate()

        // This must be called before doing anything Anyline-related!
        AnylineSdk.init(this, SdkInitializationConfig(
            sdkInitializationParameters = SdkInitializationParameters(licenseKey),
            sdkInitializationStrategy = SdkInitializationStrategy.AsyncAuto(object: SdkInitializationListener {
                override fun onInitializationFailed(state: SdkInitializationState.NotInitialized) {
                    Log.e("AnylineSdkInit", "Anyline SDK not initialized: ${state.lastError?.exception?.message}")
                }

                override fun onInitializationSucceeded(state: SdkInitializationState.Initialized) {
                    Log.d("AnylineSdkInit", "Anyline SDK initialized!")
                }
            }))
        )
    }
}
public class MainApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        try {
            // This must be called before doing anything Anyline-related!
            AnylineSdk.init(this, new SdkInitializationConfig(
                    new SdkInitializationParameters(licenseKey),
                    new SdkInitializationStrategy.AsyncAuto(new SdkInitializationListener() {
                        @Override
                        public void onInitializationStarted() {
                            Log.d("AnylineSdkInit", "Anyline SDK initialization in progress...");
                        }

                        @Override
                        public void onInitializationFailed(@NonNull SdkInitializationState.NotInitialized notInitialized) {
                            String errorMessage = "";
                            SdkInitializationError lastError = notInitialized.getLastError();
                            if (lastError != null) {
                                errorMessage = lastError.getException().getMessage();
                            }
                            Log.e("AnylineSdkInit", "Anyline SDK not initialized: " + errorMessage + ".");
                        }

                        @Override
                        public void onInitializationSucceeded(@NonNull SdkInitializationState.Initialized initialized) {
                            Log.d("AnylineSdkInit", "Anyline SDK initialized!");
                        }
                    }, null)));
        } catch (Exception e) {
            Log.e("AnylineSdkInit", "Anyline SDK not initialized: " + e.getMessage() + ".");
        }
    }
}

Handling Camera permission

The Anyline SDK uses the camera for scanning. Your application manifest must declare permission to use the camera.

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

If your application did not request the permission before loading ScanView, it may do so during its loading, as long as:

  • It was instantiated in the container creation scope (e.g. onCreate())

  • The context provided to ScanView must inherit from AppCompatActivity.

Even if these two conditions are not met, there will be no failure as long as your application requested the camera permission and it was properly granted by the user before instantiating a ScanView.

If both conditions are met and you choose to have ScanView handle camera permission, you must wait for the event onScanViewLoaded to occur before performing any operations with ScanView. For more details, please see Initialize the ScanView.

Reduce App size

By default, the Anyline SDK ships trained models for all of our supported use cases. This will increase your app size significantly, but there is a simple way to remove the assets you do not need.

For a guide on how to do so, please follow Reduce SDK Size.

Load a Plugin and start scanning

Now you are all set to load one of the Anyline SDK Plugins and start scanning.

The plugins on Android are described in detail at Plugins in the next section.