最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JavascriptInterface on Android dont work with APK in release mode - Stack Overflow

programmeradmin1浏览0评论

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: .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 mand: ~/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:

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./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 mand: ~/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:

Share Improve this question edited Dec 12, 2013 at 17:59 Paulo Coutinho asked Dec 12, 2013 at 17:07 Paulo CoutinhoPaulo Coutinho 7051 gold badge7 silver badges16 bronze badges 1
  • Hi @Nachi you are the best! The problem is solved. Thanks. You save my life. God bless you! – Paulo Coutinho Commented Dec 13, 2013 at 1:13
Add a ment  | 

2 Answers 2

Reset to default 6

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

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

See here for details.

At AndroidStudio 0.8.4 it create a file called proguard-rules.pro at the root of your app module when create a new App Project. And its contain the following message (which is the solution):

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in D:\Android\android-sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android./guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, unment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
-keepclassmembers class .package.YourWebViewJavaInterfaceClasse {
   public *;
}
发布评论

评论列表(0)

  1. 暂无评论