2013-12-12 29 views
4

我有一个问题,即时通讯搜索解决方案,因为两天前。沮丧的搜索后,我会张贴在这里:Android上的JavascriptInterface不能在发布模式下使用APK

我已经创建了一个示例应用程序,有一个web视图,这个web视图打开一个网址,有一个链接来调用一个android功能。我遵循它: http://developer.android.com/guide/webapps/webview.html#BindingJavaScript

即时通讯使用最后的AndroidStudio版本和即时通讯使用银河s4迷你测试。

当我调试它,一切正常,吐司显示。

问题是,当我生成命令发布应用程序: 〜/服务器/ gradle这个-1.8 /斌/ gradle这个assembleRelease

的应用程序中打开,但是当我碰那个叫我的本地函数的链接,它不工作,没有发生。但是在调试模式下,它可以工作。

任何人都可以帮助我吗?

的图像更好地理解:

enter image description here

+0

嗨@Nachi你是最棒的!问题已经解决了。谢谢。你拯救了我的生命。上帝祝福你! –

回答

5

如果摇篮配置为使用ProGuard的,在@JavascriptInterface注释需要明确保留。

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

有关详细信息,请参阅here

2

在AndroidStudio 0.8.4上,创建一个名为proguard-rules.pro的文件,在创建一个新的App项目时在应用程序模块的根目录下。它包含以下消息(这是解决方案):

# 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.com/guide/developing/tools/proguard.html 

# Add any project specific keep options here: 

# If your project uses WebView with JS, uncomment the following 
# and specify the fully qualified class name to the JavaScript interface 
# class: 
-keepclassmembers class com.package.YourWebViewJavaInterfaceClasse { 
    public *; 
} 
相关问题