在 build.gradle 文件的 externalNativeBuild 中指定目标-> 没有方法签名

Specify targets in externalNativeBuild of the build.gradle file-> No signature of method

  1. 在 android studio 4.1.1 中开始一个新的原生 c++ 项目。
  2. 转到模块build.gradle
  3. 添加 targets 行:
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
            targets "native-lib"  // New line
        }
    }

点击绿色播放按钮时出现错误 (运行 'app'):

Build file '<project folder>/app/build.gradle' line: 5
A problem occurred evaluating project ':app'.
> No signature of method: build_bcdq4hni531na6stswx8a7txx.android() is 
applicable for argument types: (build_bcdq4hni531na6stswx8a7txx$_run_closure1) 
values: [build_bcdq4hni531na6stswx8a7txx$_run_closure1@41fd5f78]

这是怎么回事?

targets 属性 记录在:https://developer.android.com/studio/projects/gradle-external-native-builds

这个问题的答案也用了targets属性。

我什至无法通过将 arguments "-DOPTION=1" 添加到 build.gradle 来向 CMake 传递参数!

也许“目标”和其他选项将放在 android.defaultConfig.externalNativeBuild 中(而不是 android.externalNativeBuild)。

android {

    defaultConfig {
        :
        externalNativeBuild {
            cmake {
                targets "native-lib" // New line here!
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
            // targets "native-lib"  // Not here!
        }
    }
}