Skip to content

Commit

Permalink
fix(android): prevent snapshots from failing build (#11367)
Browse files Browse the repository at this point in the history
* fix(android): prevent snapshots from failing build

* fix(android): update V8Snapshots.h template

* fix(android): amend rootActivity
  • Loading branch information
garymathews committed Nov 26, 2019
1 parent fffbeb9 commit 40bd1d9
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 58 deletions.
25 changes: 8 additions & 17 deletions android/cli/commands/_build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions build/lib/android/V8Snapshots.h.ejs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<% if (blobs && blobs.length > 0) { -%>
#ifndef V8_SNAPSHOT_H
#define V8_SNAPSHOT_H
Expand All @@ -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
#endif
<% } else { -%>
// GENERATED AT BUILD TIME
<% } -%>
3 changes: 0 additions & 3 deletions build/lib/android/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
Expand Down
4 changes: 2 additions & 2 deletions build/lib/android/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`));
}

/**
Expand All @@ -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);
}

Expand Down

0 comments on commit 40bd1d9

Please sign in to comment.