2013-07-17 44 views
0

我在我的應用程序中使用Javascript接口。Android的Javascript接口失敗

當我在我的設備(Galaxy 3)上運行調試時,它完全沒有任何問題地運行,但是當我發佈一個發佈apk文件時,JavaScript可能未運行。

這是JavaScriptInterface類:

public class StreamingMediaPlayer{ 

    public class JavaScriptInterface { 
     Context mContext; 

     JavaScriptInterface(Context c) { 
      mContext = c; 
     } 

     @JavascriptInterface 
     public void FinishExtract(String url) { 

     } 
    } 
} 

現在,我想這是因爲ProGuard的文件:

# To enable ProGuard in your project, edit project.properties 
# to define the proguard.config property as described in that file. 
# 
# Add project specific ProGuard rules here. 
# By default, the flags in this file are appended to flags specified 
# in ${sdk.dir}/tools/proguard/proguard-android.txt 
# You can edit the include path and order by changing the ProGuard 
# include property in project.properties. 
# 
# 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 fqcn.of.javascript.interface.for.webview { 
# public *; 
#} 

-dontwarn org.apache.commons.codec.binary.** 
-dontwarn com.commonsware.cwac.sacklist.** 
-dontwarn com.commonsware.cwac.tlv.** 
-dontwarn com.nineoldandroids.** 
-dontwarn com.apps.model.** 

-keep class com.millennialmedia.android.** {*;} 
-keep public class cmn.Proguard$KeepMembers 
-keep public class * implements cmn.Proguard$KeepMembers 
-keepclassmembers class * implements cmn.Proguard$KeepMembers { 
    <methods>; 
} 
-keepattributes *Annotation* 
-dontwarn android.webkit.JavascriptInterface 

-keep class com.apps.model.** { *; } 
-keepattributes *Annotation* 

難道我需要的東西添加到proguard文件?問題可以是別的嗎?

回答

1

問題是,proguard混淆了FinishExtract方法。 然後JavaScript不能找到它,因爲它假定的名字仍然是「FinishExtract」

要解決它,你可以更改您的代碼:

public class JavaScriptInterface implements cmn.Proguard.KeepMembers { 

您的ProGuard配置文件中指定的類它們實現KeepMembers應保持unobfuscated。

+0

將StreamingMediaPlayer類添加到proguard文件不是一個更好的主意嗎?也可以根據proguard文件中的註釋說明取消註釋webview的配置 – GreyBeardedGeek

+0

是將StreamingMediaPlayer.JavaScriptInterface添加到proguard配置中也可以修復它。我更喜歡這個接口的解決方案,因爲您只需要添加一次,並且可以在很多地方使用它(例如,用於多個JavaScript接口類),而無需保持類名與proguard配置同步。 – uwe