Skip to content

Commit 694fa48

Browse files
authored
update runtimehelper to invoke nativescriptsyncservice in debug only (#818)
1 parent 1b3cb97 commit 694fa48

File tree

2 files changed

+30
-60
lines changed

2 files changed

+30
-60
lines changed

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

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -181,45 +181,30 @@ public static Runtime initRuntime(Application app) {
181181
} catch (IOException e) {
182182
e.printStackTrace();
183183
}
184-
}
185-
186-
try {
187-
Class NativeScriptSyncService = Class.forName("com.tns.NativeScriptSyncService");
188-
Method isSyncEnabledMethod = NativeScriptSyncService.getMethod("isSyncEnabled", Context.class);
189-
Boolean isSyncEnabled = (Boolean)isSyncEnabledMethod.invoke(NativeScriptSyncService, app);
190184

185+
// if app is in debuggable mode run livesync service
191186
// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
192-
if (isSyncEnabled) {
187+
try {
188+
Class NativeScriptSyncService = Class.forName("com.tns.NativeScriptSyncService");
189+
193190
Constructor cons = NativeScriptSyncService.getConstructor(new Class[] {Runtime.class, Logger.class, Context.class});
194191
Object syncService = cons.newInstance(runtime, logger, app);
195192

196193
Method syncMethod = NativeScriptSyncService.getMethod("sync");
197194
syncMethod.invoke(syncService);
198-
199195
Method startServerMethod = NativeScriptSyncService.getMethod("startServer");
200196
startServerMethod.invoke(syncService);
201-
202-
// preserve this instance as strong reference
203-
// do not preserve in NativeScriptApplication field inorder to
204-
// make the code more portable
205-
// @@@
206-
// Runtime.getOrCreateJavaObjectID(syncService);
207-
} else {
208-
if (logger.isEnabled()) {
209-
logger.write("NativeScript LiveSync is not enabled.");
210-
}
197+
} catch (ClassNotFoundException e) {
198+
e.printStackTrace();
199+
} catch (NoSuchMethodException e) {
200+
e.printStackTrace();
201+
} catch (IllegalAccessException e) {
202+
e.printStackTrace();
203+
} catch (InvocationTargetException e) {
204+
e.printStackTrace();
205+
} catch (InstantiationException e) {
206+
e.printStackTrace();
211207
}
212-
213-
} catch (ClassNotFoundException e) {
214-
e.printStackTrace();
215-
} catch (NoSuchMethodException e) {
216-
e.printStackTrace();
217-
} catch (IllegalAccessException e) {
218-
e.printStackTrace();
219-
} catch (InvocationTargetException e) {
220-
e.printStackTrace();
221-
} catch (InstantiationException e) {
222-
e.printStackTrace();
223208
}
224209

225210
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));

test-app/app/src/main/java/com/tns/RuntimeHelper.java

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -181,45 +181,30 @@ public static Runtime initRuntime(Application app) {
181181
} catch (IOException e) {
182182
e.printStackTrace();
183183
}
184-
}
185-
186-
try {
187-
Class NativeScriptSyncService = Class.forName("com.tns.NativeScriptSyncService");
188-
Method isSyncEnabledMethod = NativeScriptSyncService.getMethod("isSyncEnabled", Context.class);
189-
Boolean isSyncEnabled = (Boolean)isSyncEnabledMethod.invoke(NativeScriptSyncService, app);
190184

185+
// if app is in debuggable mode run livesync service
191186
// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
192-
if (isSyncEnabled) {
193-
Constructor syncServiceConstructor = NativeScriptSyncService.getConstructor(new Class[] {Runtime.class, Logger.class, Context.class});
194-
Object syncService = syncServiceConstructor.newInstance(runtime, logger, app);
187+
try {
188+
Class NativeScriptSyncService = Class.forName("com.tns.NativeScriptSyncService");
189+
190+
Constructor cons = NativeScriptSyncService.getConstructor(new Class[] {Runtime.class, Logger.class, Context.class});
191+
Object syncService = cons.newInstance(runtime, logger, app);
195192

196193
Method syncMethod = NativeScriptSyncService.getMethod("sync");
197194
syncMethod.invoke(syncService);
198-
199195
Method startServerMethod = NativeScriptSyncService.getMethod("startServer");
200196
startServerMethod.invoke(syncService);
201-
202-
// preserve this instance as strong reference
203-
// do not preserve in NativeScriptApplication field inorder to
204-
// make the code more portable
205-
// @@@
206-
// Runtime.getOrCreateJavaObjectID(syncService);
207-
} else {
208-
if (logger.isEnabled()) {
209-
logger.write("NativeScript LiveSync is not enabled.");
210-
}
197+
} catch (ClassNotFoundException e) {
198+
e.printStackTrace();
199+
} catch (NoSuchMethodException e) {
200+
e.printStackTrace();
201+
} catch (IllegalAccessException e) {
202+
e.printStackTrace();
203+
} catch (InvocationTargetException e) {
204+
e.printStackTrace();
205+
} catch (InstantiationException e) {
206+
e.printStackTrace();
211207
}
212-
213-
} catch (ClassNotFoundException e) {
214-
e.printStackTrace();
215-
} catch (NoSuchMethodException e) {
216-
e.printStackTrace();
217-
} catch (IllegalAccessException e) {
218-
e.printStackTrace();
219-
} catch (InvocationTargetException e) {
220-
e.printStackTrace();
221-
} catch (InstantiationException e) {
222-
e.printStackTrace();
223208
}
224209

225210
runtime.runScript(new File(appDir, "internal/ts_helpers.js"));

0 commit comments

Comments
 (0)