Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ public NativeScriptApplication() {
}

public void onCreate() {
super.onCreate();
com.tns.Runtime runtime = RuntimeHelper.initRuntime(this);
if (runtime !=null) {
runtime.run();
ManualInstrumentation.Frame frame = ManualInstrumentation.start("NaitveScriptApplication.onCreate");
try {
super.onCreate();
com.tns.Runtime runtime = RuntimeHelper.initRuntime(this);
if (runtime != null) {
runtime.run();
}
} finally {
frame.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,157 +53,173 @@ public static Runtime initRuntime(Application app) {
return Runtime.getCurrentRuntime();
}

System.loadLibrary("NativeScript");

Logger logger = new LogcatLogger(app);
ManualInstrumentation.Frame frame = ManualInstrumentation.start("RuntimeHelper.initRuntime");
try {
ManualInstrumentation.Frame loadLibraryFrame = ManualInstrumentation.start("loadLibrary NativeScript");
try {
System.loadLibrary("NativeScript");
} finally {
loadLibraryFrame.close();
}

Runtime runtime = null;
boolean showErrorIntent = hasErrorIntent(app);
if (!showErrorIntent) {
NativeScriptUncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, app);
Logger logger = new LogcatLogger(app);

Thread.setDefaultUncaughtExceptionHandler(exHandler);
Runtime runtime = null;
boolean showErrorIntent = hasErrorIntent(app);
if (!showErrorIntent) {
NativeScriptUncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, app);

DefaultExtractPolicy extractPolicy = new DefaultExtractPolicy(logger);
boolean skipAssetExtraction = Util.runPlugin(logger, app);
Thread.setDefaultUncaughtExceptionHandler(exHandler);

String appName = app.getPackageName();
File rootDir = new File(app.getApplicationInfo().dataDir);
File appDir = app.getFilesDir();
DefaultExtractPolicy extractPolicy = new DefaultExtractPolicy(logger);
boolean skipAssetExtraction = Util.runPlugin(logger, app);

try {
appDir = appDir.getCanonicalFile();
} catch (IOException e1) {
}
String appName = app.getPackageName();
File rootDir = new File(app.getApplicationInfo().dataDir);
File appDir = app.getFilesDir();

if (!skipAssetExtraction) {
if (logger.isEnabled()) {
logger.write("Extracting assets...");
try {
appDir = appDir.getCanonicalFile();
} catch (IOException e1) {
}

AssetExtractor aE = new AssetExtractor(null, logger);
if (!skipAssetExtraction) {
ManualInstrumentation.Frame extractionFrame = ManualInstrumentation.start("Extracting assets");
try {
if (logger.isEnabled()) {
logger.write("Extracting assets...");
}

String outputDir = app.getFilesDir().getPath() + File.separator;
AssetExtractor aE = new AssetExtractor(null, logger);

// will force deletion of previously extracted files in app/files directories
// see https://github.com/NativeScript/NativeScript/issues/4137 for reference
boolean removePreviouslyInstalledAssets = true;
aE.extractAssets(app, "app", outputDir, extractPolicy, removePreviouslyInstalledAssets);
aE.extractAssets(app, "internal", outputDir, extractPolicy, removePreviouslyInstalledAssets);
aE.extractAssets(app, "metadata", outputDir, extractPolicy, false);
String outputDir = app.getFilesDir().getPath() + File.separator;

boolean shouldExtractSnapshots = true;
// will force deletion of previously extracted files in app/files directories
// see https://github.com/NativeScript/NativeScript/issues/4137 for reference
boolean removePreviouslyInstalledAssets = true;
aE.extractAssets(app, "app", outputDir, extractPolicy, removePreviouslyInstalledAssets);
aE.extractAssets(app, "internal", outputDir, extractPolicy, removePreviouslyInstalledAssets);
aE.extractAssets(app, "metadata", outputDir, extractPolicy, false);

// will extract snapshot of the device appropriate architecture
if (shouldExtractSnapshots) {
if (logger.isEnabled()) {
logger.write("Extracting snapshot blob");
}
boolean shouldExtractSnapshots = true;

aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy, removePreviouslyInstalledAssets);
}
// will extract snapshot of the device appropriate architecture
if (shouldExtractSnapshots) {
if (logger.isEnabled()) {
logger.write("Extracting snapshot blob");
}

extractPolicy.setAssetsThumb(app);
}
aE.extractAssets(app, "snapshots/" + Build.CPU_ABI, outputDir, extractPolicy, removePreviouslyInstalledAssets);
}

AppConfig appConfig = new AppConfig(appDir);

ClassLoader classLoader = app.getClassLoader();
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
String dexThumb = null;
try {
dexThumb = Util.getDexThumb(app);
} catch (NameNotFoundException e) {
if (logger.isEnabled()) {
logger.write("Error while getting current proxy thumb");
extractPolicy.setAssetsThumb(app);
} finally {
extractionFrame.close();
}
}
e.printStackTrace();
}

String nativeLibDir = null;
try {
nativeLibDir = app.getPackageManager().getApplicationInfo(appName, 0).nativeLibraryDir;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
e.printStackTrace();
}

boolean isDebuggable = Util.isDebuggableApp(app);
StaticConfiguration config = new StaticConfiguration(logger, appName, nativeLibDir, rootDir,
appDir, classLoader, dexDir, dexThumb, appConfig, isDebuggable);
AppConfig appConfig = new AppConfig(appDir);
ManualInstrumentation.setMode(appConfig.getProfilingMode());

runtime = Runtime.initializeRuntimeWithConfiguration(config);
if (isDebuggable) {
ClassLoader classLoader = app.getClassLoader();
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
String dexThumb = null;
try {
v8Inspector = new AndroidJsV8Inspector(app.getFilesDir().getAbsolutePath(), app.getPackageName());
v8Inspector.start();

// the following snippet is used as means to notify the VSCode extension
// debugger that the debugger agent has started
File debuggerStartedFile = new File("/data/local/tmp", app.getPackageName() + "-debugger-started");
if (debuggerStartedFile.exists() && !debuggerStartedFile.isDirectory() && debuggerStartedFile.length() == 0) {
java.io.FileWriter fileWriter = new java.io.FileWriter(debuggerStartedFile);
fileWriter.write("started");
fileWriter.close();
}

// check if --debug-brk flag has been set. If positive:
// write to the file to invalidate the flag
// inform the v8Inspector to pause the main thread
File debugBreakFile = new File("/data/local/tmp", app.getPackageName() + "-debugbreak");
boolean shouldBreak = false;
if (debugBreakFile.exists() && !debugBreakFile.isDirectory() && debugBreakFile.length() == 0) {
java.io.FileWriter fileWriter = new java.io.FileWriter(debugBreakFile);
fileWriter.write("started");
fileWriter.close();

shouldBreak = true;
dexThumb = Util.getDexThumb(app);
} catch (NameNotFoundException e) {
if (logger.isEnabled()) {
logger.write("Error while getting current proxy thumb");
}
e.printStackTrace();
}

v8Inspector.waitForDebugger(shouldBreak);
} catch (IOException e) {
String nativeLibDir = null;
try {
nativeLibDir = app.getPackageManager().getApplicationInfo(appName, 0).nativeLibraryDir;
} catch (android.content.pm.PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}

// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
if (NativeScriptSyncService.isSyncEnabled(app)) {
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, app);

syncService.sync();
syncService.startServer();

// preserve this instance as strong reference
// do not preserve in NativeScriptApplication field inorder to
// make the code more portable
// @@@
// Runtime.getOrCreateJavaObjectID(syncService);
} else {
if (logger.isEnabled()) {
logger.write("NativeScript LiveSync is not enabled.");
boolean isDebuggable = Util.isDebuggableApp(app);
StaticConfiguration config = new StaticConfiguration(logger, appName, nativeLibDir, rootDir,
appDir, classLoader, dexDir, dexThumb, appConfig, isDebuggable);

runtime = Runtime.initializeRuntimeWithConfiguration(config);
if (isDebuggable) {
try {
v8Inspector = new AndroidJsV8Inspector(app.getFilesDir().getAbsolutePath(), app.getPackageName());
v8Inspector.start();

// the following snippet is used as means to notify the VSCode extension
// debugger that the debugger agent has started
File debuggerStartedFile = new File("/data/local/tmp", app.getPackageName() + "-debugger-started");
if (debuggerStartedFile.exists() && !debuggerStartedFile.isDirectory() && debuggerStartedFile.length() == 0) {
java.io.FileWriter fileWriter = new java.io.FileWriter(debuggerStartedFile);
fileWriter.write("started");
fileWriter.close();
}

// check if --debug-brk flag has been set. If positive:
// write to the file to invalidate the flag
// inform the v8Inspector to pause the main thread
File debugBreakFile = new File("/data/local/tmp", app.getPackageName() + "-debugbreak");
boolean shouldBreak = false;
if (debugBreakFile.exists() && !debugBreakFile.isDirectory() && debugBreakFile.length() == 0) {
java.io.FileWriter fileWriter = new java.io.FileWriter(debugBreakFile);
fileWriter.write("started");
fileWriter.close();

shouldBreak = true;
}

v8Inspector.waitForDebugger(shouldBreak);
} catch (IOException e) {
e.printStackTrace();
}
}
}

runtime.runScript(new File(appDir, "internal/ts_helpers.js"));
// runtime needs to be initialized before the NativeScriptSyncService is enabled because it uses runtime.runScript(...)
if (NativeScriptSyncService.isSyncEnabled(app)) {
NativeScriptSyncService syncService = new NativeScriptSyncService(runtime, logger, app);

File javaClassesModule = new File(appDir, "app/tns-java-classes.js");
if (javaClassesModule.exists()) {
runtime.runModule(javaClassesModule);
}
syncService.sync();
syncService.startServer();

try {
// put this call in a try/catch block because with the latest changes in the modules it is not granted that NativeScriptApplication is extended through JavaScript.
JavaScriptImplementation jsImpl = app.getClass().getAnnotation(JavaScriptImplementation.class);
if (jsImpl != null) {
Runtime.initInstance(app);
// preserve this instance as strong reference
// do not preserve in NativeScriptApplication field inorder to
// make the code more portable
// @@@
// Runtime.getOrCreateJavaObjectID(syncService);
} else {
if (logger.isEnabled()) {
logger.write("NativeScript LiveSync is not enabled.");
}
}
} catch (Exception e) {
if (logger.isEnabled()) {
logger.write("Cannot initialize application instance.");

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

File javaClassesModule = new File(appDir, "app/tns-java-classes.js");
if (javaClassesModule.exists()) {
runtime.runModule(javaClassesModule);
}

try {
// put this call in a try/catch block because with the latest changes in the modules it is not granted that NativeScriptApplication is extended through JavaScript.
JavaScriptImplementation jsImpl = app.getClass().getAnnotation(JavaScriptImplementation.class);
if (jsImpl != null) {
Runtime.initInstance(app);
}
} catch (Exception e) {
if (logger.isEnabled()) {
logger.write("Cannot initialize application instance.");
}
e.printStackTrace();
}
e.printStackTrace();
}
return runtime;
} finally {
frame.close();
}
return runtime;
}

private static final String logTag = "MyApp";
Expand Down
Loading