2015-10-07 72 views
0

在使用pro guard java脚本界面方法后不能在release版本中运行,并且相同的代码在开发模式下完美工作。 请帮助我错了!在Android proguard中java脚本函数没有调用webview按钮点击

代码如下

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. 
+0

添加您progurad –

+0

@PankajKumar我有我的后两码请检查 –

回答

0

搭配使用ProGuard代码和检查

myWebView.getSettings().setJavaScriptEnabled(true); 
+0

我已经应用此代码但仍无法使用 –

+0

@Nirav你应该为你的答案包括一个解释,而不仅仅是这里的代码 –