Add References and Set a License - Flutter Android

This tutorial shows how to get started with the LEADTOOLS SDK in a Flutter Android application.

Overview  
Summary This tutorial covers how to set a license in a Flutter application using Android.
Completion Time 15 minutes
Project Download tutorial project (423 KB)
Platform Flutter (Java Android)
IDE Android Studio
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Before any functionality from the SDK can be leveraged, a valid runtime license will have to be set.

For instructions on how to obtain a runtime license, refer to Obtaining a License.

Create the Project

This tutorial builds upon the Writing custom platform-specific code directions.

You need to complete the steps up to step 3: Add an Android platform-specific implementation.

Configure the Project

Ensure the android folder is open in Android Studio. Select the File tab and open the android folder found in this project path: <APP_DIR>\batterylevel\android.

Open the <APP_DIR>\android\app folder and create a libs folder. Add the leadtools.jar into this folder. You can find leadtools.jar after installing the LEADTOOLS Android SDK in the following path: <INSTALL_DIR>\Bin\Java.

Add JAR file

Open the <APP_DIR>\android\app\src\main folder. Create a new folder in that directory and name it jniLibs. Add a subfolder for the CPU architecture that will be supported. This example will use x86_64 architecture. Copy the appropriate version of libleadtools.so to this x86_64 subfolder. The .SO files can be found in the <INSTALL_DIR>\Bin\Android directory.

Add so file

Open the <APP_DIR>\android\app\src\main\res directory and create a folder named raw. Input the LEADTOOLS license LIC file inside the raw folder. Ensure the LIC file is named leadtools.lic, and that the naming convention does not include any capitalization in the name.

Input license file in RAW folder

Locate and open build.gradle. Add the following line in the dependencies block.

Java
implementation fileTree(dir: 'libs', include: ['*.jar']) 
Edit gradle file

Add the Set License Code

Open the <APP_DIR>\android\app\src\main\java\ folder and locate the MainActivity.java file. A license will need to be set in order to be able to access any LEADTOOLS SDK functionality. Add the code below to properly set your LEADTOOLS license. Be sure to replace "Input_Developer_Key_Here" with the development key that is being used.

Java
import leadtools.LTLibrary; 
import leadtools.Platform; 
import leadtools.RasterSupport; 
 
 
public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) { 
        super.configureFlutterEngine(flutterEngine); 
        new MethodChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), CHANNEL) 
                .setMethodCallHandler( 
                        (call, result) -> { 
                            // This method is invoked on the main thread. 
                            if (call.method.equals("getBatteryLevel")) { 
                                int batteryLevel = getBatteryLevel(); 
 
                                String sharedLibsPath = this.getApplicationInfo().nativeLibraryDir; 
                                try{ 
                                    Platform.setLibPath(sharedLibsPath); 
                                    Platform.loadLibrary(LTLibrary.LEADTOOLS); 
                                } 
                                catch(Exception ex) { 
                                    Log.d(TAG,"Failed to load LEADTOOLS Native libraries" ); 
                                } 
                                try{ 
                                    RasterSupport.setLicense(this, getResources().openRawResource(R.raw.leadtools), "Input_Developer_Key_Here"); 
                                } 
                                catch(Exception ex) 
                                { 
                                    Log.d(TAG, "Failed to set LEADTOOLS license"); 
                                    finish(); 
                                } 
 
                            if (batteryLevel != -1) { 
                                    result.success(batteryLevel); 
                                } else { 
                                    result.error("UNAVAILABLE", "Battery level not available.", null); 
                                } 
                            } else { 
                                result.notImplemented(); 
                            } 
                        } 
                ); 
} 

Run the Project

Run the project by pressing Shift + F10.

If the steps were followed correctly, the application runs and the license will be set when you press the Get Battery Level button.

Wrap-up

This tutorial showed how to set a license in the android portion of a flutter application. This is the basis for all Flutter Android applications using the LEADTOOLS SDK. All the functionality in the SDK is unlocked via setting a license and setLicense must be called before calling any LEADTOOLS SDK methods.

After the SDK is purchased, the evaluation license can be replaced with a valid runtime license to disable the Nag Message.

See Also

Help Version 23.0.2024.4.23
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.


Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.