在Android proguard java脚本函数中没有调用webview按钮单击

时间:2015-10-07 09:03:17

标签: javascript android webview

使用pro guard java脚本接口方法无法在发布模式下工作并且相同的代码在开发模式下完美运行。 请帮助我错了!

代码低于

public class Tab_Login extends Fragment
{

    View rootView;
    MyJavaScriptInterface myJavaScriptInterface;
    static WebView myWebView;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {

        rootView = inflater.inflate(R.layout.login, container, false);

        myWebView.loadUrl("file:///android_asset/Login.html");
        myJavaScriptInterface = new MyJavaScriptInterface(getActivity());
        myWebView.addJavascriptInterface(myJavaScriptInterface, "Android");

    }

    public class MyJavaScriptInterface
    {
        Context mContext;

        public MyJavaScriptInterface(Context c)
        {
            this.mContext = c;
        }

        @JavascriptInterface
        public void loginBuy()
        {
            try
            {
                getActivity().runOnUiThread(new Runnable()
                {

                    @Override
                    public void run()
                    {
                        // TODO Auto-generated method stub
                        Toast.makeText(mContext, "Method call", Toast.LENGTH_LONG).show();

                    }
                });

            }
            catch (Exception e)
            {

            }
        }

    }
}

对于下面的程序代码

    // open this line
    proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

// add these line
-keep public class com.xyz.Tab_Login.MyJavaScriptInterface

-keep public class * implements com.xyz.Tab_Login.MyJavaScriptInterface

-keepclassmembers class com.xyz.Tab_Login.MyJavaScriptInterface { 
  <fields>;
    <methods>; 
}

-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

-keepattributes JavascriptInterface.

1 个答案:

答案 0 :(得分:0)

使用下面的行并检查

myWebView.getSettings().setJavaScriptEnabled(true);