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>;
}

推荐文章:


|| 版权声明:本文为博主杜锦阳原创文章,转载请注明出处。

©️2020 CSDN 皮肤主题: 点我我会动 设计师:上身试试 返回首页