2
头图

Preface

As the soul of the X-Library series framework XPage has been open source for two years, has been committed to reducing the difficulty of using Fragments, and striving to achieve an Android development model with multiple activities and Fragments.

Some time ago, after waiting and watching for a long time, I finally updated the Android Studio (Arctic Fox) , and found that there was a warning in the code using ButterKnife annotation id in the project. The warning message is as follows:

Resource IDs will be non-final in Android Gradle Plugin version 5.0, avoid using them as annotation attributes

The warning message tells us that the Id value of Resource in the Gradle 5.0 plug-in will no longer be final, so we should avoid using Id in annotation attributes. This means that if we upgrade the Gradle plugin to version 5.0, ButterKnife will no longer be used! And in ButterKnife's official document, I also saw the information that ButterKnife was marked and abandoned :

Because XPage was originally designed to be more convenient to use Fragment, ButterKnife is integrated by default. If I want to continue to use XPage, I have to reduce the Gradle plugin to version 5.0 or less, which is abandoned in ButterKnife and replaced by Viewbinding. Under the general trend, it is obviously inappropriate.

Sure enough, my XPage open source project was quickly mentioned by users remove ButterKnife's issue , as follows:

In this way, it is imperative for XPage to remove the ButterKnife dependency, so there is this XPage 3.3.0 version upgrade.

What changes after the upgrade

This upgrade mainly consists of two parts: uses gson instead of fastjson and removes the butterknife dependency , which is in line with Google in all aspects.

Use gson instead of fastjson

Why use gson instead of fastjson? I mainly consider the following two points:

  • Fastjson has often exposed several serious security vulnerabilities before, and there are flaws in security.
  • At present, most of the Android projects use gson, and it is maintained by Google as open source, and I fully believe in Google's strength.

Remove butterknife dependency

Removing the butterknife dependency and using ViewBinding instead is the trend. So what are the benefits of using ViewBinding instead? Here is a brief list:

  • type safety : ViewBinding will generate the correct type of attributes based on the View in the layout. For example, if you put a TextView in the layout, the view binding will expose a TextView type attribute for development.
  • Null security : ViewBinding will detect whether a view exists only in some configurations, and generate attributes annotated with @Nullable based on the results. Therefore, even with layout files defined in multiple configurations, view binding can still ensure empty security.
  • Reduce the definition of control variables : ViewBinding will automatically generate a binding class, we can directly access the controls in the layout through this binding object, without having to define individual variables for each control access.

Precautions for upgrading version 3.3.0

Change of dependency

No need to rely on butterknife after version 3.3.0.
  • 3.3.0 and above, only need to rely on XPage in the project.
dependencies {
  ...
  implementation 'com.github.xuexiangjys.XPage:xpage-lib:3.3.0'
  annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:3.3.0'
}
  • Version 3.2.0 and below, in addition to relying on XPage in the project, also need to rely on butterknife.
dependencies {
  ...
  // XPage
  implementation 'com.github.xuexiangjys.XPage:xpage-lib:3.2.0'
  annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:3.2.0'
  // ButterKnife的sdk
  implementation 'com.jakewharton:butterknife:10.1.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
}

Interface changes

In order to enable XPage to better use ViewBinding, I made adjustments to XPageFragment and some interfaces of XPageActivity.
  • Deleted XPageFragment in getLayoutId abstract methods, replaced by inflateView abstract methods.
    /**
     * 加载控件
     *
     * @param inflater  inflater
     * @param container 容器
     * @return 根布局
     */
    protected abstract View inflateView(LayoutInflater inflater, ViewGroup container);
  • Deleted XPageActivity in getLayoutId abstract methods, replaced by a getCustomRootView method.
    /**
     * 获取自定义根布局
     *
     * @return 自定义根布局
     */
    protected View getCustomRootView() {
        return null;
    }

Obfuscation configuration changes

Since this XPage upgrade uses gson instead of fastjson, the obfuscated configuration needs to be modified.
  • Version 3.2.0 and above, use gson for serialization, so the configuration is as follows:
# gson
-keepattributes Signature
-keepattributes *Annotation*
-dontwarn sun.misc.**
-keep class com.google.gson.examples.android.model.** { <fields>; }
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
-keepclassmembers,allowobfuscation class * {
  @com.google.gson.annotations.SerializedName <fields>;
}
-keep,allowobfuscation,allowshrinking class com.google.gson.reflect.TypeToken
-keep,allowobfuscation,allowshrinking class * extends com.google.gson.reflect.TypeToken

# xpage
-keep class com.xuexiang.xpage.annotation.** { *; }
-keep class com.xuexiang.xpage.config.** { *; }
  • Version 3.1.1 and below use fastjson for serialization, so the configuration is as follows:
# fastjson
-dontwarn com.alibaba.fastjson.**
-keep class com.alibaba.fastjson.** { *; }
-keepattributes Signature

# xpage
-keep class com.xuexiang.xpage.annotation.** { *; }
-keep class com.xuexiang.xpage.config.** { *; }

Template engineering

For the above upgrade content, I have made corresponding updates in the latest template project. Students who want to be lazy can directly use the template project.

Related Links

at last

Thank you very much for your support to XPage who like it can go to the project's Github homepage: https://github.com/xuexiangjys/XPage Click star to support it!

I am xuexiangjys, a technical up master who loves learning, programming, and is committed to Android architecture research and open source project experience sharing. For more information, welcome to search the public : 161117b3de577a [My Android Open Source Journey]

xuexiangjys
357 声望4.1k 粉丝