Quick Start Guide

Follow this guide to set up a project for Xamarin.Android in order to easily implement the Anyline SDK.

License API Migration to Anyline 26 and beyond

Starting with Anyline 26, we changed our point of initialization for the Anyline license in order to streamline our initialization process. Simply put the Anyline license check once globally in your application and then initialize any of the components (ScanView, ScanPlugins, etc.) without the license key parameter.

The Xamarin Bundle

The Xamarin SDK Bundle that you can download from github contains the following parts for you to get started in Xamarin:

  • BindingSource - Xamarin iOS and Android Binding Libraries
  • Examples - Xamarin.iOS, Xamarin.Android and Xamarin.Forms example apps
  • com.anyline.xamarin.examples_<version>.apk - Prebuilt Android APK ready to install on your Android device

Requirements

To use the Anyline SDK for Xamarin.Android you need:

  • An Android device with SDK >= 21
  • Decent camera functionality (recommended: 720p and adequate auto focus)
  • Visual Studio 2017 as an IDE

Setup

In this section we will go through the basic process of creating and configuring a simple Anyline scanning application in Xamarin.Android. If you are not yet familiar with Xamarin.Android, follow the Official Xamarin Android Quickstart Guide to develop an understanding of the fundamentals of Android application development with Xamarin.

Create a new Xamarin.Android Project

If you are using Visual Studio, click File > New Project…, select Visual C# > Android and create a new Blank App.

Create a Xamarin Android Project in Visual Studio

Generate an Anyline License Key

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 Xamarin.Android app.

License <> ApplicationId

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

To generate a license key for your application, refer to the package name of your Xamarin.Android project

Identify the Application ID in Visual Studio

Generate the License

With the applicationId you are now able to Generate a License.

Once you generated your license key for the package name of your app, you can integrate it as follows:

Integrate the License Key

In order to integrate your generated license key, you can either add the license key as string to your .cs code-behind of your scanning activity, or create a string resource under ResourcesValuesStrings.xml and reference it.

In your C# code-behind
//INSERT YOUR LICENSE KEY HERE
public static readonly string LICENSE_KEY = "YOUR_LICENSE_KEY";
From a resource string
<resources>
        <string name="license_key">YOUR_LICENSE_KEY</string>
</resources>

Add Anyline XamarinSDK.Droid via NuGet

To access the functionality of our SDK, simply right-click on your project, go to manage Nuget packages and search for “Anyline”.

Select Anyline.Xamarin.SDK.Droid and install the latest available version.

Accept the license agreement and you’re ready to use Anyline!

Set minimum Android target

In the Application settings of your project, set the minimum version of your Android target to API level 21.

The Android API Level in your Xamarin Project

Add necessary permissions and features

The scanning activity needs the following permissions and features:

Permissions
  • CAMERA
  • VIBRATE
Features
  • android.hardware.camera
  • android.hardware.camera.flash
  • android.hardware.camera.autofocus

This is how you can add them to your AndroidManifest.xml in your Properties node:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="AT.Anyline.Xamarin.App.Droid" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
        <uses-sdk />
  <!--add android:hardwareAccelerated="true" for Camera2 API support-->
  <application android:label="Anyline Examples (Xamarin.Android)" android:icon="@drawable/ic_launcher" android:theme="@style/AppTheme"
                           android:hardwareAccelerated="true">
  </application>
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-permission android:name="android.permission.VIBRATE" />
        <uses-feature android:name="android.hardware.camera" />
        <uses-feature android:name="android.hardware.camera.flash" android:required="false" />
        <uses-feature android:name="android.hardware.camera.autofocus" android:required="false" />
</manifest>

Additionally, you should use this code snippet in your OnCreate() method of your MainActivity to ensure the capability:

int REQUEST_CAMERA = 0;

if (ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.Camera) != Permission.Granted)
{
    // Should we show an explanation?
    if (!ActivityCompat.ShouldShowRequestPermissionRationale(this, Android.Manifest.Permission.Camera))
    {
        // No explanation needed, we can request the permission.
       ActivityCompat.RequestPermissions(this, new string[] { Android.Manifest.Permission.Camera }, REQUEST_CAMERA);
    }
}
else
{
    System.Diagnostics.Debug.WriteLine("Permission Granted!");
}

Camera API & Hardware Acceleration

Since newer devices might use the Android Camera2 API, it’s important to make sure that hardware acceleration is enabled in the activity that implements Anyline. This can be set as seen above through the AndroidManifest, but better yet it should be set directly in the C# code of the activity like so: [Activity(Label = "My Activity", HardwareAccelerated = true)]

Dex Compiler

Starting from SDK 21, Anyline uses Java 8 which requires Xamarin.Android projects to be compiled using the “d8” Dex compiler. To set this compiler, go to the project Properties and, under the “Android Options” tab, select the option “d8” in the Dex compiler. If the option is not available on Visual Studio (older versions), you can also add the following line directly on the .csproj file for both Debug and Release groups: <AndroidDexTool>d8</AndroidDexTool>

Identify the Application ID in Visual Studio

Initialize the SDK

Initialize the Anyline SDK in your ViewController, or in another place your preference. The SDK only needs to be initialized once, before the first usage of the scan plugins.

MainActivity - OnCreate

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);

    ...

    // INSERT YOUR LICENSE KEY HERE
    string licenseKey = "YOUR_LICENSE_KEY";

    // INITIALIZE THE ANYLINE SDK
    IO.Anyline.AnylineSDK.Init(licenseKey, this);
}

In the next section, we will implement a ScanView and provide a configuration to customize visual parameters and behaviours.