Skip to content

Commit 4408976

Browse files
committed
Fix failure to open snapshot on emulator of Api level 17
1 parent efa2abd commit 4408976

File tree

7 files changed

+214
-161
lines changed

7 files changed

+214
-161
lines changed

build-artifacts/project-template-gradle/src/main/java/com/tns/RuntimeHelper.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,15 @@ public static Runtime initRuntime(Application app)
117117
e.printStackTrace();
118118
}
119119

120-
StaticConfiguration config = new StaticConfiguration(logger, debugger, appName, null, rootDir,
121-
appDir, classLoader, dexDir, dexThumb, appConfig);
120+
String nativeLibDir = null;
121+
try {
122+
nativeLibDir = app.getPackageManager().getApplicationInfo(appName, 0).nativeLibraryDir;
123+
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
124+
e.printStackTrace();
125+
}
126+
127+
StaticConfiguration config = new StaticConfiguration(logger, debugger, appName, nativeLibDir, rootDir,
128+
appDir, classLoader, dexDir, dexThumb, appConfig);
122129

123130
runtime = Runtime.initializeRuntimeWithConfiguration(config);
124131

runtime/src/main/java/com/tns/Runtime.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import com.tns.bindings.ProxyGenerator;
3232

3333
public class Runtime {
34-
private native void initNativeScript(int runtimeId, String filesPath, boolean verboseLoggingEnabled, String packageName, Object[] v8Options, String callingDir, JsDebugger jsDebugger);
34+
private native void initNativeScript(int runtimeId, String filesPath, String nativeLibDir, boolean verboseLoggingEnabled, String packageName, Object[] v8Options, String callingDir, JsDebugger jsDebugger);
3535

3636
private native void runModule(int runtimeId, String filePath) throws NativeScriptException;
3737

@@ -443,10 +443,10 @@ private boolean isInitializedImpl() {
443443
}
444444

445445
public void init() {
446-
init(config.logger, config.debugger, config.appName, config.runtimeLibPath, config.rootDir, config.appDir, config.classLoader, config.dexDir, config.dexThumb, config.appConfig, dynamicConfig.callingJsDir);
446+
init(config.logger, config.debugger, config.appName, config.nativeLibDir, config.rootDir, config.appDir, config.classLoader, config.dexDir, config.dexThumb, config.appConfig, dynamicConfig.callingJsDir);
447447
}
448448

449-
private void init(Logger logger, Debugger debugger, String appName, File runtimeLibPath, File rootDir, File appDir, ClassLoader classLoader, File dexDir, String dexThumb, AppConfig appConfig, String callingJsDir) throws RuntimeException {
449+
private void init(Logger logger, Debugger debugger, String appName, String nativeLibDir, File rootDir, File appDir, ClassLoader classLoader, File dexDir, String dexThumb, AppConfig appConfig, String callingJsDir) throws RuntimeException {
450450
if (initialized) {
451451
throw new RuntimeException("NativeScriptApplication already initialized");
452452
}
@@ -471,7 +471,7 @@ private void init(Logger logger, Debugger debugger, String appName, File runtime
471471
jsDebugger = new JsDebugger(debugger, threadScheduler);
472472
}
473473

474-
initNativeScript(getRuntimeId(), Module.getApplicationFilesPath(), logger.isEnabled(), appName, appConfig.getAsArray(), callingJsDir, jsDebugger);
474+
initNativeScript(getRuntimeId(), Module.getApplicationFilesPath(), nativeLibDir, logger.isEnabled(), appName, appConfig.getAsArray(), callingJsDir, jsDebugger);
475475

476476
if (jsDebugger != null && this.runtimeId == 0) {
477477
jsDebugger.start();

runtime/src/main/java/com/tns/StaticConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class StaticConfiguration
77
public final Logger logger;
88
public final Debugger debugger;
99
public final String appName;
10-
public final File runtimeLibPath;
10+
public final String nativeLibDir;
1111
public final File rootDir;
1212
public final File appDir;
1313
public final ClassLoader classLoader;
@@ -16,12 +16,12 @@ public class StaticConfiguration
1616
public final AppConfig appConfig;
1717

1818
public StaticConfiguration(Logger logger, Debugger debugger,
19-
String appName, File runtimeLibPath, File rootDir, File appDir, ClassLoader classLoader,
19+
String appName, String nativeLibDir, File rootDir, File appDir, ClassLoader classLoader,
2020
File dexDir, String dexThumb, AppConfig appConfig) {
2121
this.logger = logger;
2222
this.debugger = debugger;
2323
this.appName = appName;
24-
this.runtimeLibPath = runtimeLibPath;
24+
this.nativeLibDir = nativeLibDir;
2525
this.rootDir = rootDir;
2626
this.appDir = appDir;
2727
this.classLoader = classLoader;

runtime/src/main/jni/Runtime.cpp

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "include/zipconf.h"
2424
#include <sstream>
2525
#include <dlfcn.h>
26+
#include "sys/system_properties.h"
2627

2728
using namespace v8;
2829
using 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

421422
bool 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);

runtime/src/main/jni/Runtime.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ namespace tns
3131

3232
static void Init(JavaVM *vm, void *reserved);
3333

34-
static void Init(JNIEnv *_env, jobject obj, int runtimeId, jstring filesPath, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger);
34+
static void Init(JNIEnv *_env, jobject obj, int runtimeId, jstring filesPath, jstring nativeLibsDir, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger);
3535

36-
void Init(jstring filesPath, bool verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger);
36+
void Init(jstring filesPath, jstring nativeLibsDir, bool verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger);
3737

3838
v8::Isolate* GetIsolate() const;
3939

@@ -81,7 +81,7 @@ namespace tns
8181
v8::Persistent<v8::Function> *m_gcFunc;
8282
volatile bool m_runGC;
8383

84-
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, jstring packageName, jstring callingDir, jobject jsDebugger, jstring profilerOutputDir);
84+
v8::Isolate* PrepareV8Runtime(const std::string& filesPath, jstring nativeLibsDir, jstring packageName, jstring callingDir, jobject jsDebugger, jstring profilerOutputDir);
8585
jobject ConvertJsValueToJavaObject(JEnv& env, const v8::Local<v8::Value>& value, int classReturnType);
8686

8787
static std::map<int, Runtime*> s_id2RuntimeCache;

runtime/src/main/jni/com_tns_Runtime.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
3131
return JNI_VERSION_1_6;
3232
}
3333

34-
extern "C" JNIEXPORT void Java_com_tns_Runtime_initNativeScript(JNIEnv *_env, jobject obj, jint runtimeId, jstring filesPath, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger)
34+
extern "C" JNIEXPORT void Java_com_tns_Runtime_initNativeScript(JNIEnv *_env, jobject obj, jint runtimeId, jstring filesPath, jstring nativeLibDir, jboolean verboseLoggingEnabled, jstring packageName, jobjectArray args, jstring callingDir, jobject jsDebugger)
3535
{
3636
try
3737
{
38-
Runtime::Init(_env, obj, runtimeId, filesPath, verboseLoggingEnabled, packageName, args, callingDir, jsDebugger);
38+
Runtime::Init(_env, obj, runtimeId, filesPath, nativeLibDir, verboseLoggingEnabled, packageName, args, callingDir, jsDebugger);
3939
}
4040
catch (NativeScriptException& e)
4141
{

0 commit comments

Comments
 (0)