Skip to content

Commit

Permalink
feat(android): improve sdk kroll-apt incremental build times (#11525)
Browse files Browse the repository at this point in the history
- Modified kroll-apt Java annotation processor to only regenerate C++ files if proxy bindings have changed.
  * Reduces incremental build time by 5 seconds on my machine.
  • Loading branch information
jquick-axway committed Mar 9, 2020
1 parent ff53751 commit 558b6ed
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class KrollBindingGenerator
private HashMap<String, Object> tiModules = new HashMap<String, Object>();

private JSONUtils jsonUtils;
private boolean canOverwrite = true;

public KrollBindingGenerator(String outPath, String moduleId)
{
Expand Down Expand Up @@ -88,15 +89,16 @@ protected void saveTypeTemplate(Template template, String outFile, Map<Object, O
Writer writer = null;
try {
File file = new File(outPath, outFile);
System.out.println("Generating " + file.getAbsolutePath());

File parent = file.getParentFile();
if (!parent.exists()) {
parent.mkdirs();
}

writer = new FileWriter(file);
template.process(root, writer);
if (this.canOverwrite || !file.exists()) {
System.out.println("Generating " + file.getAbsolutePath());
writer = new FileWriter(file);
template.process(root, writer);
}

} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -229,6 +231,16 @@ public void loadBindingsFromJsonFile(String jsonPath) throws ParseException, IOE
loadBindingsFrom(properties);
}

public boolean getCanOverwrite()
{
return this.canOverwrite;
}

public void setCanOverwrite(boolean value)
{
this.canOverwrite = value;
}

public void loadBindingsFrom(Map<Object, Object> properties)
{
if (properties == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package org.appcelerator.kroll.annotations.generator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
Expand Down Expand Up @@ -112,6 +113,7 @@ public class KrollJSONGenerator extends AbstractProcessor
protected String jarJsonFileName;
protected String jsonFilePath;
protected boolean initialized = false;
private boolean hasPropertiesChanged = true;

@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
Expand Down Expand Up @@ -756,8 +758,14 @@ protected void generateFullAPIName(Map<String, Object> proxyAttrs)

protected void generateJSON()
{
// Flag that JSON bindings/properties have changed since last write, unless the below says otherwise.
this.hasPropertiesChanged = true;

// Generate a JSON string from the "properties" dictionary.
String jsonString = JSONValue.toJSONString(this.properties);
if (jsonString == null) {
jsonString = "";
}

// Write a JSON file to the Java project we just read the annotations from.
// This will cause the JSON file to be bundled into the project's JAR file.
Expand All @@ -777,19 +785,39 @@ protected void generateJSON()

// Write a JSON file to the given file system path.
if (this.jsonFilePath != null) {
FileWriter writer = null;
try {
File filePath = new File(this.jsonFilePath);
filePath.getParentFile().mkdirs();
writer = new FileWriter(filePath);
writer.write(jsonString);
} catch (Exception e) {
debug("Exception trying to generate JSON file: %s, %s", this.jsonFilePath, e.getMessage());
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception e) {
// Determine if bindings have changed by reading last written JSON file, if it exists.
try (BufferedReader reader = new BufferedReader(new FileReader(this.jsonFilePath))) {
StringBuffer stringBuffer = new StringBuffer(Math.max(jsonString.length(), 32768));
char[] charBuffer = new char[2048];
while (true) {
int readBytes = reader.read(charBuffer, 0, charBuffer.length);
if (readBytes <= 0) {
break;
}
stringBuffer.append(charBuffer, 0, readBytes);
}
if (jsonString.contentEquals(stringBuffer)) {
this.hasPropertiesChanged = false;
}
} catch (Exception ex) {
}

// Write the JSON file if changed.
if (this.hasPropertiesChanged) {
FileWriter writer = null;
try {
File filePath = new File(this.jsonFilePath);
filePath.getParentFile().mkdirs();
writer = new FileWriter(filePath);
writer.write(jsonString);
} catch (Exception e) {
debug("Exception trying to generate JSON file: %s, %s", this.jsonFilePath, e.getMessage());
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception e) {
}
}
}
}
Expand Down Expand Up @@ -819,7 +847,12 @@ private void generateCppFiles()
KrollBindingGenerator generator = new KrollBindingGenerator(directoryPath, jsModuleName);
generator.loadBindingsFrom(this.properties);
if (tiBindingsJsonFilePath != null) {
// Load Titanium SDK library's bindings. We only do this for module builds.
generator.loadTitaniumBindingsFromJsonFile(tiBindingsJsonFilePath);
} else {
// Do incremental-like builds by only overwriting last C++ files if bindings have changed.
// Only do this for SDK builds. Can't do it for modules since we'd have to track SDK binding changes.
generator.setCanOverwrite(this.hasPropertiesChanged);
}
generator.generateBindings();
} catch (Exception ex) {
Expand Down

0 comments on commit 558b6ed

Please sign in to comment.