Skip to content

Commit

Permalink
Working conditional android compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
ospfranco committed Mar 12, 2024
1 parent 04e1b65 commit c22fedd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 20 deletions.
17 changes: 15 additions & 2 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()
8 changes: 5 additions & 3 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}
Expand Down Expand Up @@ -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".
Expand Down
11 changes: 5 additions & 6 deletions cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "sqlbatchexecutor.h"
#include "utils.h"
#include <iostream>
#include <sqlite3.h>
#include <string>
#include <unordered_map>
#include <vector>
Expand Down Expand Up @@ -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()) {
Expand Down
14 changes: 8 additions & 6 deletions cpp/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Expand Down Expand Up @@ -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));
}

Expand Down Expand Up @@ -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",
};
}
Expand Down
4 changes: 4 additions & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ android {
versionName "1.0"
}

packagingOptions {
pickFirst '**/libcrypto.so'
}

splits {
abi {
reset()
Expand Down
4 changes: 2 additions & 2 deletions example/src/tests/dbsetup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
});
Expand All @@ -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',
});
Expand Down
2 changes: 1 addition & 1 deletion example/src/tests/queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function queriesTests() {
}

db = open({
name: 'test',
name: 'queries.sqlite',
encryptionKey: 'test',
});

Expand Down

0 comments on commit c22fedd

Please sign in to comment.