Home > Net >  How to pass arguments to the CMake build of a plugin from gradle build in flutter?
How to pass arguments to the CMake build of a plugin from gradle build in flutter?

Time:11-05

My question is similar to this one, except that in that question, he wanted to pass arguments directly from gradle to CMake. But in my case, I am not directly invoking the cmake build, but rather "flutter is doing it for me"

So to put it all together, when the app build starts:

  • gradle build of the app starts (build.gradle in the flutter project) ------(1)
  • the above build invokes gradle build of the plugin (build.gradle in the plugin's project) ------(2)
  • the gradle build of the plugin invokes the CMake build of the plugin (CMakeLists.txt in the plugin project) ------(3)

Now sending args from step (2) to step (3) is as mentioned in the link above.

My question is how to send args from step (1) to step (2)?

I am not very experienced with gradle but I tried inspecting the build.gradle file of step (1) to see from where it is invoking the build.gradle of the plugin, but I wasn't able to do that.

Update:

  • this answer is very similar to what I want, except that, as I mentioned, I want to pass variable from a gradle build that indirectly invokes cmake.
  • The use case for me is as follows: the plugin has some C code and uses shared libraries, these libraries have different versions and the user must choose one of them at build time, thats why I want the user to pass variables to cmake, for ex:
        externalNativeBuild {
            cmake {
                arguments "-DLIB_VERSION=VERSION_WITH_SOME_OPTION"
            }
        }

and then in CMake:

if(${LIB_VERSION})
# link some version of the shared libs
else()
# link another version

CodePudding user response:

So I found a solution, but I don't know if this is the right way to do it, as it defines the variables to be passed as "global gradle variables".

So we have 3 files:

(1) android/app/build.gradle in the flutter app's project folder

(2) android/build.gradle in the plugin's project folder

(3) CMakeLists.txt in the plugin's project folder

We want to pass an argument from (1) to (3) (since the user of the plugin only has access to (1)):

  • in (1), define the variables to be passed, put this at the end of the file:
ext {
    Q16 = 1
    HDRI = 0
}
  • in (2), read the variables at the beginning of the file:
def Q8
def Q16
def HDRI
if(rootProject.hasProperty("Q8")){
    Q8 = 1
}
else{
    Q8 = 0
}
if(rootProject.hasProperty("Q16")){
    Q16 = 1
}
else{
    Q16 = 0
}
if(rootProject.hasProperty("HDRI")){
    HDRI = 1
}
else{
    HDRI = 0
}

Then pass them to (3) (since (2) directly invokes (3)):

   android{
    ...
    defaultConfig{
        ...
        externalNativeBuild{
            cmake {
                arguments "-DQ8=${Q8}" , "-DQ16=${Q16}" , "-DHDRI=${HDRI}"
            }
        }
    }
}
  • Now we can read them in (3):
    message("from CMakeLists.txt: Q8 = ${Q8}")
    message("from CMakeLists.txt: Q16 = ${Q16}")
    message("from CMakeLists.txt: HDRI = ${HDRI}")
  • Related