Difference between libraries and main Android application based on SDL2 built by Gradle 4

Gradle Android Experimental Plugin works without problem with Android Studio and it is the recommended approach use it when starting a new project with NDK.

It’s very easy to get started, but there are some traps. Let’s explore common mistake when writing Gradle build script for Android with NDK.

In case of an application which is based on C libraries, the goal is clear. Build libraries and then link everything into the final project.

The tricky part is the definition of dependencies. The definition for the main and for libraries is different. If you use the same style of definition you may run into error like: Android Studio is not able to import the project, because of missing .so file.

Here is the definition which you can find in build.gradle for the main module:

model {
     repositories {
        libs(PrebuiltLibraries) {
            SDL2 {
                headers.srcDir "../SDL2/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("${gradle.libDistributionRoot}/SDL2/lib/${targetPlatform.getName()}/libSDL2.so")
                }
            }
            SDL2_image {
                headers.srcDir "../SDL2_image/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file("${gradle.libDistributionRoot}/SDL2_image/lib/${targetPlatform.getName()}/libSDL2_image.so")
                }
            }

        }
    }

   android {
        compileSdkVersion = gradle.sdkVersion
        buildToolsVersion = '25.0.3'

        defaultConfig {
            minSdkVersion.apiLevel = 13
            versionCode = 1
            versionName = '1.0'
        }
        ndk {
            moduleName = 'main'
            cppFlags.addAll([
                    "-I" + file("../SDL2/include/").absolutePath,
                    "-I" + file("../SDL2_image/include/").absolutePath,
            ])
            CFlags.addAll([
                    "-I" + file("../SDL2/include/").absolutePath,
                    "-I" + file("../SDL2_image/include/").absolutePath,
            ])
            stl "stlport_static"
        }

        sources {
            main {
                jni {
                    dependencies {
                        library 'SDL2' linkage 'shared'
                        library 'SDL2_jpeg' linkage 'shared'
                        library 'SDL2_png' linkage 'shared'
                        library 'SDL2_image' linkage 'shared'
                    }
                    source {
                        srcDirs 'src/'
                    }
                }
            }
        }

    }
}

If you look close enough, you’ll see that the first part of the file contains a definition of PrebuiltLibraries. These libraries are referencing directly the .so file. In the section dependencies, you can find reference to the declared dependencies.

Gradle will just build the app and it will grab all referenced .so files and put them into a final application. That is correct for the main module. When you’re writing build files for libraries, you should take a different approach.

Here is an example of SDL2_image/build.gradle file:

apply plugin: 'com.android.model.native'

model {

    android {
        compileSdkVersion = gradle.sdkVersion
        buildToolsVersion = '25.0.3'

        defaultConfig {
            minSdkVersion.apiLevel = 13
            versionCode = 1
            versionName = '1.0'
        }
        ndk {
            moduleName = 'SDL2_image'
            ldLibs.addAll(["GLESv1_CM", "EGL", "GLESv2", "log", "android", "dl"])
            CFlags.addAll(["-DGL_GLEXT_PROTOTYPES"])
            CFlags.addAll(["-I" + file("include/").absolutePath,
                           "-DGL_GLEXT_PROTOTYPES",
                           "-DLOAD_JPG",
                           "-DLOAD_PNG",
                           "-DLOAD_XPM"
            ])
        }

        sources {
            main {
                jni {
                    dependencies {
                        project ':SDL2' linkage 'shared'
                        project ':SDL2_jpeg' linkage 'shared'
                        project ':SDL2_png' linkage 'shared'
                    }
                    exportedHeaders {
                        srcDir "../SDL2/include"
                        srcDir "../SDL2_jpeg/include"
                        srcDir "../SDL2_png/include"
                    }
                    source {
                        srcDir "src"

                    }
                }
            }
        }

    }
}

Here you can see that there is no definition of PrebuiltLibraries, but the sources contain dependencies on projects of other libraries. In addition to that, there is exportedHeaders declaration which will tell the compiler where to find .h files. That is because you do not need any reference to .so dependencies when building .so library. You need just headers.

The different is very small in the dependencies section. It can simplify your build process if you use it correctly.

Here is an example of an error that occurs when the .so file is missing and library is referencing the file:

project refresh failed Error:Exception thrown while executing model rule: 
NdkComponentModelPlugin.Rules#createNativeBuildModel(NativeBuildConfig, ModelMap<androidbinaryinternal>, ModelMap<ndkabioptions>, NdkHandler) > create...

You can fix either build files or you can build missing libraries one by one.

The correct version of build files is available in sdl2-android-example project.

3. September 2017 at 20:55 - Development (Tags: , , , ). Both comments and pings are currently closed.

Comments are closed.