android webview中調用了js的時候混淆注意事項

來源:互聯網
上載者:User

android webview中調用了js的時候混淆注意事項

今天有一個bug,就是webview跟js互動的方法怎麼也調不起來,debug包問題,release包就出錯,想想是打包時混淆的問題,打了一個不混淆的包,果不其然,就是混淆的問題。

然後就找解決方案,在proguard-project檔案中有這麼一句

# If your project uses WebView with JS, uncomment the following# and specify the fully qualified class name to the JavaScript interface# class:#-keepclassmembers class fqcn.of.javascript.interface.for.webview {#   public *;#}
把注釋解除,把fqcn.of.javascript.interface.for.webview換成你自己定義的那個類名(包名也必須有,如果定義的是內部類,則是cn.wj.ui.WebViewActivity$myInterface),在4.1的系統上是沒有問題了,但4.2的機子上還是不行,再找找,哦,原來是4.2以上版本調用js介面需要在方法使用聲明@JavascriptInterface,然後混淆時可能會弄丟該聲明導致,程式無法調用js,需要繼續再設定檔中添加條件,

-keepattributes *Annotation*
-keepattributes *JavascriptInterface*


所以當使用了webview和js互動時,混淆檔案中應該添上這些配置:

-keepclassmembers class cn.xx.xx.Activity$AppAndroid {  public *;}-keepattributes *Annotation*-keepattributes *JavascriptInterface*


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.