Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set CmakeLists path in product flavor for each Android ABI?

I need to have a separate CMakeLists.txt for each Android ABI. I tried to use product flavor to set the path for CMakeLists.txt. But I am getting following error on running ./gradlew assembleDebug or any other gradle command from command line.

Could not find method path() for arguments [CMakeLists.txt] on object of type com.android.build.gradle.internal.dsl.ExternalNativeCmakeOptions.

Here is how I have set product flavor in build.gradle.

productFlavors {
    arm64_v8a {
        ndk {
            abiFilters "arm64-v8a"
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
    x86_64 {
        ndk {
            abiFilters "x86_64"
        }
        externalNativeBuild {
            cmake {
                path "CMakeLists.txt"
            }
        }
    }
}

NOTE - I had initially named the files as "CMakeLists_arm64-v8a.txt" and "CMakeLists_x86_64.txt". But that was failing so tried same name.

How to fix this or is there a workaround for this?

like image 634
Vinayak Garg Avatar asked May 17 '17 12:05

Vinayak Garg


People also ask

When would you use a product Flavour in your build setup?

123 let's go Simply put, a product flavor is a variant of your app. It is very useful when you want to create multiple versions of your app. This means you can generate different versions or variants of your app using a single codebase.

How do I get the current flavor in gradle?

gradle. getStartParameter(). getTaskRequests(). toString() contains your current flavor name but the first character is capital.

What is a flavor dimension?

Flavor Dimensions is a way to group flavors by a name. For now, we're using just a single group. Add the following line in your defaultConfig block: flavorDimensions "default" Now syncing the gradle would give you the following product flavors: Android Build Variants combine build types and product flavors.


1 Answers

No, you cannot have CMakeLists.txt paths different for different flavors and/or ABIs, but you can use the arguments to add conditionals in your cmake script, for example like this:

flavorDimensions "abi"
productFlavors {
    arm64_v8a {
        dimension "abi"
        ndk {
            abiFilters "arm64-v8a"
        }
        externalNativeBuild {
            cmake {
                arguments "-DFLAVOR=ARM"
            }
        }
    }
    x86_64 {
        dimension "abi"
        ndk {
            abiFilters "x86_64"
        }
        externalNativeBuild {
            cmake {
                arguments "-DFLAVOR=x86"
            }
        }
    }
}

Now you can check this in your CMakeLists.txt:

if (FLAVOR STREQUAL 'ARM')
  include(arm.cmake)
endif()

But in your case, you can rely on the argument that is defined by Android Studio, and don't need your own parameter:

if (ANDROID_ABI STREQUAL 'arm64-v8a')
  include(arm.cmake)
endif()

Actually, you probably don't need separate productFlavors at all, but rather use splits to produce thin APKs for each ABI.

like image 78
Alex Cohn Avatar answered Sep 23 '22 02:09

Alex Cohn