【Android】不使用WebView來執行Javascript指令碼(Rhino)

農民伯伯發表於2013-12-05

 

 

前言

動態執行指令碼能有效的降低重要功能硬編碼帶來的問題,尤其是依賴於第三方的應用,可以通過動態指令碼+線上引數(例如友盟線上引數)再不更新應用的情況下升級功能。


宣告

歡迎轉載,但請保留文章原始出處:) 
部落格園:http://www.cnblogs.com
農民伯伯: http://over140.cnblogs.com 

 

正文

一、準備

專案地址:https://developer.mozilla.org/en-US/docs/Rhino

專案說明:由Mozilla開發,是 JavaScript 一種基於Java的實現。

專案使用:把js.jar拷貝到專案工程既可。 

 

二、 程式碼

主要實現了從Java中執行js中的函式、從js中呼叫Java中的方法,程式碼:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text1 = (TextView) findViewById(android.R.id.text1);
        TextView text2 = (TextView) findViewById(android.R.id.text2);

        text1.setText(runScript(JAVA_CALL_JS_FUNCTION, "Test", new String[] {}));

        text2.setText(runScript(JS_CALL_JAVA_FUNCTION, "Test", new String[] {}));
    }

    /** Java執行js的方法 */
    private static final String JAVA_CALL_JS_FUNCTION = "function Test(){ return '農民伯伯 java call js Rhino'; }";

    /** js呼叫Java中的方法 */
    private static final String JS_CALL_JAVA_FUNCTION = //
    "var ScriptAPI = java.lang.Class.forName(\"" + MainActivity.class.getName() + "\", true, javaLoader);" + //
        "var methodRead = ScriptAPI.getMethod(\"jsCallJava\", [java.lang.String]);" + //
        "function jsCallJava(url) {return methodRead.invoke(null, url);}" + //
        "function Test(){ return jsCallJava(); }";

    /**
     * 執行JS
     * 
     * 
@param js js程式碼
     * 
@param functionName js方法名稱
     * 
@param functionParams js方法引數
     * 
@return
     
*/
    public String runScript(String js, String functionName, Object[] functionParams) {
        Context rhino = Context.enter();
        rhino.setOptimizationLevel(-1);
        try {
            Scriptable scope = rhino.initStandardObjects();

            ScriptableObject.putProperty(scope, "javaContext", Context.javaToJS(MainActivity.this, scope));
            ScriptableObject.putProperty(scope, "javaLoader", Context.javaToJS(MainActivity.class.getClassLoader(), scope));

            rhino.evaluateString(scope, js, "MainActivity", 1, null);

            Function function = (Function) scope.get(functionName, scope);

            Object result = function.call(rhino, scope, scope, functionParams);
            if (result instanceof String) {
                return (String) result;
            } else if (result instanceof NativeJavaObject) {
                return (String) ((NativeJavaObject) result).getDefaultValue(String.class);
            } else if (result instanceof NativeObject) {
                return (String) ((NativeObject) result).getDefaultValue(String.class);
            }
            return result.toString();//(String) function.call(rhino, scope, scope, functionParams);
        } finally {
            Context.exit();
        }
    }

    public static String jsCallJava(String url) {
        return "農民伯伯 js call Java Rhino";
    }
}

 

三、下載

SampleRhino.zip(2013-12-05) 

 

四、相關文章

4.1 Embed JavaScript in Android Java Code with Rhino 

4.2 JavaScript (Rhino) on Android 

4.3 純java 的javascript引擎:rhino 

4.4 深入淺出Rhino:Java與JS互操作 

 

五、其他

注意,混淆的時候js.jar可能混淆不過去,請參照文章4.1的方法。

 

相關文章