Search code examples
android-gradle-pluginndk-build

Android ndk different cflags


I have 3 projects in Android Studio: producer, consumer, and lib.

Lib is a shared JNI library in which I define a few functions to behave differently while others the same depending on how they're used. For example:

void function() {
#ifdef PRODUCER
  printf("I'm a producer!\n");
#endif // PRODUCER
#ifdef CONSUMER
  printf("I'm a consumer!\n");
#endif // CONSUMER
}

I'd like my gradle.build script for lib to basically have two potential flavors: producer & consumer then specify a dependency in the other projects like compile project(':lib:producer') or project(':lib:consumer'). These different targets would only vary in the following:

android {
    defaultConfig {
        ndk {
            cFlag "-DPRODUCER" // or -DCONSUMER
        }
    }
}

Is this possible?


Solution

  • Turns out the best thing to do here was upgrade to Android Studio 2.2+ then use the externalNativeBuild's CMake to generate multiple libraries.