Is there a way to programmatically determine the ABI (arm64-v8a, armeabi-v7a, x86, etc.) of the current build in the build.gradle file?
I tried to create a function like this:
def getABIFromCurrentBuild() { return android.os.Build.CPU_ABI; }
but the android.os package is not available from the Gradle plugin.
Background:
I have an application project in Android Studio that uses the JNI library. The JNI library, in turn, refers to the C ++ static library, which is built outside of Android Studio using the Makefile. However, the static library is divided into different versions (one for each ABI).
I want to avoid creating a Makefile for the JNI library and just use the ndk block in the build.gradle file of the application in Android Studio. This is what my ndk block looks like:
ndk { moduleName "myJNILib" cFlags "-std=c++11" ldLibs "GLESv3 ${project.projectDir}/../../build/local/${getABIFromCurrentBuild()}/myStaticLib.a" stl "gnustl_shared" }
As you can see, I hacked my static library on the linker command line when creating the JNI library, but I was stuck trying to figure out how to link the correct version.
I also tried to highlight all ABI types in the productFlavors block as follows:
productFlavors { x86 { ndk { abiFilter "x86" moduleName "myJNILib" cFlags "-std=c++11" ldLibs "GLESv3 ${project.projectDir}/../../build/local/x86/myStaticLib.a" stl "gnustl_shared" } } x86_64 { ndk { abiFilter "x86_64" moduleName "myJNILib" cFlags "-std=c++11" ldLibs "GLESv3 ${project.projectDir}/../../build/local/x86_64/myStaticLib.a" stl "gnustl_shared" } } ... }
which gets everything right, but then the JNI shared library is only copied to some of the apks because the apk naming scheme used by the Gradle plugin is much more complicated than what I captured in productFlavors.
For example, one of the apks is called "app-arm64-x86_64-debug". In this case, there is no ABI filter that matches.
Update
Currently, I have avoided the need for a separate static library by simply adding a symlink to my static library code in <project>/app/src/main/jni/ . This forces it to compile directly to the JNI library, which is essentially the same effect as a link to a static library. Plus, it has the advantage that the source code of the library is displayed in Android Studio, which makes access easier.