androidc++firebasecmake

CMake add_subdirectory given source which is not an existing directory


I'm following this tutorial in order to implement firebase in my android project using C++. I've followed all the steps as the tutorial shows, but when I run my project, I'm getting this error message:

 CMake Error at CMakeLists.txt:46 (add_subdirectory):
    add_subdirectory given source "olympic/firebase_cpp_sdk" which is not an
    existing directory.

CMakeLists.txt:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp)

# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})
# Add Firebase libraries to the target using the function from the SDK.
add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)


# The core Firebase library (firebase_app) is required to use any Firebase product,
# and it must always be listed last.
set(firebase_libs firebase_analytics firebase_app)
target_link_libraries(${target_name} "${firebase_libs}")

Build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"


    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    externalNativeBuild {
        cmake {
            path "src/main/cpp/CMakeLists.txt"
            version "3.10.2"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.deltadna.android:deltadna-sdk:4.11.3.1'
    implementation 'com.helpshift:android-helpshift-aar:7.+'
    implementation "com.mixpanel.android:mixpanel-android:5.+"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

apply plugin: 'com.google.gms.google-services'

android.defaultConfig.externalNativeBuild.cmake {
    arguments "-DFIREBASE_CPP_SDK_DIR=$gradle.firebase_cpp_sdk_dir"
}

apply from: "firebase_cpp_sdk/Android/firebase_dependencies.gradle"
firebaseCpp.dependencies {
    analytics
}

Solution

  • The error complains that the directory olympic/firebase_cpp_sdk does not exist. From the documentation for add_subdirectory(), the first argument to this command can be a relative or absolute path:

    If it is a relative path it will be evaluated with respect to the current directory (the typical usage), but it may also be an absolute path.

    Since you have provided a relative path, CMake will look for olympic/firebase_cpp_sdk relative to the current CMakeLists.txt file being processed; this path does not exist on your system. To ensure CMake can locate the firebase_cpp_sdk directory, try specifying an absolute path instead, as suggested in the tutorial in this step:

    1. Specify the location of the unzipped SDK in your project's gradle.properties file:

       systemProp.firebase_cpp_sdk.dir=full-path-to-SDK
      

    So try the full path:

    systemProp.firebase_cpp_sdk.dir=/your/full/path/to/SDK