9

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.

Almas Dusal
  • 613
  • 7
  • 15
  • 1
    *Very nice indeed!* I believe that **gradle** does extract the **.so** files if the **.aar** is a dependency. – Alex Cohn Sep 03 '18 at 14:50
  • I thought so. But it does not extract before cmake build. I don't know why. I searched for it and did not find any answer easy than this method. – Almas Dusal Sep 06 '18 at 00:05
  • … but in your solution the **aar** is not declared as dependency of your project, s it? – Alex Cohn Sep 06 '18 at 04:54
  • 1
    It's also must declare library as dependency. Because of Java part of library. I don't know much about dependency decleration also affecting to JNI part or not. – Almas Dusal Sep 06 '18 at 15:09

1 Answers1

2

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
Almas Dusal
  • 613
  • 7
  • 15