0

I've inherited a really messy android project with a lot of NDK dependencies and having a lot of problems with getting gradle to correctly link and include all .so and .a files into the resulting apk.

The project consists of some java code that sets up some activities and call into a big NDK library, built from C++ which in turn links with a dosens of 3rd party library (prebuilt or built from source).

I have managed to make it build with the latest gradle experimental plugin, but for some reason, my module isn't included in the apk while my 3rd party .so files are even though I can see that gradle have built my module into a .so file which it have placed in the build directory.

My build.gradle looks like this:

apply plugin: 'com.android.model.application'
model {
    android {
        compileSdkVersion = 23
        buildToolsVersion = "22.0.1"
        defaultConfig.with {
            applicationId = "<removed>"
            minSdkVersion.apiLevel = 7
            targetSdkVersion.apiLevel = 23
            versionCode = 1
            versionName = "1.0"
        }
        ndk {
            moduleName = "XM"
            CFlags.add("-I${file("src/main/jni")}".toString())
            cppFlags.addAll(["-I${file("../../3rd_part/android/osg")}".toString()])
            cppFlags.addAll(["-I${file("../../3rd_part/android/opus")}".toString()])
            ldFlags.add("-L${file("src/main/jniLibs/armeabi-v7a")}".toString())
            ldLibs.addAll(["osgdb_jpeg", "osgdb_freetype", "jpeg","argsub_es", "jnigraphics", "android", "log"])
            stl = "gnustl_shared"
            abiFilters.add("armeabi-v7a")
        }

        sources {
            main {
                jni {
                    source {
                        srcDir "../../core"
                        srcDir "src/main/jni"
                    }
                }

            }
        }
        lintOptions.abortOnError = false
    }

    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-v4:23.0.0'
    compile 'com.android.support:appcompat-v7:23.0.0'
    compile 'com.android.support:design:23.0.0'
    compile 'com.wunderlist:sliding-layer:1.1.1'

}

So to sum up my question: why does gradle build my module (libXM.so), place it into build/libs/xM/shared/armeabi-v7a/debug, but not include it into my final apk file?

monoceres
  • 4,722
  • 4
  • 38
  • 63
  • Check my answer here: http://stackoverflow.com/a/33164947/3626214 maybe it helps you – Aspicas Apr 08 '16 at 12:48
  • Can you explain how you know that libXM.so isn't being included? Are there any errors in the log? Does the project run and fail to find that dependency? – Francesca Nannizzi Apr 08 '16 at 13:37
  • Since APK files are just zip, I extracted the files using winrar and checked, and yes, the applicaiton fails when I try to load the xm library. – monoceres Apr 08 '16 at 13:40

1 Answers1

1

I didn't get several things: if you want to build a shared library with gradle, then you have to use apply plugin: 'com.android.model.library' and not application

If you want to build an application and use prebuilt library, then you have to write something like this:

    sources {
        main {
            jni {
                source {
                    srcDir "../../core"
                    srcDir "src/main/jni"
                }
                dependencies{
                    library "XM" linkage "shared"
                }

And, of course, to set ldFlags previously.

c4pQ
  • 884
  • 8
  • 28
  • I'm new to the gradle build system so I may have confused things, my whole project is an application as it contains activites and is overall a standalone application. The bulk of the functionality is in C++ (this C++ in turn depends on prebuilt libraries) though which I want to compile and use together with the application. Maybe I should keep the C++ in a separate project? – monoceres Apr 08 '16 at 14:03
  • 1
    Yes, you should. Make separate project with `'com.android.model.library'` and then from java-application you can get this library included in your final apk. Here you can find hints, how to use both gradle and gradle experimental plugins: http://stackoverflow.com/questions/36472561/android-ndk-unable-to-debug-native-code-with-two-gradle-plugins – c4pQ Apr 08 '16 at 14:08
  • 1
    In java-project build.gradle you can add `dependencies { compile project(':xm_lib') ...` So first you native library would build and then included in your application – c4pQ Apr 08 '16 at 14:12
  • Ah, sorry, forgot about this Q. I now use a seperate module which works fine! However I got the same issue with the seperate module until I changed to module name to lowercase characters only. No idea what the deal was with that. :S – monoceres Apr 18 '16 at 10:51