Search code examples
androidandroid-ndkandroid-productflavors

Different ndk code for flavor


I know how to run a different Java code for different flavors, and I have a Java class that implements the code I need for each flavor. However, what do I do if I want to implement a different C++ (NDK) for each flavor? How do I setup access to different class or h file for the flavors? Any ideas?


Solution

  • Use targets to select target libraries for each flavor in your build.gradle and define libraries in your CMakeLists.txt.

    From guide https://developer.android.com/studio/projects/gradle-external-native-builds?hl=ja#pass-args-flags

    productFlavors {
        ...
        demo {
            ...
            externalNativeBuild {
                cmake {
                    ...
                    targets "native-lib-demo"
                }
            }
        }
    
        paid {
            ...
            externalNativeBuild {
                cmake {
                    ...
                    targets "native-lib-paid",
                }
            }
        }
    }
    

    To check which library is included, use Build > Analyze APK and look inside the lib directory.