Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set cpp folder for a specific build flavor

I have an issue with adding a cpp folder and CMakelist.txt file to a specific flavor in android studio.

I was working on three separate apps with a unique database structure, so I tried to make these three apps as one application using build flavors and other related settings.

For the first two apps, it did so well, but for the last one, I faced an issue that I had no idea how to add cpp folder and CMakelist.txt file to that specific flavor using Gradle. As you may have guessed, the last application is NDK based and uses a CMakelist.txt file and has an activity that works with JNI.


android {
   ...
    defaultConfig {
        applicationId "com.example"
        ...
        externalNativeBuild {
            cmake {
                cppFlags ""
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    flavorDimensions 'program'
    productFlavors {
        first {
            dimension = 'program'
            applicationIdSuffix  = '.first'
        }
        second {
            dimension = 'program'
            applicationIdSuffix  = '.second'
        }
        third {
            dimension = 'program'
            applicationIdSuffix  = '.third'

            externalNativeBuild {
                cmake {
                    relativeProjectPath "src/third/cpp/CMakeLists.txt"
                    version "3.10.2"
                }
            }

            buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                    ndk { abiFilters "arm64-v8a" }
                }
                debug {
                    ndk { abiFilters "arm64-v8a" }
                }
            }
        }
    }

I am using a relativeProjectPath method in gradle, and expect that CMakelist.txt links with my cpp folders but nothing happens.

like image 739
Emad Razavi Avatar asked Oct 20 '25 01:10

Emad Razavi


1 Answers

I have not tried it that way. What worked for me in my previous projects is this.

android {
    ...
    flavorDimensions "program"
    productFlavors {
        ...
        third {
            dimension "program"
            externalNativeBuild.cmake {
                arguments "-DFLAVOR=THIRD_PROGRAM"
            }
        }
    }

    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    ...
}

Then in your CMakeList.txt, do this conditional test

if(${FLAVOR} STREQUAL "THIRD_PROGRAM")
    // build your cpp codes for flavor THIRD
else()
    // nothing to build?
endif()

Note that my CMakeList.txt is in app/ directory.

like image 117
user1506104 Avatar answered Oct 22 '25 16:10

user1506104



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!