From c22feddf3fb80ffe93b44f23b57cba12fbb2f0cd Mon Sep 17 00:00:00 2001 From: Oscar Franco Date: Tue, 12 Mar 2024 19:48:38 +0100 Subject: [PATCH] Working conditional android compilation --- android/CMakeLists.txt | 17 +++++++++++++++-- android/build.gradle | 8 +++++--- cpp/bindings.cpp | 11 +++++------ cpp/bridge.cpp | 14 ++++++++------ example/android/app/build.gradle | 4 ++++ example/src/tests/dbsetup.spec.ts | 4 ++-- example/src/tests/queries.spec.ts | 2 +- 7 files changed, 40 insertions(+), 20 deletions(-) diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 2489a597..b1369561 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -41,10 +41,16 @@ add_library( if (OP_SQLITE_USE_SQLCIPHER) target_sources(${PACKAGE_NAME} PRIVATE ../cpp/sqlcipher/sqlite3.h ../cpp/sqlcipher/sqlite3.c) + + add_definitions( + -DOP_SQLITE_USE_SQLCIPHER + -DSQLITE_HAS_CODEC + -DSQLITE_TEMP_STORE=2 + ) - add_definitions(-DOP_SQLITE_USE_SQLCIPHER) + find_package(openssl REQUIRED CONFIG) else() - target_sources(${PACKAGE_NAME} PRIVATE ../cpp/sqlite3.h ../cpp/sqlite3.c) + target_sources(${PACKAGE_NAME} PRIVATE ../cpp/sqlite3.h ../cpp/sqlite3.c) endif() set_target_properties( @@ -56,12 +62,19 @@ set_target_properties( find_package(ReactAndroid REQUIRED CONFIG) find_package(fbjni REQUIRED CONFIG) +find_library(LOG_LIB log) + target_link_libraries( ${PACKAGE_NAME} + ${LOG_LIB} fbjni::fbjni ReactAndroid::jsi ReactAndroid::turbomodulejsijni ReactAndroid::react_nativemodule_core android ) + +if (OP_SQLITE_USE_SQLCIPHER) + target_link_libraries(${PACKAGE_NAME} PRIVATE openssl::crypto) +endif() \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle index 90a1a933..f3e77845 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -92,9 +92,9 @@ android { cppFlags "-O2", "-fexceptions", "-frtti", "-std=c++1y", "-DONANDROID" abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a' - arguments "-DOP_SQLITE_USE_SQLCIPHER='${System.getenv("OP_SQLITE_USE_SQLCIPHER")}'", - "-DANDROID_STL=c++_shared", + arguments "-DANDROID_STL=c++_shared", "-DSQLITE_FLAGS='${SQLITE_FLAGS ? SQLITE_FLAGS : ''}'" + "-DOP_SQLITE_USE_SQLCIPHER='${System.getenv("OP_SQLITE_USE_SQLCIPHER") == '1'? 1 : 0}'" abiFilters (*reactNativeArchitectures()) } } @@ -140,10 +140,12 @@ repositories { } def kotlin_version = getExtOrDefault("kotlinVersion") - dependencies { implementation 'com.facebook.react:react-native' implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + if (System.getenv("OP_SQLITE_USE_SQLCIPHER") == '1') { + implementation('com.android.ndk.thirdparty:openssl:1.1.1q-beta-1') + } } // Resolves "LOCAL_SRC_FILES points to a missing file, Check that libfb.so exists or that its path is correct". diff --git a/cpp/bindings.cpp b/cpp/bindings.cpp index 3f7590ff..e48d8db5 100644 --- a/cpp/bindings.cpp +++ b/cpp/bindings.cpp @@ -8,7 +8,6 @@ #include "sqlbatchexecutor.h" #include "utils.h" #include -#include #include #include #include @@ -77,11 +76,11 @@ void install(jsi::Runtime &rt, } // TODO(osp) find a way to display the yellow box from c++ #else - if (!encryptionKey.empty()) { - // RCTLogWarn(@"Your message") - throw std::runtime_error("[OP SQLite] SQLCipher is not enabled, " - "encryption key is not allowed"); - } + // if (!encryptionKey.empty()) { + // // RCTLogWarn(@"Your message") + // throw std::runtime_error("[OP SQLite] SQLCipher is not enabled, " + // "encryption key is not allowed"); + // } #endif if (!location.empty()) { diff --git a/cpp/bridge.cpp b/cpp/bridge.cpp index 9a56341a..aa4b5dcf 100644 --- a/cpp/bridge.cpp +++ b/cpp/bridge.cpp @@ -64,8 +64,10 @@ BridgeResult opsqlite_open(std::string const &dbName, dbMap[dbName] = db; #ifdef OP_SQLITE_USE_SQLCIPHER - opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr, - nullptr, nullptr); + auto encryptionResult = + opsqlite_execute(dbName, "PRAGMA key = '" + encryptionKey + "'", nullptr, + nullptr, nullptr); + LOGD("Encrypting database"); #endif return BridgeResult{.type = SQLiteOk, .affectedRows = 0}; } @@ -330,7 +332,7 @@ sqlite3_stmt *opsqlite_prepare_statement(std::string const &dbName, if (statementStatus == SQLITE_ERROR) { const char *message = sqlite3_errmsg(db); - throw std::runtime_error("[op-sqlite] SQL statement error: " + + throw std::runtime_error("[op-sqlite] SQL prepare statement error: " + std::string(message)); } @@ -368,9 +370,9 @@ opsqlite_execute(std::string const &dbName, std::string const &query, const char *message = sqlite3_errmsg(db); return { .type = SQLiteError, - .message = "[op-sqlite] SQL statement error:" + - std::to_string(statementStatus) + - " description:" + std::string(message) + + .message = "[op-sqlite] SQL statement error on opsqlite_execute:\n" + + std::to_string(statementStatus) + " description:\n" + + std::string(message) + ". See error codes: https://www.sqlite.org/rescode.html", }; } diff --git a/example/android/app/build.gradle b/example/android/app/build.gradle index b46a6f80..1d3fac2f 100644 --- a/example/android/app/build.gradle +++ b/example/android/app/build.gradle @@ -59,6 +59,10 @@ android { versionName "1.0" } + packagingOptions { + pickFirst '**/libcrypto.so' + } + splits { abi { reset() diff --git a/example/src/tests/dbsetup.spec.ts b/example/src/tests/dbsetup.spec.ts index 5078ef3d..168ad2c5 100644 --- a/example/src/tests/dbsetup.spec.ts +++ b/example/src/tests/dbsetup.spec.ts @@ -9,7 +9,7 @@ export function dbSetupTests() { describe('DB setup tests', () => { it('Create in memory DB', async () => { let inMemoryDb = open({ - name: 'inMemoryTest', + name: 'inMemoryTest.sqlite', location: ':memory:', encryptionKey: 'test', }); @@ -25,7 +25,7 @@ export function dbSetupTests() { if (Platform.OS === 'android') { it('Create db in external directory Android', () => { let androidDb = open({ - name: 'AndroidSDCardDB', + name: 'AndroidSDCardDB.sqlite', location: ANDROID_EXTERNAL_FILES_PATH, encryptionKey: 'test', }); diff --git a/example/src/tests/queries.spec.ts b/example/src/tests/queries.spec.ts index 77d9453b..ef97a9ec 100644 --- a/example/src/tests/queries.spec.ts +++ b/example/src/tests/queries.spec.ts @@ -20,7 +20,7 @@ export function queriesTests() { } db = open({ - name: 'test', + name: 'queries.sqlite', encryptionKey: 'test', });