Skip to content

Commit 5607b77

Browse files
authored
Supports Android-like build on desktop (#1529)
Such environment is set up when one wants to run unit tests in Android Studio. Unit tests are run on host (not in emulator), but JNI and even many Android's core API like Context are still provided by Robolectric. On the other hand typical Android C++ headers like <android/log.h> are not accessible. This patch adds checks for macro ANDROID_HOST which is defined on such enviroment. Relates-To: HERESDK-1085 Signed-off-by: Yauheni Khnykin <yauheni.khnykin@here.com>
1 parent 9c8701d commit 5607b77

File tree

6 files changed

+25
-18
lines changed

6 files changed

+25
-18
lines changed

olp-cpp-sdk-core/include/olp/core/context/Context.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
#include <olp/core/CoreApi.h>
2828

29-
#ifdef ANDROID
29+
#if defined(ANDROID) || defined(ANDROID_HOST)
3030
#include <jni.h>
3131
#endif
3232

@@ -98,7 +98,7 @@ class CORE_API Context {
9898
*/
9999
Scope();
100100

101-
#ifdef ANDROID
101+
#if defined(ANDROID) || defined(ANDROID_HOST)
102102
/**
103103
* @brief Creates the `Scope` instance.
104104
*
@@ -147,13 +147,13 @@ class CORE_API Context {
147147
/// deinitialize the Context
148148
static void deinit();
149149

150-
#ifdef ANDROID
150+
#if defined(ANDROID) || defined(ANDROID_HOST)
151151
/// initialize the Context
152152
static void init(JavaVM* vm, jobject application);
153153
#endif
154154

155155
public:
156-
#ifdef ANDROID
156+
#if defined(ANDROID) || defined(ANDROID_HOST)
157157
/**
158158
* @brief Gets the `JavaVM` object.
159159
*

olp-cpp-sdk-core/src/context/Context.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <windows.h>
3030
#undef min
3131
#undef max
32-
#elif defined(ANDROID)
32+
#elif defined(ANDROID) || defined(ANDROID_HOST)
3333
#include <jni.h>
3434
#endif
3535

@@ -73,7 +73,7 @@ void Context::addInitializeCallbacks(InitializedCallback initCallback,
7373

7474
void Context::init() {
7575
auto cd = Instance();
76-
#ifdef ANDROID
76+
#if defined(ANDROID) || defined(ANDROID_HOST)
7777
cd->java_vm = nullptr;
7878
#else
7979
(void)cd;
@@ -82,7 +82,7 @@ void Context::init() {
8282
}
8383

8484
void Context::deinit() {
85-
#ifdef ANDROID
85+
#if defined(ANDROID) || defined(ANDROID_HOST)
8686
auto cd = Instance();
8787
// Release the global reference of Context.
8888
// Technically not needed if we took the application context,
@@ -92,7 +92,12 @@ void Context::deinit() {
9292
bool attached = false;
9393
if (cd->java_vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) !=
9494
JNI_OK) {
95-
if (cd->java_vm->AttachCurrentThread(&env, nullptr) != JNI_OK) {
95+
#if defined(ANDROID)
96+
auto envPtr = &env;
97+
#else
98+
auto envPtr = reinterpret_cast<void**>(&env);
99+
#endif
100+
if (cd->java_vm->AttachCurrentThread(envPtr, nullptr) != JNI_OK) {
96101
env = nullptr;
97102
} else {
98103
attached = true;
@@ -111,7 +116,7 @@ void Context::deinit() {
111116
deinitialize();
112117
}
113118

114-
#if defined(ANDROID)
119+
#if defined(ANDROID) || defined(ANDROID_HOST)
115120
void Context::init(JavaVM* vm, jobject context) {
116121
auto cd = Instance();
117122
cd->java_vm = vm;
@@ -189,7 +194,7 @@ Context::Scope::Scope() : m_cd(Instance()) {
189194
Context::init();
190195
}
191196
}
192-
#ifdef ANDROID
197+
#if defined(ANDROID) || defined(ANDROID_HOST)
193198
/// see Context::init()
194199
Context::Scope::Scope(JavaVM* vm, jobject application) : m_cd(Instance()) {
195200
std::lock_guard<std::mutex> lock(m_cd->context_mutex);

olp-cpp-sdk-core/src/context/ContextInternal.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929
#include <string>
3030
#endif
3131

32-
#ifdef ANDROID
32+
#if defined(ANDROID) || defined(ANDROID_HOST)
3333
#include <jni.h>
34-
#elif defined(__APPLE__)
34+
#endif
35+
36+
#if defined(__APPLE__)
3537
#include <olp/core/context/EnterBackgroundSubscriber.h>
3638
#endif
3739

@@ -45,7 +47,7 @@ struct ContextData {
4547

4648
std::mutex context_mutex;
4749
size_t context_instance_counter = 0;
48-
#ifdef ANDROID
50+
#if defined(ANDROID) || defined(ANDROID_HOST)
4951
JavaVM* java_vm = nullptr;
5052
jobject context = nullptr;
5153
#elif defined(__APPLE__)

olp-cpp-sdk-core/src/http/android/NetworkAndroid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#pragma once
2121

22-
#ifdef __ANDROID__
22+
#if defined(__ANDROID__) || defined(ANDROID_HOST)
2323

2424
#include <condition_variable>
2525
#include <future>

olp-cpp-sdk-core/src/http/curl/NetworkCurl.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ namespace {
5454

5555
const char* kLogTag = "CURL";
5656

57-
#ifdef OLP_SDK_ENABLE_ANDROID_CURL
57+
#if defined(OLP_SDK_ENABLE_ANDROID_CURL) && !defined(ANDROID_HOST)
5858
const auto kCurlAndroidCaBundleFolder = "/system/etc/security/cacerts";
5959

6060
#ifdef OLP_SDK_USE_MD5_CERT_LOOKUP
@@ -251,7 +251,7 @@ void GetTrafficData(CURL* handle, uint64_t& upload_bytes,
251251
CURLcode SetCaBundlePaths(CURL* handle) {
252252
OLP_SDK_CORE_UNUSED(handle);
253253

254-
#ifdef OLP_SDK_ENABLE_ANDROID_CURL
254+
#if defined(OLP_SDK_ENABLE_ANDROID_CURL) && !defined(ANDROID_HOST)
255255
// FIXME: We could disable this lookup as it won't work on most devices
256256
// (probably all of them) since OpenSSL still will be trying to find
257257
// certificate with SHA1 lookup
@@ -331,7 +331,7 @@ NetworkCurl::NetworkCurl(NetworkInitializationSettings settings)
331331
#endif
332332

333333
std::string ca_bundle_path;
334-
#ifdef OLP_SDK_ENABLE_ANDROID_CURL
334+
#if defined(OLP_SDK_ENABLE_ANDROID_CURL) && !defined(ANDROID_HOST)
335335
ca_bundle_path = kCurlAndroidCaBundleFolder;
336336
#elif OLP_SDK_NETWORK_HAS_OPENSSL
337337
ca_bundle_path = CaBundlePath();

olp-cpp-sdk-core/src/http/curl/NetworkCurl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#include <boost/optional.hpp>
3535

36-
#ifdef OLP_SDK_ENABLE_ANDROID_CURL
36+
#if defined(OLP_SDK_ENABLE_ANDROID_CURL) && !defined(ANDROID_HOST)
3737
#ifdef OLP_SDK_NETWORK_HAS_OPENSSL
3838
#include <openssl/ossl_typ.h>
3939

0 commit comments

Comments
 (0)