0

Using Kotlin DSL, does anyone know how I can tell Gradle to only execute the instructions in my library module's externalNativeBuild block if a) CMakeLists.txt is present? or b) based on buildType (debug no, release yes).

I've tried moving the externalNativeBuild block into buildTypes block, but this did not work. I've also tried several variations of something like this:

val cMakePath = "src/main/cpp/CMakeLists.txt"
    if (File(cMakePath).exists()) {
        externalNativeBuild {
            cmake {
                path(cMakePath)
            }
        }
    }

(In my specific setup, CMakeLists.txt gets generated during release builds, the native part is only present for release builds. That's why I tried it this way.)

But even if Gradle is happy - as in no compiler warnings or errors, with every conditional solution I try, at startup my app fails because

java.lang.UnsatisfiedLinkError: dlopen failed: library "mylibrary.so" not found

so obviously the native lib doesn't get built.

How can I achieve my desired behaviour?

michpohl
  • 852
  • 8
  • 30
  • You might be able to prevent the actual build step by doing something like `afterEvaluate { project -> android.applicationVariants.all { variant -> if (variant.buildType.name != "release") { project.tasks.("externalNativeBuild${variant.name.capitalize()}").enabled = false } } }` – Michael Jun 02 '22 at 08:52
  • But you could run into problems during the sync phase if the CMakeLists.txt file doesn't exist at that point. It sounds like a somewhat strange setup to generate it at build-time. – Michael Jun 02 '22 at 08:53
  • Well, I am using a plugin that generates native code. During CI runs this native code gets regenerated. It might be somewhat strange, but it is what it is :-) The thing is, the whole native part is not used in debug builds, hence the idea to keep it off there. – michpohl Jun 02 '22 at 16:00

0 Answers0