How to evaluate Javascript expression in Android using java ?

If you’re looking to make Android Application using javascript expression evaluate

then here is library called rhino

implementation 'org.mozilla:rhino:1.7R4'

write some code like this to get the result of JS evaluation

Context rhino = Context.enter()
// turn off optimization to work with android
rhino.optimizationLevel = -1

String evaluation = "2+2"

try {
    ScriptableProject scope = rhino.initStandardObjects()
    String result = rhino.evaluateString(scope, evaluation, "JavaScript", 1, null).toString()
} finally {
    Context.exit()
}

Leave a Comment