2323#include " include/zipconf.h"
2424#include < sstream>
2525#include < dlfcn.h>
26+ #include " sys/system_properties.h"
2627
2728using namespace v8 ;
2829using namespace std ;
@@ -130,18 +131,18 @@ ObjectManager* Runtime::GetObjectManager() const
130131 return m_objectManager;
131132}
132133
133- void Runtime::Init (JNIEnv *_env, jobject obj, int runtimeId, jstring filesPath, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger)
134+ void Runtime::Init (JNIEnv *_env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibDir, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger)
134135{
135136 JEnv env (_env);
136137
137138 auto runtime = new Runtime (env, obj, runtimeId);
138139
139140 auto enableLog = verboseLoggingEnabled == JNI_TRUE;
140141
141- runtime->Init (filesPath, enableLog, packageName, args, callingDir, jsDebugger);
142+ runtime->Init (filesPath, nativeLibDir, enableLog, packageName, args, callingDir, jsDebugger);
142143}
143144
144- void Runtime::Init (jstring filesPath, bool verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger)
145+ void Runtime::Init (jstring filesPath, jstring nativeLibDir, bool verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger)
145146{
146147 LogEnabled = verboseLoggingEnabled;
147148
@@ -161,7 +162,7 @@ void Runtime::Init(jstring filesPath, bool verboseLoggingEnabled, jstring packag
161162 DEBUG_WRITE (" Initializing Telerik NativeScript" );
162163
163164 NativeScriptException::Init (m_objectManager);
164- m_isolate = PrepareV8Runtime (filesRoot, packageName, callingDir, jsDebugger, profilerOutputDir);
165+ m_isolate = PrepareV8Runtime (filesRoot, nativeLibDir, packageName, callingDir, jsDebugger, profilerOutputDir);
165166
166167 s_isolate2RuntimesCache.insert (make_pair (m_isolate, this ));
167168}
@@ -420,7 +421,7 @@ static void InitializeV8() {
420421
421422bool x = false ;
422423
423- Isolate* Runtime::PrepareV8Runtime (const string& filesPath, jstring packageName, jstring callingDir, jobject jsDebugger, jstring profilerOutputDir)
424+ Isolate* Runtime::PrepareV8Runtime (const string& filesPath, jstring nativeLibDir, jstring packageName, jstring callingDir, jobject jsDebugger, jstring profilerOutputDir)
424425{
425426 Isolate::CreateParams create_params;
426427 bool didInitializeV8 = false ;
@@ -429,8 +430,30 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
429430
430431 m_startupData = new StartupData ();
431432
432- void * snapshotPtr = dlopen (" libsnapshot.so" , RTLD_LAZY | RTLD_LOCAL);
433- if (snapshotPtr)
433+ // Retrieve the device android Sdk version
434+ char sdkVersion[PROP_VALUE_MAX];
435+ __system_property_get (" ro.build.version.sdk" , sdkVersion);
436+
437+ auto pckName = ArgConverter::jstringToString (packageName);
438+
439+ void * snapshotPtr;
440+
441+ // If device isn't running on Sdk 17
442+ if (strcmp (sdkVersion, string (" 17" ).c_str ()) != 0 ) {
443+ snapshotPtr = dlopen (" libsnapshot.so" , RTLD_LAZY | RTLD_LOCAL);
444+ } else {
445+ // If device is running on android Sdk 17
446+ // dlopen reads relative path to dynamic libraries or reads from folder different than the nativeLibsDirs on the android device
447+ string libDir = ArgConverter::jstringToString (nativeLibDir);
448+ string snapshotPath = libDir + " /libsnapshot.so" ;
449+ snapshotPtr = dlopen (snapshotPath.c_str (), RTLD_LAZY | RTLD_LOCAL);
450+ }
451+
452+ if (snapshotPtr == nullptr ) {
453+ DEBUG_WRITE_FORCE (" Failed to load snapshot: %s" , dlerror ());
454+ }
455+
456+ if (snapshotPtr)
434457 {
435458 m_startupData->data = static_cast <const char *>(dlsym (snapshotPtr, " TNSSnapshot_blob" ));
436459 m_startupData->raw_size = *static_cast <const unsigned int *>(dlsym (snapshotPtr, " TNSSnapshot_blob_len" ));
@@ -530,7 +553,7 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
530553 V8::SetCaptureStackTraceForUncaughtExceptions (true , 100 , StackTrace::kOverview );
531554
532555 isolate->AddMessageListener (NativeScriptException::OnUncaughtError);
533-
556+
534557 __android_log_print (ANDROID_LOG_DEBUG, " TNS.Native" , " V8 version %s" , V8::GetVersion ());
535558
536559 auto globalTemplate = ObjectTemplate::New ();
@@ -610,7 +633,6 @@ Isolate* Runtime::PrepareV8Runtime(const string& filesPath, jstring packageName,
610633
611634 CallbackHandlers::Init (isolate);
612635
613- auto pckName = ArgConverter::jstringToString (packageName);
614636 auto outputDir = ArgConverter::jstringToString (profilerOutputDir);
615637 m_profiler.Init (isolate, global, pckName, outputDir);
616638 JsDebugger::Init (isolate, pckName, jsDebugger);
0 commit comments