有沒有辦法在編譯時將常量傳遞到externalNativeBuild gradle 外殼中,并能夠在代碼中使用它?
例如:
externalNativeBuild {
cmake {
arguments "-DTEST=" rootProject.ext.value
}
}
并且能夠以某種方式使用它,如下所示:
void foo() {
int bla = TEST;
....
}
請注意,上面的代碼不起作用,因為 C 編譯器無法識別 TEST,這正是我要解決的問題。
uj5u.com熱心網友回復:
最簡單的解決方案是使用add_definitions:
add_definitions(-DTEST=${TEST})
到您的 CMakeLists.txt 檔案。
另一種是使用組態檔
基于 Android Studio 的原生示例應用程式:
在與默認 native-lib.cpp 相同的檔案夾中創建檔案:config.h.in,內容如下:
#ifndef NATIVEAPP1_CONFIG_H_IN #define NATIVEAPP1_CONFIG_H_IN #cmakedefine TEST ${TEST} #endif //NATIVEAPP1_CONFIG_H_IN在 CMakeLists.txt 添加:
# This sets cmake variable to the one passed from gradle. # This TEST variable is available only in cmake but can be read in # config.h.in which is used by cmake to create config.h set(TEST ${TEST}) configure_file( config.h.in ${CMAKE_BINARY_DIR}/generated/config.h ) include_directories( ${CMAKE_BINARY_DIR}/generated/ )
(例如,在add_library電話上方添加它)
最后,添加
#include "config.h"您的 cod(例如在 native-lib.cpp 中)以使用TEST宏。重建
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/311270.html
標籤:安卓 等级 建造 android-ndk
