集成指南 - xuexiangjys/XPage GitHub Wiki

除了文档之外,我们还提供了XPage视频教程 供大家学习参考。

添加Gradle依赖

1.在项目根目录的build.gradle的 repositories 添加jitpack仓库

allprojects {
     repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

2.在dependencies添加引用

在应用项目(一般是app)的 build.gradle 的 dependencies 添加:

以下是版本说明,选择一个即可。

  • androidx版本:3.0.0及以上
dependencies {
  ...
  // XPage
  implementation 'com.github.xuexiangjys.XPage:xpage-lib:3.4.0'
  annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:3.4.0'
}

【版本注意】3.3.0及以上版本去除了ButterKnife的依赖。

【升级注意】从 3.3.0以下 升级到 3.4.0及以上:

    @Deprecated
    protected abstract View inflateView(LayoutInflater inflater, ViewGroup container);

------> 替换为

    protected abstract View onCreateContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, boolean attachToRoot);
  • support版本:2.3.0及以下
dependencies {
  ...
  // XPage
  implementation 'com.github.xuexiangjys.XPage:xpage-lib:2.3.0'
  annotationProcessor 'com.github.xuexiangjys.XPage:xpage-compiler:2.3.0'
  // ButterKnife的sdk
  implementation 'com.jakewharton:butterknife:8.4.0'
  annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

【注意】如果你使用的是kotlin,请使用如下配置:

apply plugin: 'kotlin-kapt'

dependencies {
  ...
  //XPage
  implementation 'com.github.xuexiangjys.XPage:xpage-lib:3.4.0'
  kapt 'com.github.xuexiangjys.XPage:xpage-compiler:3.4.0'
}

【注意】:如果你使用的module不止一个的话,每个module下都需要增加XPage的依赖。

3.进行moduleName注册(非必要)

defaultConfig {
    ...

    javaCompileOptions {
        annotationProcessorOptions {
            arguments = [ moduleName : project.getName() ]
        }
    }
}

【注意】:如果不注册的话,默认ModuleName为app


页面注册

方法一:Application中动态注册【推荐】

1.自动进行页面注册【推荐】

使用apt编译时自动生成的页面注册配置类 "moduleName"+PageConfig 的getPages()进行注册。

PageConfig.getInstance()
        .debug("PageLog")       //开启调试
        .setContainActivityClazz(XPageActivity.class) //设置默认的容器Activity
        .init(this);            //初始化页面配置

【注意】:如果你的项目中只是增加了依赖,还没有使用@Page注解XPageFragment页面的话,在编译时是不会自动生成注册页面的!!

2.手动动态进行页面注册

PageConfig.getInstance()
        .setPageConfiguration(new PageConfiguration() { //页面注册
            @Override
            public List<PageInfo> registerPages(Context context) {
                List<PageInfo> pageInfos = new ArrayList<>();
                addPageInfoAndSubPages(pageInfos, MainFragment.class);
                pageInfos.add(PageConfig.getPageInfo(DateReceiveFragment.class));
                return pageInfos;        //手动注册页面
            }
        })
        .debug("PageLog")       //开启调试
        .init(this);            //初始化页面配置

方法二:assets中静态注册

在assets文件夹中新建“corepage.json“,然后进行如下配置:

[
  {
    "name": "测试页面1",
    "classPath": "com.xuexiang.xpagedemo.fragment.TestFragment1",
    "params": ""
  },
  {
    "name": "测试页面2",
    "classPath": "com.xuexiang.xpagedemo.fragment.TestFragment2",
    "params": {
      "key1":"这是参数1的值",
      "key2":"这是参数2的值"
    }
  },
]

混淆配置

  • 3.2.0及以上版本,使用的是gson进行序列化的,所以配置如下:
# 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.** { *; }
  • 3.1.1及以下版本,使用的是fastjson进行序列化的,所以配置如下:
# 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.** { *; }
⚠️ **GitHub.com Fallback** ⚠️