From 40bd1d92749857a37ebea2fa87c2f064d1775a51 Mon Sep 17 00:00:00 2001 From: Gary Mathews Date: Tue, 26 Nov 2019 13:42:33 -0800 Subject: [PATCH] fix(android): prevent snapshots from failing build (#11367) * fix(android): prevent snapshots from failing build * fix(android): update V8Snapshots.h template * fix(android): amend rootActivity --- android/cli/commands/_build.js | 25 ++++------ .../appcelerator/titanium/TiApplication.java | 46 ++++++++++++------- .../titanium/TiLaunchActivity.java | 15 +----- build/lib/android/V8Snapshots.h.ejs | 16 ++++--- build/lib/android/index.js | 3 -- build/lib/android/snapshot.js | 4 +- 6 files changed, 51 insertions(+), 58 deletions(-) diff --git a/android/cli/commands/_build.js b/android/cli/commands/_build.js index 293a0972a39..79de673577d 100644 --- a/android/cli/commands/_build.js +++ b/android/cli/commands/_build.js @@ -2585,23 +2585,14 @@ AndroidBuilder.prototype.copyResources = function copyResources(next) { const tasks = [ // First copy all of the Titanium SDK's core JS files shared by all platforms. function (cb) { - // Check if a snapshot has been generated. - fs.stat(path.join(this.platformPath, 'native', 'include', 'V8Snapshots.h'), (error, stat) => { - // 'V8Snapshot.h' will always exists, check size to determin if a snapshot was generated. - if (error || stat.size <= 64) { - const src = path.join(this.titaniumSdkPath, 'common', 'Resources', 'android'); - warnDupeDrawableFolders.call(this, src); - _t.logger.debug(__('Copying %s', src.cyan)); - copyDir.call(this, { - src: src, - dest: this.buildBinAssetsResourcesDir, - ignoreRootDirs: ti.allPlatformNames - }, cb); - return; - } - // Do not copy 'common' bundle over, as it is included in our snapshot. - return cb(); - }); + const src = path.join(this.titaniumSdkPath, 'common', 'Resources', 'android'); + warnDupeDrawableFolders.call(this, src); + _t.logger.debug(__('Copying %s', src.cyan)); + copyDir.call(this, { + src: src, + dest: this.buildBinAssetsResourcesDir, + ignoreRootDirs: ti.allPlatformNames + }, cb); }, // Next, copy all files in the project's Resources directory, diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java b/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java index ec977a5d8ac..53f955b6375 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiApplication.java @@ -769,6 +769,35 @@ public boolean isCoverageEnabled() return false; } + public static void launch() + { + final TiRootActivity rootActivity = TiApplication.getInstance().getRootActivity(); + if (rootActivity == null) { + return; + } + + // Fetch a path to the main script that was last loaded. + String appPath = rootActivity.getUrl(); + if ((appPath == null) || appPath.isEmpty()) { + return; + } + appPath = "Resources/" + appPath; + + final KrollRuntime runtime = KrollRuntime.getInstance(); + final boolean hasSnapshot = runtime.evalString("global._startSnapshot") != null; + if (hasSnapshot) { + + // Snapshot available, start snapshot. + runtime.doRunModule("global._startSnapshot(global)", appPath, rootActivity.getActivityProxy()); + + } else { + + // Could not find snapshot, fallback to launch script. + runtime.doRunModuleBytes(KrollAssetHelper.readAssetBytes(appPath), appPath, + rootActivity.getActivityProxy()); + } + } + public void softRestart() { // Fetch the root activity hosting the JavaScript runtime. @@ -795,13 +824,6 @@ public void softRestart() return; } - // Fetch a path to the main script that was last loaded. - String appPath = rootActivity.getUrl(); - if ((appPath == null) || appPath.isEmpty()) { - return; - } - appPath = "Resources/" + appPath; - // Prevent termination of root activity. boolean canFinishRoot = TiBaseActivity.canFinishRoot; TiBaseActivity.canFinishRoot = false; @@ -820,15 +842,7 @@ public void softRestart() runtime.initRuntime(); // manually re-launch app - if (KrollAssetHelper.assetExists(appPath)) { - runtime.doRunModuleBytes(KrollAssetHelper.readAssetBytes(appPath), appPath, - rootActivity.getActivityProxy()); - - // launch script does not exist, must be using snapshot - // execute startup method baked in snapshot - } else { - runtime.doRunModule("global._startSnapshot(global)", appPath, rootActivity.getActivityProxy()); - } + TiApplication.launch(); } @Override diff --git a/android/titanium/src/java/org/appcelerator/titanium/TiLaunchActivity.java b/android/titanium/src/java/org/appcelerator/titanium/TiLaunchActivity.java index a49cf4c054e..5b648aef3cb 100644 --- a/android/titanium/src/java/org/appcelerator/titanium/TiLaunchActivity.java +++ b/android/titanium/src/java/org/appcelerator/titanium/TiLaunchActivity.java @@ -95,20 +95,7 @@ protected String resolveUrl(TiUrl url) protected void loadScript() { - try { - String fullUrl = resolveUrl(this.url); - if (KrollAssetHelper.assetExists(fullUrl)) { - KrollRuntime.getInstance().runModuleBytes(KrollAssetHelper.readAssetBytes(fullUrl), fullUrl, - activityProxy); - - // launch script does not exist, must be using snapshot - // execute startup method baked in snapshot - } else { - KrollRuntime.getInstance().runModule("global._startSnapshot(global)", fullUrl, activityProxy); - } - } finally { - Log.d(TAG, "Signal JS loaded", Log.DEBUG_MODE); - } + TiApplication.launch(); } @Override diff --git a/build/lib/android/V8Snapshots.h.ejs b/build/lib/android/V8Snapshots.h.ejs index a6556e2ce78..7399eafe4ee 100644 --- a/build/lib/android/V8Snapshots.h.ejs +++ b/build/lib/android/V8Snapshots.h.ejs @@ -1,3 +1,4 @@ +<% if (blobs && blobs.length > 0) { -%> #ifndef V8_SNAPSHOT_H #define V8_SNAPSHOT_H @@ -12,13 +13,16 @@ static const unsigned char snapshot_data[] = { <% } -%> <%- blob.readUInt8(blob.length - 1) %> }; -#endif -<% } -%> -<% DEFINE_SNAPSHOT('i386', x86) -%> -<% DEFINE_SNAPSHOT('arm', arm) -%> -<% DEFINE_SNAPSHOT('aarch64', arm64) -%> static const int snapshot_size = sizeof(snapshot_data); static v8::StartupData snapshot = { (const char*) snapshot_data, snapshot_size }; +#endif +<% } -%> +<% if (blobs.x86) DEFINE_SNAPSHOT('i386', blobs.x86) -%> +<% if (blobs.arm) DEFINE_SNAPSHOT('arm', blobs.arm) -%> +<% if (blobs.arm64) DEFINE_SNAPSHOT('aarch64', blobs.arm64) -%> -#endif \ No newline at end of file +#endif +<% } else { -%> +// GENERATED AT BUILD TIME +<% } -%> \ No newline at end of file diff --git a/build/lib/android/index.js b/build/lib/android/index.js index 708fd3f0ee8..9cc94eb0f18 100644 --- a/build/lib/android/index.js +++ b/build/lib/android/index.js @@ -124,9 +124,6 @@ class Android { // Copy android/modules/*/lib/*.jar await this.copyModuleLibraries(path.join(ANDROID_ROOT, 'modules'), ANDROID_DEST); - // Discard local changes on the generated V8Snapshots.h - await git.discardLocalChange(ANDROID_ROOT, 'runtime/v8/src/native/V8Snapshots.h'); - // Copy over module resources const filterRegExp = new RegExp('\\' + path.sep + 'android(\\' + path.sep + 'titanium-(.+)?.(jar|res.zip|respackage))?$'); // eslint-disable-line security/detect-non-literal-regexp return fs.copy(DIST_ANDROID, ANDROID_MODULES, { filter: src => filterRegExp.test(src) }); diff --git a/build/lib/android/snapshot.js b/build/lib/android/snapshot.js index af627b89bc3..cbc1a03b0ab 100644 --- a/build/lib/android/snapshot.js +++ b/build/lib/android/snapshot.js @@ -66,7 +66,7 @@ async function generateBlob(target) { console.warn(`Generating snapshot blob for ${target}...`); // Generate snapshot - return promisify(exec)(MKSNAPSHOT_PATH, args); + return promisify(exec)(MKSNAPSHOT_PATH, args).catch(e => console.warn(`Could not generate blob for ${target}: ${e.message}`)); } /** @@ -89,7 +89,7 @@ async function generateHeader() { console.log(`Generating V8Snapshots.h for ${Object.keys(blobs).join(', ')}...`); // Generate 'V8Snapshots.h' from template - const output = await promisify(ejs.renderFile)(path.join(__dirname, 'V8Snapshots.h.ejs'), blobs, {}); + const output = await promisify(ejs.renderFile)(path.join(__dirname, 'V8Snapshots.h.ejs'), { blobs }, {}); return fs.writeFile(path.join(ANDROID_DIR, 'runtime', 'v8', 'src', 'native', 'V8Snapshots.h'), output); }