Integrate D1 SDK using Xamarin
Overview
Xamarin is a cross-platform, open source app platform for building Android and iOS apps using .NET and C#.
To integrate D1 SDK using Xamarin, refer to the following sections:
Integrate a native library into a Xamarin.Forms app
To use a native Android and iOS library or component in a multiplatform Xamarin.Forms App, multiple layers need to be created to access the native APIs on multiple platforms.
General C# library
To access the native API for Android and iOS in a Xamarin.Forms App in a multiplatform manner, you have to create a General C# Library, containing all the required APIs to implement in the native Android and iOS platforms. This General C# Library will then be accessed directly from the Xamarin.Forms App.
Android platform
Android Bindings Library
On the Android platform, firstly create an Android Bindings Library. The Android Bindings Library is an assembly containing Managed Callable Wrappers for Java types. To develop an Android Xamarin application, you can use this Android Bindings Library without the need to create additional layers (that is, General C# Library and Android Class Library).
Android Class Library
To use the Android Bindings Library in a Xamarin.Forms App, you have to create an Android Class Library which will be the intermediate layer between the Android Bindings Library and the General C# Library.
iOS platform
iOS Proxy Framework
The iOS Binding Library is only able to access Objective-C classes or Swift classes which are either annotated with the objc attribute or are subclassed from the NSObject class. In the case a pure Swift framework is used, you have to wrap this framework to an Objective-C framework to be able to create an iOS Binding Library.
iOS Bindings Library
Subsequently, you can create an iOS Bindings Library and wrap the iOS framework (either original framework or the proxy framework in case a pure Swift framework is used). To develop an iOS Xamarin application, you can use this iOS Bindings Library directly without the need to create additional layers (General C# Library and iOS Class Library).
iOS Class Library
To use the iOS Bindings Library in a Xamarin.Forms App, you have to create an iOS Class Library which will be the intermediate layer between the iOS Bindings Library and the General C# Library.
Integrate D1 SDK into a Xamarin.Forms app
The wrapping of the configure and login D1 API calls will be described in the following sections.
This guide was tested with:
D1 SDK
3.2.0 Android
3.2.0 iOS
Visual Studio Community 2022 for Mac Preview (Version 17.6 Preview (17.6 build 1569)
Runtime .NET 7.0.3 (64-bit)
Xamarin.Android Version: 13.2.0.6 (Visual Studio Community)
Xamarin.iOS Version: 16.4.0.6 Visual Studio Community
Xcode 14.3.1
General C# Library
This library will be directly accessed from the Xamarin.Forms App. It defines the general interfaces which are implemented by the Android Class Library and iOS Class Library.
In Visual Studio, select File > New Project > Multiplatform > Library > General > Class Library to create a new class library.
In Target framework, select
.NET Standard 2.1.Add the general interfaces.
Android
The Android part consists of:
Android Bindings Library
Android class Library
Android Bindings Library
In Visual Studio, select File > New Project > Android > Library > Bindings Library to create a new Android Bindings Library project.
In the Project explorer, right click on the Jars folder, select Add/Existing Files... and select both
d1-debug.aarandd1-release.aarfiles to add to the newly created Android Bindings Library project.Configure the build settings for Debug and Release by opening the project
*.csprojfile in some arbitrary text editor and modify it to used1-debug.aarfor the Debug build andd1-release.aarfor the Release build.
Update the obfuscated source code and other rules.
As Xamarin is unable to bind classes and methods which are dependent (extend or take as parameter) on the obfuscated source code,the obfuscated classes have to be marked as obfuscated in the
Metadata.xmlfile. In addition, there may be certain naming conflicts which have to be resolved.The following Android binding configuration was tested with D1 SDK 2.4.0.
Android Class Library
The Android Class Library wraps the Android Bindings Library and implements the general interfaces defined in the General C# Class Library.
Step 1: Create a new Android Class Library.
In Visual Studio, select File > New Project > Android > Library > Class Library.
Step 2: Add the dependencies.
The Android Class Library is dependent on the Android Bindings Library and the General C# Library.
In the Project Explorer, right-click on the high level solution, select Add/Existing Project... and select *.csproj. Perform the same for both Android Bindings Library and General C# Class Library.
Right-click on the References folder in the Android Class Library project, and select Add Project Reference.....
Click the
Projectstab and select both Android Bindings Library and the General C# Class Library.
Step 3: Add Xamarin.Essentials.
In the Project Explorer, right-click on the Android Class Library project, select Manage NuGet packages... and search to add Xamarin.Essentials.
Step 4: Implement the interfaces.
iOS
The iOS part consists of:
iOS Proxy Framework
iOS Bindings Library
iOS Class Library
iOS Proxy Framework
The first step is to build a native Swift framework with Objective-C header enabled. The framework has the header embedded in the package in the following directory: .framework/Headers/-Swift.h. This header exposes the public interfaces, which will be used to create Xamarin.iOS binding metadata and generate C# classes exposing the Swift framework members.
Step 1: Create a new XCode Framework project.
Click File > New > Project > Multiplatform > Framework & Library > Framework to create a new XCode Framework project.
Step 2: Moving the D1 frameworks.
Move these D1 frameworks and place them in General > Frameworks and Libraries.
Step 3: Add the D1 wrapper code.
Step 4: Build the framework.
Select the appropriate platform (Device or Simulator)
Product/BuildProduct/Show Build folder in finderto locate the created framework.
iOS Bindings Library
By now, you should have the framework with the Objective-C generated interface header ready to be consumed by a Xamarin.iOS binding. The next step is to prepare the API definition interfaces which are used by a binding project to generate C# classes. These definitions can either be created manually or automatically by the Objective Sharpie tool and the generated header file.
Use Sharpie to generate the metadata.
Step 1: Create the API definition using Sharpie tool.
This will give the output:
The generated API definitions need to be reviewed and in most cases to be updated. For this reason the Verify keyword is added to the generated files as an indication for this.
On the generated files, perform the following updates:
Step 2: Create a new iOS Bindings Library.
Select File > New Project > iOS > Library > Bindings Library to create a new iOS Bindings Library.
Step 3: Replace ApiDefinitions.cs and StructsAndEnums.cs files.
Replace the ApiDefinitions.cs and StructsAndEnums.cs files with the newly created ones from the step 1.
Step 4: Add Native Reference.
Right-click on Native References, select Add Native Reference and select the newly created proxy framework.
iOS Class Library
The iOS Class Library wraps the iOS Bindings Library and implements the general interfaces defined in the General C# Class Library.
Step 1: Create a new iOS Class Library.
In Visual Studio, select File > New Project... > iOS > Library > Class Library to create a new iOS Class Library.
Step 2: Add the dependencies.
The iOS Class Library is dependent on the iOS Bindings Library and the General C# Library.
In the Project Explorer, right-click on the high level solution,select Add/Existing Project... and select the *.csproj. Perform the same for both iOS Bindings Library and General C# Class Library.
Right-click on References folder in the Android Class Library project, and select Add Project Reference....
Click the Projects tab and select both iOS Bindings Library and the General C# Class Library.
Step 3: Add Xamarin.Essentials.
In the Project Explorer, right-click on the iOS Class Library project and select Manage NuGet packages... and search to add Xamarin.Essentials.
Step 4: Implement the interfaces.
C# Forms Application
Step 1: Create a new Xamarin.Forms app.
Click File > New > Project > Multiplatform > App > Blank App to create a new Xamarin Forms App.
To be able to access the Sandbox Environment on the Android platform, the application needs to have the package name com.thalesgroup.gemalto.d1.validation which is registered for Google Pay.
Step 2: Add the project dependencies.
In the Project Explorer, right-click on the high level solution, select Add/Existing Project... and add all the newly created projects (General C# Library, Android Class Library and iOS Class Library, Android Bindings Library, iOS Bindings library) to the solution.
In the Project Explorer, right click on the Dependencies folder of the Xamarin.Forms app project and select Add Project Reference....
Select the required General C# Library that you wish to add.
To be able to add the project dependencies, the projects need to use the same .NET Standard Library version (tested with .NET Standard Library 2.1).
The Xamarin Forms App is dependent on the General C# Class Library.
The Android part of the Xamarin.Forms app is dependent on:
The Android Class Library.
The General C# Library.
The iOS part of the Xamarin Forms App is dependent on:
The iOS Class Library.
The General C# Library.
Android setup
Step 1: Add the Project dependencies.
In the Project Explorer, right click on the References folder of the Xamarin.Forms app project and select Add Project Reference....
Select the required General C# Library and the Android Class Library.
Step 2: Add the other dependencies.
The following dependencies need to be added to the Android Xamarin.Forms app:
Square.OkHttp3.LoggingInterceptor
Xamarin.GooglePlayServices.Base
These can be added as NuGet packages as described in the following steps:
In the Project Explorer, right click on the Packages folder under the Xamarin.Forms app project.
Select
Manage NuGet Packages...and add the appropriate packages.
There is another dependency, JNA, which has to be added manually in the form of an AAR file.
Download the AAR file.
Copy the downloaded AAR file to the Assets folder under the Android Xamarin Forms App project.
Right-click on the AAR file and select Properties.
Set the Build action to AndroidAarLibrary.
Step 3: Add other assets and resources.
Add the Host APDU service resource file.
Add and update the following files to the Android Xamarin.Forms project:
Add the NFC Wallet configuration files.
Step 4: Update the AndroidManifest.xml file.
Step 5: Update the application signing keystore.
To run the NFC Wallet use case on the Android platform, the application needs to be signed with a specific keystore.
Step 6: Add Firebase Cloud Messaging (FCM).
As NFC Wallet on the Android platform uses Firebase Cloud Messaging (FCM), Google Messaging Services has to be enabled in the Android application and the google-services.json file needs to be added.
Add the dependency, Xamarin.Firebase.Messaging, to the Android Xamarin.Forms app.
These can be added as NuGet packages as described in the following steps:
In the Project Explorer, right click on the Packages folder under the Xamarin.Forms app project.
Select Manage NuGet Packages... and add the appropriate packages.
Move the
google-services.jsonto the Android Xamarin.Forms project. Right click on thegoogle-services.jsonfile, select Properties and set the Build action toGoogleServicesJson.
Step 7: Register native D1 implementations.
iOS setup
In the Project Explorer, right click on the References folder of the Xamarin.Forms app project and select Add Project Reference... and add the General C# Library and the iOS Class Library.
Register the native D1 implementations.
Access the D1 API from the Xamarin.Forms app
After performing all the above steps, the D1 API can now be accessed from the Xamarin.Forms app.
References
Last updated
Was this helpful?