androidc++android-ndkjava-native-interfaceaar

Android NDK build cmake include .so library included in .aar library


I have an android_library.aar file that containing library.so and some other resources and java files.

I imported android_library.aar to my project. My project uses c++ code with NDK. My problem is I can not compile C++ code that dependent with library.so with CMake. How could I extract library.so from aar before CMake compile? I want to include library.so to my C++ code directly without java. I know how to include .so in a project but I don't know how to do it from inside of aar.


Solution

  • I found a solution on my own after 1-week research. I found some code from Google GVR code. But it hasn't worked for my case. I changed build.dependsOn to preBuild.dependsOn then it worked. It might help some people in the future.

    Here is the example code:

    In the module's build.gradle file:

    sourceSets {
        main {
            jniLibs.srcDirs = ["jniLibs"]
        }
    }
    
    task extractSo(type: Copy) {
        println 'Extracting *.so file(s)....'
    
        from zipTree("${project.rootDir}/ModuleName/ModuleName.aar")
        into "${project.rootDir}/${project.name}/src/main/jniLibs/"
        include "jni/**/*.so"
    
        eachFile {
            def segments = it.getRelativePath().getSegments() as List
            println segments
            it.setPath(segments.tail().join("/"))
            return it
        }
        includeEmptyDirs = false
    }
    
    preBuild.dependsOn extractSo