将架构信息从 gradlew 命令传递到 cmake

Pass architecture information from gradlew command to cmake

我有一个 android 应用程序支持 4 种不同的架构,即 armeabi-v7aarm64-v8ax86x86_64。我不希望为每 Android 个构建构建这些架构中的每一个。我想通过 gradlew 命令将体系结构信息作为参数传递,以便跳过剩余体系结构的构建。我知道 -DANDROID_ABI 标志作为参数传递给 cmake 可以解决问题,但不确定如何通过 gradlew 命令将其作为参数传递?

defaultConfig {
    minSdkVersion 21
    targetSdkVersion 26

    externalNativeBuild {
        cmake {
            cppFlags "-frtti -fexceptions"
            arguments "-DANDROID_ABI=<<requested arch to built>>"
        }
    }
}

换句话说,如何将此信息从 gradlew 命令传递到 cmake?

技巧可以像下面这样:

android {   
    ...
    defaultConfig {
        externalNativeBuild {
            cmake {
                ...

                if (project.hasProperty("armeabi-v7a")) {
                    abiFilters 'armeabi-v7a'
                } else if (project.hasProperty("arm64-v8a")) {
                    abiFilters 'arm64-v8a'
                } else if (project.hasProperty("x86")) {
                    abiFilters 'x86'
                } else if (project.hasProperty("x86_64")) {
                    abiFilters 'x86_64'
                } else {
                    abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
                }
                ...
            }
        }
    }
}

从命令行,您可以执行以下操作,例如只构建 abi armeabi-v7a

./gradlew externalNativeBuild -Parmeabi-v7a
https://developer.android.com/studio/build/gradle-tips#configure-separate-apks-per-abi

You can create multiple apks using same code.

android {
  ...
  splits {

    // Configures multiple APKs based on ABI.
    abi {

      // Enables building multiple APKs.
      enable true

      // By default all ABIs are included, so use reset() and include to specify that we only
      // want APKs for x86, armeabi-v7a, and mips.
      reset()

      // Specifies a list of ABIs that Gradle should create APKs for.
      include "x86", "armeabi-v7a", "mips"

      // Specify that we want to also generate a universal APK that includes all ABIs.
      universalApk true
    }
  }
}