feat: support single process multiple AppLifecycle instance

This commit is contained in:
2023-10-04 04:16:15 +08:00
parent 92106d6e92
commit ae71bea0a0
2 changed files with 56 additions and 34 deletions

View File

@@ -231,7 +231,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* - 在 [loadZygote] 中不会被装载 - 仅会在 [loadSystem]、[loadApp] 中装载 * - 在 [loadZygote] 中不会被装载 - 仅会在 [loadSystem]、[loadApp] 中装载
* *
* - 作为 Hook API 装载时请使用原生的 [Application] 实现生命周期监听 * - 作为 Hook API 装载时请使用原生的 [Application] 实现生命周期监听
* @param isOnFailureThrowToApp 是否在发生异常时将异常抛出给宿主 - 默认是 * @param isOnFailureThrowToApp 是否在发生异常时将异常抛出给宿主 - 默认是 (仅在第一个 Hooker 设置有效)
* @param initiate 方法体 * @param initiate 方法体
*/ */
inline fun onAppLifecycle(isOnFailureThrowToApp: Boolean = true, initiate: AppLifecycle.() -> Unit) = inline fun onAppLifecycle(isOnFailureThrowToApp: Boolean = true, initiate: AppLifecycle.() -> Unit) =
@@ -794,7 +794,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param result 回调 - ([Context] baseContext,[Boolean] 是否已执行 super) * @param result 回调 - ([Context] baseContext,[Boolean] 是否已执行 super)
*/ */
fun attachBaseContext(result: (baseContext: Context, hasCalledSuper: Boolean) -> Unit) { fun attachBaseContext(result: (baseContext: Context, hasCalledSuper: Boolean) -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.attachBaseContextCallback = result if (isCurrentScope) AppParasitics.AppLifecycleActor.get(this@PackageParam).attachBaseContextCallback = result
} }
/** /**
@@ -802,7 +802,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param initiate 方法体 * @param initiate 方法体
*/ */
fun onCreate(initiate: Application.() -> Unit) { fun onCreate(initiate: Application.() -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onCreateCallback = initiate if (isCurrentScope) AppParasitics.AppLifecycleActor.get(this@PackageParam).onCreateCallback = initiate
} }
/** /**
@@ -810,7 +810,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param initiate 方法体 * @param initiate 方法体
*/ */
fun onTerminate(initiate: Application.() -> Unit) { fun onTerminate(initiate: Application.() -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onTerminateCallback = initiate if (isCurrentScope) AppParasitics.AppLifecycleActor.get(this@PackageParam).onTerminateCallback = initiate
} }
/** /**
@@ -818,7 +818,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param initiate 方法体 * @param initiate 方法体
*/ */
fun onLowMemory(initiate: Application.() -> Unit) { fun onLowMemory(initiate: Application.() -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onLowMemoryCallback = initiate if (isCurrentScope) AppParasitics.AppLifecycleActor.get(this@PackageParam).onLowMemoryCallback = initiate
} }
/** /**
@@ -826,7 +826,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param result 回调 - ([Application] 当前实例,[Int] 类型) * @param result 回调 - ([Application] 当前实例,[Int] 类型)
*/ */
fun onTrimMemory(result: (self: Application, level: Int) -> Unit) { fun onTrimMemory(result: (self: Application, level: Int) -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onTrimMemoryCallback = result if (isCurrentScope) AppParasitics.AppLifecycleActor.get(this@PackageParam).onTrimMemoryCallback = result
} }
/** /**
@@ -834,7 +834,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param result 回调 - ([Application] 当前实例,[Configuration] 配置实例) * @param result 回调 - ([Application] 当前实例,[Configuration] 配置实例)
*/ */
fun onConfigurationChanged(result: (self: Application, config: Configuration) -> Unit) { fun onConfigurationChanged(result: (self: Application, config: Configuration) -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onConfigurationChangedCallback = result if (isCurrentScope) AppParasitics.AppLifecycleActor.get(this@PackageParam).onConfigurationChangedCallback = result
} }
/** /**
@@ -844,7 +844,7 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
*/ */
fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit) { fun registerReceiver(vararg action: String, result: (context: Context, intent: Intent) -> Unit) {
if (isCurrentScope && action.isNotEmpty()) if (isCurrentScope && action.isNotEmpty())
AppParasitics.AppLifecycleCallback.onReceiverActionsCallbacks[action.value()] = Pair(action, result) AppParasitics.AppLifecycleActor.get(this@PackageParam).onReceiverActionsCallbacks[action.value()] = action to result
} }
/** /**
@@ -853,13 +853,14 @@ open class PackageParam internal constructor(internal var wrapper: PackageParamW
* @param result 回调 - ([Context] 当前上下文,[Intent] 当前 Intent) * @param result 回调 - ([Context] 当前上下文,[Intent] 当前 Intent)
*/ */
fun registerReceiver(filter: IntentFilter, result: (context: Context, intent: Intent) -> Unit) { fun registerReceiver(filter: IntentFilter, result: (context: Context, intent: Intent) -> Unit) {
if (isCurrentScope) AppParasitics.AppLifecycleCallback.onReceiverFiltersCallbacks[filter.toString()] = Pair(filter, result) if (isCurrentScope)
AppParasitics.AppLifecycleActor.get(this@PackageParam).onReceiverFiltersCallbacks[filter.toString()] = filter to result
} }
/** 设置创建生命周期监听回调 */ /** 设置创建生命周期监听回调 */
internal fun build() { internal fun build() {
AppParasitics.AppLifecycleCallback.isOnFailureThrowToApp = isOnFailureThrowToApp if (AppParasitics.AppLifecycleActor.isOnFailureThrowToApp == null)
AppParasitics.AppLifecycleCallback.isCallbackSetUp = true AppParasitics.AppLifecycleActor.isOnFailureThrowToApp = isOnFailureThrowToApp
} }
} }

View File

@@ -112,10 +112,13 @@ internal object AppParasitics {
/** [ClassLoader] 监听回调数组 */ /** [ClassLoader] 监听回调数组 */
private var classLoaderCallbacks = mutableMapOf<Int, (Class<*>) -> Unit>() private var classLoaderCallbacks = mutableMapOf<Int, (Class<*>) -> Unit>()
/** 当前 Hook APP (宿主) 的生命周期演绎者数组 */
private val appLifecycleActors = mutableMapOf<String, AppLifecycleActor>()
/** /**
* 当前 Hook APP (宿主) 的全局生命周期 [Application] * 当前 Hook APP (宿主) 的全局生命周期 [Application]
* *
* 需要 [YukiHookAPI.Configs.isEnableDataChannel] or [AppLifecycleCallback.isCallbackSetUp] 才会生效 * 需要 [YukiHookAPI.Configs.isEnableDataChannel] or [appLifecycleActors] 不为空才会生效
*/ */
internal var hostApplication: Application? = null internal var hostApplication: Application? = null
@@ -267,36 +270,44 @@ internal object AppParasitics {
* @param throwable 当前异常 * @param throwable 当前异常
*/ */
fun YukiHookCallback.Param.throwToAppOrLogger(throwable: Throwable) { fun YukiHookCallback.Param.throwToAppOrLogger(throwable: Throwable) {
if (AppLifecycleCallback.isOnFailureThrowToApp) this.throwable = throwable if (AppLifecycleActor.isOnFailureThrowToApp != false) this.throwable = throwable
else YLog.innerE("An exception occurred during AppLifecycle event", e = throwable) else YLog.innerE("An exception occurred during AppLifecycle event", e = throwable)
} }
/** Hook [Application] 装载方法 */ /** Hook [Application] 装载方法 */
runCatching { runCatching {
if (AppLifecycleCallback.isCallbackSetUp) { if (appLifecycleActors.isNotEmpty()) {
YukiHookHelper.hook(ApplicationClass.method { name = "attach"; param(ContextClass) }, object : YukiMemberHook() { YukiHookHelper.hook(ApplicationClass.method { name = "attach"; param(ContextClass) }, object : YukiMemberHook() {
override fun beforeHookedMember(param: Param) { override fun beforeHookedMember(param: Param) {
runCatching { runCatching {
(param.args?.get(0) as? Context?)?.also { AppLifecycleCallback.attachBaseContextCallback?.invoke(it, false) } appLifecycleActors.forEach { (_, actor) ->
(param.args?.get(0) as? Context?)?.also { actor.attachBaseContextCallback?.invoke(it, false) }
}
}.onFailure { param.throwToAppOrLogger(it) } }.onFailure { param.throwToAppOrLogger(it) }
} }
override fun afterHookedMember(param: Param) { override fun afterHookedMember(param: Param) {
runCatching { runCatching {
(param.args?.get(0) as? Context?)?.also { AppLifecycleCallback.attachBaseContextCallback?.invoke(it, true) } appLifecycleActors.forEach { (_, actor) ->
(param.args?.get(0) as? Context?)?.also { actor.attachBaseContextCallback?.invoke(it, true) }
}
}.onFailure { param.throwToAppOrLogger(it) } }.onFailure { param.throwToAppOrLogger(it) }
} }
}) })
YukiHookHelper.hook(ApplicationClass.method { name = "onTerminate" }, object : YukiMemberHook() { YukiHookHelper.hook(ApplicationClass.method { name = "onTerminate" }, object : YukiMemberHook() {
override fun afterHookedMember(param: Param) { override fun afterHookedMember(param: Param) {
runCatching { runCatching {
(param.instance as? Application?)?.also { AppLifecycleCallback.onTerminateCallback?.invoke(it) } appLifecycleActors.forEach { (_, actor) ->
(param.instance as? Application?)?.also { actor.onTerminateCallback?.invoke(it) }
}
}.onFailure { param.throwToAppOrLogger(it) } }.onFailure { param.throwToAppOrLogger(it) }
} }
}) })
YukiHookHelper.hook(ApplicationClass.method { name = "onLowMemory" }, object : YukiMemberHook() { YukiHookHelper.hook(ApplicationClass.method { name = "onLowMemory" }, object : YukiMemberHook() {
override fun afterHookedMember(param: Param) { override fun afterHookedMember(param: Param) {
runCatching { runCatching {
(param.instance as? Application?)?.also { AppLifecycleCallback.onLowMemoryCallback?.invoke(it) } appLifecycleActors.forEach { (_, actor) ->
(param.instance as? Application?)?.also { actor.onLowMemoryCallback?.invoke(it) }
}
}.onFailure { param.throwToAppOrLogger(it) } }.onFailure { param.throwToAppOrLogger(it) }
} }
}) })
@@ -305,7 +316,7 @@ internal object AppParasitics {
runCatching { runCatching {
val self = param.instance as? Application? ?: return val self = param.instance as? Application? ?: return
val type = param.args?.get(0) as? Int? ?: return val type = param.args?.get(0) as? Int? ?: return
AppLifecycleCallback.onTrimMemoryCallback?.invoke(self, type) appLifecycleActors.forEach { (_, actor) -> actor.onTrimMemoryCallback?.invoke(self, type) }
}.onFailure { param.throwToAppOrLogger(it) } }.onFailure { param.throwToAppOrLogger(it) }
} }
}) })
@@ -314,12 +325,12 @@ internal object AppParasitics {
runCatching { runCatching {
val self = param.instance as? Application? ?: return val self = param.instance as? Application? ?: return
val config = param.args?.get(0) as? Configuration? ?: return val config = param.args?.get(0) as? Configuration? ?: return
AppLifecycleCallback.onConfigurationChangedCallback?.invoke(self, config) appLifecycleActors.forEach { (_, actor) -> actor.onConfigurationChangedCallback?.invoke(self, config) }
}.onFailure { param.throwToAppOrLogger(it) } }.onFailure { param.throwToAppOrLogger(it) }
} }
}) })
} }
if (YukiHookAPI.Configs.isEnableDataChannel || AppLifecycleCallback.isCallbackSetUp) if (YukiHookAPI.Configs.isEnableDataChannel || appLifecycleActors.isNotEmpty())
YukiHookHelper.hook(InstrumentationClass.method { name = "callApplicationOnCreate" }, object : YukiMemberHook() { YukiHookHelper.hook(InstrumentationClass.method { name = "callApplicationOnCreate" }, object : YukiMemberHook() {
override fun afterHookedMember(param: Param) { override fun afterHookedMember(param: Param) {
runCatching { runCatching {
@@ -346,14 +357,16 @@ internal object AppParasitics {
} }
} }
hostApplication = it hostApplication = it
AppLifecycleCallback.onCreateCallback?.invoke(it) appLifecycleActors.forEach { (_, actor) ->
AppLifecycleCallback.onReceiverActionsCallbacks.takeIf { e -> e.isNotEmpty() }?.forEach { (_, e) -> actor.onCreateCallback?.invoke(it)
if (e.first.isNotEmpty()) IntentFilter().apply { actor.onReceiverActionsCallbacks.takeIf { e -> e.isNotEmpty() }?.forEach { (_, e) ->
e.first.forEach { action -> addAction(action) } if (e.first.isNotEmpty()) IntentFilter().apply {
}.registerReceiver(e.second) e.first.forEach { action -> addAction(action) }
}.registerReceiver(e.second)
}
actor.onReceiverFiltersCallbacks.takeIf { e -> e.isNotEmpty() }
?.forEach { (_, e) -> e.first.registerReceiver(e.second) }
} }
AppLifecycleCallback.onReceiverFiltersCallbacks.takeIf { e -> e.isNotEmpty() }
?.forEach { (_, e) -> e.first.registerReceiver(e.second) }
runCatching { runCatching {
/** 过滤系统框架与一系列服务组件包名不唯一的情况 */ /** 过滤系统框架与一系列服务组件包名不唯一的情况 */
if (isDataChannelRegistered || if (isDataChannelRegistered ||
@@ -446,15 +459,23 @@ internal object AppParasitics {
} }
/** /**
* 当前 Hook APP (宿主) 的生命周期回调处理类 * 当前 Hook APP (宿主) 的生命周期演绎者
*/ */
internal object AppLifecycleCallback { internal class AppLifecycleActor {
/** 是否已设置回调 */ internal companion object {
internal var isCallbackSetUp = false
/** 是否在发生异常时将异常抛出给宿主 */ /** 是否在发生异常时将异常抛出给宿主 */
internal var isOnFailureThrowToApp = true internal var isOnFailureThrowToApp: Boolean? = null
/**
* 获取、创建新的 [AppLifecycleActor]
* @param instance 实例
* @return [AppLifecycleActor]
*/
internal fun get(instance: Any) =
appLifecycleActors[instance.toString()] ?: AppLifecycleActor().apply { appLifecycleActors[instance.toString()] = this }
}
/** [Application.attachBaseContext] 回调 */ /** [Application.attachBaseContext] 回调 */
internal var attachBaseContextCallback: ((Context, Boolean) -> Unit)? = null internal var attachBaseContextCallback: ((Context, Boolean) -> Unit)? = null