androidgradlegradle-kotlin-dslandroid-module

Create a new configuration and set its BuildTypeAttr for a module in Android gradle


Following this stack overflow answer Error building Android library: Direct local .aar file dependencies are not supported to solve my original problem, on my module i created a build.gradle file with the following content:

configurations.maybeCreate("default")
artifacts.add("default", file('[nameOfTheAar].aar'))

So far, so good. I then wanted to extend this so it would use 2 different aar in case of debug and release. I tried:

configurations.maybeCreate("debug")
configurations.maybeCreate("release")
artifacts.add("debug", file('[nameOfTheAar]-debug.aar'))
artifacts.add("release", file('[nameOfTheAar]-release.aar'))

But the name of configuration is not sufficient to allow the automatic selection, and I got the following error:

Could not determine the dependencies of task ':[moduleName]:buildCMakeDebug[arm64-v8a]'.
> Could not resolve all task dependencies for configuration ':realsense:debugCompileClasspath'.
   > Could not resolve project :[submoduleName].
     Required by:
         project :[moduleName]
      > No matching configuration of project :[submoduleName] was found. The consumer was configured to find a library for use during compile-time, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'com.android.build.api.attributes.AgpVersionAttr' with value '8.3.1', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
          - Configuration 'debug':
              - Other compatible attributes:
                  - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.3.1')
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
                  - Doesn't say anything about its component category (required a library)
                  - Doesn't say anything about its target Java environment (preferred optimized for Android)
                  - Doesn't say anything about its usage (required compile-time)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm')
          - Configuration 'release':
              - Other compatible attributes:
                  - Doesn't say anything about com.android.build.api.attributes.AgpVersionAttr (required '8.3.1')
                  - Doesn't say anything about com.android.build.api.attributes.BuildTypeAttr (required 'debug')
                  - Doesn't say anything about its component category (required a library)
                  - Doesn't say anything about its target Java environment (preferred optimized for Android)
                  - Doesn't say anything about its usage (required compile-time)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'androidJvm')

So it looks like it needs an attribute of type BuildTypeAttr setup, I've tried something along the lines of the following but it's not the right syntax / it doesn't work:

configurations.maybeCreate("debug").attributes { it.attribute(BuildTypeAttr, "debug")}
configurations.maybeCreate("release").attributes { it.attribute(BuildTypeAttr, "release")}
artifacts.add("debug", file('librealsense-debug.aar'))
artifacts.add("release", file('librealsense-release.aar'))

What is the right way (using kotlin script language) to set the build type attribute on a configuration?


Solution

  • This is the build.gradle needed on [submodule] to automatically select different aar files depending on the build type:

    import com.android.build.api.attributes.*
    
    configurations.maybeCreate("debug").attributes { attribute(BuildTypeAttr.ATTRIBUTE, project.objects.named(BuildTypeAttr.class,"debug")) }
    configurations.maybeCreate("release").attributes { attribute(BuildTypeAttr.ATTRIBUTE, project.objects.named(BuildTypeAttr.class,"release")) }
    
    artifacts.add("debug", file('[nameOfTheAar]-debug.aar'))
    artifacts.add("release", file('[nameOfTheAar]-release.aar'))
    

    This allows to have only one line in your [module] importing the [submodule]

    implementation(project(":[submodulename]"))
    

    key points were: