Flutter: What do you do with a third party 32bit Android NDK library?

Swav Kulinski
3 min readMar 28, 2018

--

I am happy to announce that last week I started work on my first commercial project using Flutter! A year of speaking, convincing, demos and live coding sessions has finally paid off.

As I hit the ground last Monday there was already a challenge facing me. My new app needed to use a third party library which used an NDK. I already knew that Flutter should have no problem with native libraries. Just add them to the project and create a Channel for it. Simple…

Not so fast.

My iOS teammate, Andrea Bizzotto implemented the channels and deployed the app without much hassle. I thought on Android it would be as simple as on iOS. I started the emulator and deployed the app. Success! Life is great, but how can I take a photo on the emulator? Better get my Pixel and try it on the real thing. Flutter compiled the code without any problem:

Launching lib/main.dart on Pixel in debug mode…
Initializing gradle… 0.8s
Resolving dependencies… 0.9s
Running ‘gradlew assembleDebug’… 2.2s
Built build/app/outputs/apk/debug/app-debug.apk (47.6MB).

Unfortunately, there was a nasty surprise waiting for me.

Nothing ever works on a Monday morning, eh? I checked ADB logcat for a FATAL EXCEPTION.

03-26 07:14:55.437 26092-26092/com.theappbusiness.flutter.helloworld E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.theappbusiness.flutter.helloworld, PID: 26092
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.theappbusiness.flutter.helloworld-8vjCHZsfrBa5rfrnJLsV5g==/base.apk"],nativeLibraryDirectories=[/data/app/com.theappbusiness.flutter.helloworld-8vjCHZsfrBa5rfrnJLsV5g==/lib/arm64, /data/app/com.theappbusiness.flutter.helloworld-8vjCHZsfrBa5rfrnJLsV5g==/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]] couldn't find "liblept.so"
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)
at java.lang.System.loadLibrary(System.java:1657)

The next step was to see what was actually is inside the APK. I could unzip it manually and just check it, but Android Studio has a nice APK Analyzer now so I decided to make a use of it.

Ok so, the emulator was fine as x86 has a full set of libraries. The Pixel, on the other hand, is missing the third-party SDK as it is an arm64-v8a device. I wasn’t sure if coping over libraries would do any good. I decided to check if there was a way to force Flutter into 32 bit. I didn’t have to search for long.

flutter run --help..target-platform Specify the target platform when building the app for an Android device.
Ignored on iOS.
[default (default), android-arm, android-arm64]

Apparently, Flutter has no means to detect if a third-party library is 32bit only but it has a switch just for that. I’ve added an extra switch to the command line and the resulting APK was 32bit only.

flutter run --target-platform android-arm

Now all that remained was to gently remind the author of the library that this is 2018 and we probably need a 64bit version of his work. For now, I am unblocked and can continue on my first commercial Flutter app.

--

--

Swav Kulinski

Computer enthusiast since 80s, Android developer since Eclair, now Flutter enthusiast. https://gitlab.com/swav-kulinski