我正在遵循 this 教程,以便使用 C++ 在我的 Android 项目中实现 firebase。我已按照教程所示执行所有步骤,但是当我运行我的项目时,我收到此错误消息:
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}")
构建.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
}
该错误抱怨目录
olympic/firebase_cpp_sdk
不存在。从 add_subdirectory()
的文档来看,此命令的第一个参数可以是相对或绝对路径:
如果是相对路径,则会根据当前目录(典型用法)进行评估,但它也可能是绝对路径。
由于您提供了相对路径,CMake 将查找
olympic/firebase_cpp_sdk
相对于当前正在处理的 CMakeLists.txt 文件;您的系统上不存在此路径。为了确保 CMake 可以找到 firebase_cpp_sdk
目录,请尝试指定一个 absolute 路径,如本步骤教程中所建议:
在项目的 gradle.properties 文件中指定解压后的 SDK 的位置:
systemProp.firebase_cpp_sdk.dir=full-path-to-SDK
所以尝试完整路径:
systemProp.firebase_cpp_sdk.dir=/your/full/path/to/SDK
您必须先指定“firebase sdk”的路径:
# Add Firebase libraries to the target using the function from the SDK.
add_subdirectory(${FIREBASE_CPP_SDK_DIR} bin/ EXCLUDE_FROM_ALL)
然后将此路径设置为cmake变量“FIREBASE_CPP_SDK_DIR”:
set(FIREBASE_CPP_SDK_DIR "${CMAKE_CURRENT_LIST_DIR}/<some relative path inside of your project>")
where
CMAKE_CURRENT_LIST_DIR
- 当前 CMakeLists.txt 所在的路径。
删除
node_modules
删除package-lock.json
奔跑吧npm install
然后运行 Android 项目解决了我的问题。