From 9585d4b91f4e9b37605c84332a9354c2d4733e0e Mon Sep 17 00:00:00 2001 From: fankesyooni Date: Fri, 16 Sep 2022 02:39:36 +0800 Subject: [PATCH] Modify change appContext, appResources return object with null-safe type in PackageParam --- docs/api/public/PackageParam.md | 12 ++++++++++-- .../yukihookapi/hook/param/PackageParam.kt | 10 ++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/docs/api/public/PackageParam.md b/docs/api/public/PackageParam.md index 326e402f..4eb40a8c 100644 --- a/docs/api/public/PackageParam.md +++ b/docs/api/public/PackageParam.md @@ -59,13 +59,17 @@ val appUserId: Int ### appContext *- field* ```kotlin -val appContext: Application +val appContext: Application? ``` **变更记录** `v1.0.72` `新增` +`v1.0.93` `修改` + +加入可空类型 (空安全) + **功能描述** > 获取当前 Hook APP 的 `Application`。 @@ -75,13 +79,17 @@ val appContext: Application ### appResources *- field* ```kotlin -val appResources:Resources +val appResources:Resources? ``` **变更记录** `v1.0.80` `新增` +`v1.0.93` `修改` + +加入可空类型 (空安全) + **功能描述** > 获取当前 Hook APP 的 Resources。 diff --git a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt index 8294f301..7953e01f 100644 --- a/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt +++ b/yukihookapi/src/api/kotlin/com/highcapable/yukihookapi/hook/param/PackageParam.kt @@ -88,19 +88,17 @@ open class PackageParam internal constructor(@PublishedApi internal var wrapper: * 获取当前 Hook APP 的 [Application] 实例 * * - ❗首次装载可能是空的 - 请延迟一段时间再获取或通过设置 [onAppLifecycle] 监听来完成 - * @return [Application] - * @throws IllegalStateException 如果 [Application] 是空的 + * @return [Application] or null */ - val appContext get() = AppParasitics.hostApplication ?: YukiHookAppHelper.currentApplication() ?: error("PackageParam got null appContext") + val appContext get() = AppParasitics.hostApplication ?: YukiHookAppHelper.currentApplication() /** * 获取当前 Hook APP 的 Resources * * - ❗你只能在 [HookResources.hook] 方法体内或 [appContext] 装载完毕时进行调用 - * @return [Resources] - * @throws IllegalStateException 如果当前处于 [loadZygote] 或 [appContext] 尚未加载 + * @return [Resources] or null */ - val appResources get() = wrapper?.appResources ?: appContext.resources ?: error("You cannot call to appResources in this time") + val appResources get() = wrapper?.appResources ?: appContext?.resources /** * 获取当前系统框架的 [Context]