Native: how to reference the buildTypes value in a component

Hi zosrothko,

Thanks for the great detail in the question. The BinaryNamingScheme is an internal API and I haven’t seen any discussion about making it public. The best step forward is really to use the answer I linked in my previous comment. The ground work is exposed there. The little missing detail is you should use the code inside a convention for your project. As the name say it’s a global configuration applied to all the project that need to follow a specific convention. The following code could be use to create a convention for all cpp project:

File appendDebugSuffix(File binaryFile) {
  int extensionSeparatorIndex = binaryFile.path.lastIndexOf('.')
  return new File(binaryFile.path.substring(0, extensionSeparatorIndex) + "_d" + binaryFile.path.substring(extensionSeparatorIndex))
}

allprojects {
  plugins.withId("cpp") {
    model {
      buildTypes {
        Release
        Debug
      }
      components {
        withType(NativeComponentSpec) {
          binaries.withType(NativeBinarySpec) {
            if(buildType == buildTypes.Debug) {
              if (it instanceof SharedLibraryBinarySpec) {
                sharedLibraryFile = appendDebugSuffix(sharedLibraryFile)
                sharedLibraryLinkFile = appendDebugSuffix(sharedLibraryLinkFile)
              } else if (it instanceof StaticLibraryBinarySpec) {
                staticLibraryFile = appendDebugSuffix(staticLibraryFile)
              } else if (it instanceof NativeExecutableBinarySpec) {
                // executable.file = ...
              } else {
                throw new GradleException("Unknown native library binary")
              }
            }
          }
        }
      }
    }
  }
}

As we can see from this code, it will apply an action for allprojects that will only execute if the cpp plugin is applied. It will then proceed in adding the Debug and Release buildTypes and configure NativeBinarySpec for all NativeComponentSpec. This is a convention as it will only do the work if specific condition are met for the entire project. The code is still a bit opinionated as it assume a Debug build type will be present. You could play around are remove this assumption in order to generalize event more the code. You can also only apply the convention on a subset of project by using subprojects or similar behavior.

I hope this answer your question,

Daniel