Search code examples
androidandroid-studiogradlecmakegradle-experimental

Migrate Native Module from Gradle Experimental Plugin to Stable Gradle


I have an Android Library project which has a part in C/C++ via Android NDK. The project started half of a year ago so we chose to use Experimental Plugin because of better NDK support. I'm using gradle-experimental:0.8.2right now. I have a com.android.model.native module and i would like to migrate it to gradle:2.2.0. The only option i see in Gradle Android Plugin DSL is:

  • AppExtension: android extension for com.android.application projects.
  • LibraryExtension: android extension for com.android.library projects.
  • TestExtension: android extension for com.android.test projects.

So the question is how to make a pure native module in gradle with stable gradle plugin?

Here is my current native module:

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

apply from: "../config.gradle"
def config = ext.configuration

model {
    android {
        compileSdkVersion = config.compileSdkVersion
        buildToolsVersion = config.buildToolsVersion

        defaultConfig {
            minSdkVersion.apiLevel = config.minimumSdkVersion
            targetSdkVersion.apiLevel = config.targetSdkVersion
            versionCode = 1
            versionName = '1.0'
        }
        ndk {
            moduleName = 'apicore'
            platformVersion = config.minimumSdkVersion
            cppFlags.add("-std=c++11")
            cppFlags.add("-pthread")
            cppFlags.add("-fexceptions")
            cppFlags.add("-frtti")
            stl = "gnustl_static"
            abiFilters.addAll(config.targetPlatforms)
            ldLibs.addAll(['android', 'log'])
        }
        sources {
            main {
                jni {
                    source {
                        //include "someFile.txt"
                        // This is ignored.
                        exclude "main.cpp"
                        exclude "misc/APITest.cpp"
                        exclude "misc/APITest.h"
                    }
                }
            }
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }
    }
}

Solution

  • I just migrated my project from Gradle Experimental Plugin to Gradle plugin for Android. The current Gradle plugin for Android still not provide something what com.android.model.native extension provided from the experimental plugin which is an ability to create a pure native module. I have to realise that i don't even need that. What i did to replace the com.android.model.native module is i made a library module where i handle the native code and building of my native libraries and i just copy the native libraries where i need them. Of course the module generate the .aar but thats not a problem i just don't use it.