1. 程式人生 > >Android ProGuard加密webview的JavascriptInterface註解annotation的問題

Android ProGuard加密webview的JavascriptInterface註解annotation的問題

或作或輟,一曝十寒,則雖讀書百年,吾未見其可也。——(明)吳夢祥


原因:
使用 @android.webkit.JavascriptInterfaceJavascriptInterface 原因導致加密後找不到這個類的方法,主要是加密後原有的類下方法是普通的annotation,無法匯入js互動中。

// Compiled from JavascriptInterface.java (version 1.5 : 49.0, no super bit)
@java.lang.annotation.Retention(value=java.lang.annotation.RetentionPolicy.RUNTIME)
@java.lang.annotation.Target(value={java.lang.annotation.ElementType.METHOD})
public abstract @interface android.webkit.JavascriptInterface extends java.lang.annotation.Annotation {

}

加密過程的警告可以忽略:

Note: the configuration refers to the unknown class 'com.xx.WebViewActivity.JavascriptInterface'
      Maybe you meant the fully qualified name 'android.webkit.JavascriptInterface'?
Note: the configuration refers to the unknown class com.xx.WebViewActivity.JavascriptInterface'
      Maybe you meant the fully qualified name 'android.webkit.JavascriptInterface'?
Note: the configuration refers to the unknown class 'com.xx.WebViewActivity.JavascriptInterface'
      Maybe you meant the fully qualified name 'android.webkit.JavascriptInterface'?
Note: the configuration refers to the unknown class 'JavascriptInterface'
      Maybe you meant the fully qualified name 'android.webkit.JavascriptInterface'?
Note: there were 4 references to unknown classes.
      You should check your configuration for typos.
Note: there were 1 unresolved dynamic references to classes or interfaces.
      You should check if you need to specify additional program jars.
Note: there were 1 accesses to class members by means of introspection.
      You should consider explicitly keeping the mentioned class members
      (using '-keep' or '-keepclassmembers').

最後解決:


-keepclassmembers class com.xx.WebViewActivity$JavascriptInterface {
    public <fields>;
    public <methods>;
}

# keep annotated by JavascriptInterface
-keep @com.xx..WebViewActivity.JavascriptInterface class * {
    <fields>;
    <methods>;
}

-keep class * {
    @com.xx.WebViewActivity.JavascriptInterface
    <fields>;
}

-keepclassmembers class * {
    @com.xx.WebViewActivity.JavascriptInterface
    <methods>;
}

-keep class android.support.** {
    <fields>;
    <methods>;
}

-keep class com.xx.WebViewActivity.** {
    <fields>;
    <methods>;
}

-keepclassmembers classcom.xx.WebViewActivity$JavascriptInterface {
    <methods>;
}

推薦文章:


|| 版權宣告:本文為博主杜錦陽原創文章,轉載請註明出處。