Flutter Programming with Android AAR File

Flutter, a framework developed by Google, aims to help developers build iOS and Android apps from a single codebase in Dart programming language. It is still an early-stage open-source project. The development environment only works on macOS and Linux (64-bit). I was curious about how Flutter performs comparing to Xamarin and Cordova, and thus, I got started with Flutter a week ago. When developing an Android app, you may need to use some third-party SDKs, which wrapped as *.jar, *.so or *.aar files. In this post, I will share how to link an Android aar file in Flutter project.

How to Add AAR File to Flutter Project

Download Dynamsoft Barcode Reader for Android to get DynamsoftBarcodeReader.aar file.

Download Flutter source code.

Automatically configure aar file with Android Studio for Flutter project

Import flutter/examples/hello_services/android/ to Android Studio.

Click File > New > New Module and choose Import .JAR/.AAR Package to import DynamsoftBarcodeReader.aar to the Android project.

Press F4 to open Project Structure, and then add the dependent module.

Manually configure aar file for Flutter project

After configuring aar file with Android Studio, open the project with IntelliJ IDEA to see what happened. Thereafter, we can manually configure an aar file in Flutter project as follows:

Create a new folder android/DynamsoftBarcodeReader.

Create android/DynamsoftBarcodeReader/build.gradle:

Copy DynamsoftBarcodeReader.aar to this folder:

Edit android/settings.gradle:

Building Android Barcode Reader with Flutter

Open AndroidManifest.xml to add permissions:

Write Java code to invoke Dynamsoft Barcode APIs and send result message to the Flutter UI:

Create Flutter UI with Input, Button, and Text widgets:

Run the Flutter project:

References

APK Building Issues

The Android APK file generated by Flutter project contains a native library libsky_shell.so compiled for armeabi-v7a. DynamsoftBarcodeReader.aar built for armeabi-v7a and arm64-v8a. If you unzip the APK file, you will see there is no libsky_shell.so file in arm64-v8a folder. This will cause a crash when running the App on Android devices with a 64-bit processor, like Qualcomm Snapdragon 820, because by default the operating system will load shared library from arm64-v8a folder.

How to solve this issue? Remove native libraries other than armeabit-v7a and rebuild the aar file.

Source Code

https://github.com/yushulx/flutter-android-aar

Manager of Dynamsoft Open Source Projects | Tech Lover