To expose Java objects to JavaScript, use the Hummer annotation processor.
- Configure
HUMMER_MODULE_NAME in your module's gradle file. - Add
com.didi.hummer:hummer-compiler as an annotationProcessor dependency. - Annotate your class with
@Component("Name") and its properties/methods with @JsProperty or @JsMethod respectively.
// 1. Module gradle configuration
android {
defaultConfig {
...
javaCompileOptions {
annotationProcessorOptions {
arguments = [HUMMER_MODULE_NAME: project.getName()]
}
}
}
}
// 2. Add annotation processor dependency
dependencies {
...
annotationProcessor 'com.didi.hummer:hummer-compiler:0.2.15'
}
// 3. Define the exported class
@Component("TestExportModel")
public class TestExportModel {
@JsProperty("text")
public String text;
@JsProperty("floatValue")
public float floatValue;
public float getFloatValue() {
return floatValue;
}
@JsProperty("style")
private Map<String, Object> style;
public void setStyle(Map<String, Object> style) {
this.style = style;
}
public Map<String, Object> getStyle() {
return style;
}
@JsProperty("array")
private List<String> array;
public void setArray(List<String> array) {
this.array = array;
}
public List<String> getArray() {
return array;
}
@JsMethod("getElementById")
public String getSubview(String viewID, int index, Long test, HashMap<String, String> testMap, ArrayList<Object> testList) {
return "getSubview func, " + viewID + ", map: " + testMap + ", list: " + testList;
}
}