Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavascriptInterface on Android dont work with APK in release mode

I have a problem that im searching for a solution since two days ago. After frustrating searchs, i will post here:

I have created a sample app that have a webview and this webview open a url that have a link to call a android function. I follow it: http://developer.android.com/guide/webapps/webview.html#BindingJavaScript

Im using last AndroidStudio version and im using a galaxy s4 mini for test.

When i debug it, everything works correct and a toast is showed.

The problem is when i generate a RELEASE application with command: ~/Server/gradle-1.8/bin/gradle assembleRelease

The application open, but when i touch the link that call my native function, it dont work and nothing happen. But with debug mode, it works.

Can anyone help me?

An image to understand better:

enter image description here

like image 782
Paulo Coutinho Avatar asked Mar 22 '23 08:03

Paulo Coutinho


1 Answers

If Gradle is configured to use ProGuard, the @JavascriptInterface annotations need to be explicitly preserved.

-keep public class com.mypackage.MyClass$MyJavaScriptInterface
-keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface
-keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface { 
    <methods>; 
}
-keepattributes JavascriptInterface

See here for details.

like image 63
Nachi Avatar answered Apr 25 '23 16:04

Nachi