Build executables for any ARM device
Android Studio has given developers a new IDE to create apps that work in Android. Now with the recent addition of CMake integration, it has also given us Embedded Developers something a bit more. We now have an IDE that can target not only Android but any device that runs linux on a supported ABI.
Shock, horror, no, it's good to react with eagerness and let our inner hacker out. When building apps for Raspberry Pi, Snapdragon's family of boards, or any embedded device running linux, instead of creating a cross compiler; we can use the one that Google provided. In this blog post, I'm going to dedicate us to the lofty goal of a traditional Hello World compiled from Android Studio, and run on a Snapdragon 410c. It's simple really, so let's get started.
1st, create a module or new project, then get Android Studio to use CMakeLists.txt. From here, we need to start doing things that aren't part of the documentation.
We need to create an executable. However, the libc and other shared libs that Android uses, might not be the same that ship with Android. So what we can do is to create a static executable; that is we can create an executable that packages all needed libraries in the executable. While yes there is a significant security consideration in using static linking, understanding that security consideration is necessary. The primary concern is that when a shared system library gets updated, all executables that use it get that same update. However, a static library would never benefit from that update. This means, in order to be secure, we must frequently rebuild the executables we create and redeploy them to all of the places we work with. If you're okay with this trade off, then this is the right method for you. If not and you want to benefit from the shared libraries, I'd suggest doing a cross compiler and placing it in the toolchains directory of your NDK folder; this will allow you to select it from gradle and continue to use Android Studio as the rest of this post outlines.
For now, let's see how easy it is to do a static executable. Change your CMakeLists.txt file to resemble this:
cmake_minimum_required(VERSION 3.4.1) set(CMAKE_EXE_LINKER_FLAGS "-static") add_executable( HelloWorld src/main/cpp/native-lib.cpp )
You can see from the above, we need to create two new commands, add_executable instead of add_library creates an executable. Setting the linker flags to -static creates an executable that doesn't depend upon the system libs as shared objects.
Now when you hit build, you will find your executable waiting for deployment in <ProjectRoot>/app/.externalNativeBuild/cmake/debug/arm64-v8a
all that is left is to deploy your executable to your device, and run it.
I'm currently working on a build with Daydream, and Snapdragon Flight as part of one large project. This will be a blog series and now that we have this integral part of it; expect to see more from me very soon.