This commit is contained in:
2022-02-05 03:53:38 +08:00
parent 5c3335fab3
commit e6bfc181de
44 changed files with 349 additions and 216 deletions

View File

@@ -1,41 +1,19 @@
plugins {
id 'com.android.library'
id 'org.jetbrains.kotlin.android'
id 'java-library'
id 'org.jetbrains.kotlin.jvm'
}
android {
compileSdk 31
defaultConfig {
minSdk 22
targetSdk 31
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles "consumer-rules.pro"
}
sourceSets { main { java.srcDirs("src/main/kotlin") } }
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
sourceSets.main { java.srcDir("src/api/kotlin") }
}
kotlin { sourceSets.main { kotlin.srcDir("src/api/kotlin") } }
dependencies {
// Used 82 API Version
compileOnly 'de.robv.android.xposed:api:82'
implementation 'androidx.appcompat:appcompat:1.4.1'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
compileOnly fileTree(include: ['android-stub.jar'], dir: 'libs')
implementation 'androidx.annotation:annotation:1.3.0'
}

Binary file not shown.

View File

@@ -1,21 +0,0 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -30,11 +30,14 @@
package com.highcapable.yukihookapi
import android.content.pm.ApplicationInfo
import androidx.annotation.Keep
import com.highcapable.yukihookapi.YukiHookAPI.encase
import com.highcapable.yukihookapi.annotation.DoNotUseField
import com.highcapable.yukihookapi.annotation.DoNotUseMethod
import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker
import com.highcapable.yukihookapi.param.CustomParam
import com.highcapable.yukihookapi.param.PackageParam
import com.highcapable.yukihookapi.hook.param.CustomParam
import com.highcapable.yukihookapi.hook.param.PackageParam
import de.robv.android.xposed.callbacks.XC_LoadPackage
/**
* YukiHook 的装载 API 调用类
@@ -43,22 +46,36 @@ import com.highcapable.yukihookapi.param.PackageParam
*
* 模块装载方式已经自动对接 Xposed API - 可直接调用 [encase] 完成操作
*/
@Keep
object YukiHookAPI {
/** 全局标识 */
const val TAG = "YukiHookAPI"
/** Xposed Hook API 绑定的模块包名 */
/**
* Xposed Hook API 绑定的模块包名 - 未写将自动生成
* - 你不应该设置此变量的名称 - 请使用 [encase] 装载模块包名
*/
@DoNotUseField
internal var modulePackageName = ""
@Keep
var modulePackageName = ""
/** Xposed Hook API 方法体回调 */
@DoNotUseField
internal var packageParamCallback: (PackageParam.() -> Unit)? = null
private var packageParamCallback: (PackageParam.() -> Unit)? = null
/**
* 装载 Xposed API 回调
*
* - 装载代码将自动生成 - 你不应该手动使用此方法装载 Xposed 模块事件
* @param lpparam Xposed [XC_LoadPackage.LoadPackageParam]
*/
@DoNotUseMethod
@Keep
fun onXposedLoaded(lpparam: XC_LoadPackage.LoadPackageParam) = packageParamCallback?.invoke(PackageParam(lpparam))
/**
* 作为模块装载调用入口方法 - Xposed API
* @param moduleName 模块包名 - 填入当前的 BuildConfig.APPLICATION_ID
* @param moduleName 你的模块包名 - 可通过 Xposed API 自动生成
* @param initiate Hook 方法体
*/
fun encase(moduleName: String = "", initiate: PackageParam.() -> Unit) {
@@ -68,7 +85,7 @@ object YukiHookAPI {
/**
* 作为模块装载调用入口方法 - Xposed API
* @param moduleName 模块包名 - 填入当前的 BuildConfig.APPLICATION_ID
* @param moduleName 你的模块包名 - 可通过 Xposed API 自动生成
* @param hooker Hook 子类数组 - 必填不能为空
* @throws IllegalStateException 如果 [hooker] 是空的
*/

View File

@@ -45,7 +45,7 @@ package com.highcapable.yukihookapi.annotation
)
@Retention(AnnotationRetention.BINARY)
/**
* 警告方法外部调用声明
* - 警告方法外部调用声明
* 此方法除继承和接口外不应该在这里被调用
* 如果调用此方法可能会出现错误或 APP 发生异常
*/

View File

@@ -45,7 +45,7 @@ package com.highcapable.yukihookapi.annotation
)
@Retention(AnnotationRetention.BINARY)
/**
* 警告方法外部调用声明
* - 警告方法外部调用声明
* 此方法除继承和接口外不应该在这里被调用
* 如果调用此方法可能会出现错误或 APP 发生异常
*/

View File

@@ -0,0 +1,65 @@
/**
* MIT License
*
* Copyright (C) 2022 HighCapable
*
* This file is part of YukiHookAPI.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is Created by fankes on 2022/2/3.
*/
@file:Suppress("unused")
package com.highcapable.yukihookapi.annotation.xposed
import androidx.annotation.Keep
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy
/**
* 标识 YukiHook 注入 Xposed 入口的类注释
*
* - 你的项目 source 目录必须为 ../src/main/ 后方内容根据你自行喜好定义即可 - 否则编译会报错
*
* - 你的 Hook 入口类(HookEntryClass) 需要按照此格式创建 --> 你的模块APP包名/hook/...可允许子包名存在.../你的入口类
*
* 例子com.example.module.hook.MainHook、com.example.module.hook.inject.MainInject、com.example.module.hook.custom.CustomClass
*
* 你的 xposed_init 入口将被自动生成为 --> 你的模块APP包名/hook/...可允许子包名存在.../你的入口类_YukiHookXposedInit
*
* 例子com.example.module.hook.MainHook_YukiHookXposedInit
*
* - 你的模块包名将被这样识别:|com.example.module|.hook...
*
* - 若你不喜欢这样创建类 - 没问题 - 请在 [YukiHookAPI.encase] 中自行指定模块包名即可 - 但不按照规则定义包名你将会收到编译警告
*
* 例子YukiHookAPI.encase(moduleName = "com.example.module", ...)
*
* - 最后这一点很重要:请不要随意修改项目 ../src/main/assets/xposed_init 中的内容 - 否则可能会导致模块装载发生错误
*
* 你必须将被注释的类继承于 [YukiHookXposedInitProxy] 接口实现 [YukiHookXposedInitProxy.onHook] 方法 - 否则编译会报错
*
* 只能拥有一个 Hook 入口 - 若存在多个注释编译会报错
*
* 详情请参考 [YukiHookAPI Wiki](https://github.com/fankes/YukiHookAPI/wiki)
*/
@Target(AnnotationTarget.CLASS)
@Keep
annotation class InjectYukiHookWithXposed

View File

@@ -34,8 +34,8 @@ import com.highcapable.yukihookapi.hook.core.finder.ConstructorFinder
import com.highcapable.yukihookapi.hook.core.finder.FieldFinder
import com.highcapable.yukihookapi.hook.core.finder.MethodFinder
import com.highcapable.yukihookapi.hook.log.loggerE
import com.highcapable.yukihookapi.param.HookParam
import com.highcapable.yukihookapi.param.PackageParam
import com.highcapable.yukihookapi.hook.param.HookParam
import com.highcapable.yukihookapi.hook.param.PackageParam
import de.robv.android.xposed.XC_MethodHook
import de.robv.android.xposed.XC_MethodReplacement
import de.robv.android.xposed.XposedBridge

View File

@@ -29,8 +29,8 @@ package com.highcapable.yukihookapi.hook.entity
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.annotation.DoNotUseMethod
import com.highcapable.yukihookapi.hook.param.PackageParam
import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy
import com.highcapable.yukihookapi.param.PackageParam
/**
* YukiHook 的子类实现

View File

@@ -35,12 +35,12 @@ import android.net.Uri
import android.os.Bundle
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.hook.entity.YukiBaseHooker
import com.highcapable.yukihookapi.hook.param.PackageParam
import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy
import com.highcapable.yukihookapi.param.PackageParam
/**
* [YukiHookXposedInitProxy] 中装载 [YukiHookAPI]
* @param moduleName 模块包名 - 填入当前的 BuildConfig.APPLICATION_ID
* @param moduleName 你的模块包名 - 可通过 Xposed API 自动生成
* @param initiate Hook 方法体
*/
fun YukiHookXposedInitProxy.encase(moduleName: String = "", initiate: PackageParam.() -> Unit) =
@@ -48,11 +48,18 @@ fun YukiHookXposedInitProxy.encase(moduleName: String = "", initiate: PackagePar
/**
* [YukiHookXposedInitProxy] 中装载 [YukiHookAPI]
* @param moduleName 模块包名 - 填入当前的 BuildConfig.APPLICATION_ID
* @param hooker Hook 子类数组 - 必填不能为空
* @throws IllegalStateException 如果 [hooker] 是空的
*/
fun YukiHookXposedInitProxy.encase(moduleName: String = "", vararg hooker: YukiBaseHooker) =
fun YukiHookXposedInitProxy.encase(vararg hooker: YukiBaseHooker) = YukiHookAPI.encase(hooker = hooker)
/**
* [YukiHookXposedInitProxy] 中装载 [YukiHookAPI]
* @param moduleName 你的模块包名 - 可通过 Xposed API 自动生成
* @param hooker Hook 子类数组 - 必填不能为空
* @throws IllegalStateException 如果 [hooker] 是空的
*/
fun YukiHookXposedInitProxy.encase(moduleName: String, vararg hooker: YukiBaseHooker) =
YukiHookAPI.encase(moduleName, *hooker)
/**

View File

@@ -25,7 +25,7 @@
*
* This file is Created by fankes on 2022/2/2.
*/
package com.highcapable.yukihookapi.param
package com.highcapable.yukihookapi.hook.param
import android.content.pm.ApplicationInfo

View File

@@ -27,7 +27,7 @@
*/
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
package com.highcapable.yukihookapi.param
package com.highcapable.yukihookapi.hook.param
import de.robv.android.xposed.XC_MethodHook
import java.lang.reflect.Constructor

View File

@@ -27,7 +27,7 @@
*/
@file:Suppress("unused", "MemberVisibilityCanBePrivate", "EXPERIMENTAL_API_USAGE")
package com.highcapable.yukihookapi.param
package com.highcapable.yukihookapi.hook.param
import android.content.pm.ApplicationInfo
import com.highcapable.yukihookapi.annotation.DoNotUseMethod
@@ -40,7 +40,7 @@ import de.robv.android.xposed.callbacks.XC_LoadPackage
*
* 如果你想将 YukiHook 作为 Hook API 使用 - 你可自定义 [customParam]
*
* 特别注意如果 [baseParam] [customParam] 都为空将发生问题
* - 特别注意如果 [baseParam] [customParam] 都为空将发生问题
* @param baseParam 对接 Xposed API [XC_LoadPackage.LoadPackageParam] - 默认空
* @param customParam 自定义装载类 - 默认空
*/
@@ -116,7 +116,7 @@ open class PackageParam(
/**
* 将目标 [Class] 绑定到 [appClassLoader]
*
* 请注意未绑定到 [appClassLoader] [Class] 不能被装载 - 调用 [hook] 方法会自动绑定
* - 请注意未绑定到 [appClassLoader] [Class] 不能被装载 - 调用 [hook] 方法会自动绑定
* @return [Class]
* @throws NoClassDefFoundError 如果找不到类会报错
*/

View File

@@ -1,2 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.highcapable.yukihookapi" />

View File

@@ -1 +0,0 @@
com.highcapable.yukihookapi.hook.xposed.YukiHookLoadPackage

View File

@@ -1,45 +0,0 @@
/**
* MIT License
*
* Copyright (C) 2022 HighCapable
*
* This file is part of YukiHookAPI.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is Created by fankes on 2022/2/3.
*/
package com.highcapable.yukihookapi.annotation.xposed
import androidx.annotation.Keep
import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy
import com.highcapable.yukihookapi.hook.xposed.YukiHookLoadPackage
/**
* 标识注入 YukiHook 的类
*
* 此类将使用 [YukiHookLoadPackage] 自动调用 XposedInit
*
* 你可以将被注释的类继承于 [YukiHookXposedInitProxy] 接口实现 [YukiHookXposedInitProxy.onHook] 方法
*
* 只能拥有一个 Hook 入口 - 多个入口将以首个得到的入口为准
*/
@Target(AnnotationTarget.CLASS)
@Keep
annotation class InjectYukiHookWithXposed

View File

@@ -1,90 +0,0 @@
/**
* MIT License
*
* Copyright (C) 2022 HighCapable
*
* This file is part of YukiHookAPI.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* This file is Created by fankes on 2022/2/2.
*/
@file:Suppress("EXPERIMENTAL_API_USAGE")
package com.highcapable.yukihookapi.hook.xposed
import androidx.annotation.Keep
import com.highcapable.yukihookapi.YukiHookAPI
import com.highcapable.yukihookapi.annotation.xposed.InjectYukiHookWithXposed
import com.highcapable.yukihookapi.hook.factory.clazz
import com.highcapable.yukihookapi.hook.factory.findConstructor
import com.highcapable.yukihookapi.hook.factory.findMethod
import com.highcapable.yukihookapi.hook.log.loggerE
import com.highcapable.yukihookapi.hook.proxy.YukiHookXposedInitProxy
import com.highcapable.yukihookapi.hook.type.BooleanType
import com.highcapable.yukihookapi.param.PackageParam
import de.robv.android.xposed.IXposedHookLoadPackage
import de.robv.android.xposed.callbacks.XC_LoadPackage
/**
* 接管 Xposed 的 [IXposedHookLoadPackage] 入口
*
* 你可以使用 [YukiHookAPI.encase] 或在 [YukiHookXposedInitProxy] 中监听模块开始装载
*
* 需要标识 Hook 入口的类 - 请声明注释 [InjectYukiHookWithXposed]
*/
@Keep
class YukiHookLoadPackage : IXposedHookLoadPackage {
override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam?) {
if (lpparam == null) return
runCatching {
/** 执行入口方法 */
hookEntryClassName().clazz.apply {
findMethod(name = "onHook")?.invoke(findConstructor()?.newInstance())
}
}.onFailure {
loggerE(msg = "Try to load ${hookEntryClassName()} Failed", e = it)
}
/** 装载 APP Hook 实体类 */
PackageParam(lpparam).apply {
/** Hook 模块激活状态 */
loadApp(name = YukiHookAPI.modulePackageName) {
YukiHookModuleStatus::class.java.hook {
injectMember {
method {
name = "isActive"
returnType = BooleanType
}
replaceToTrue()
}.onAllFailure { loggerE(msg = "Try to Hook ModuleStatus Failed", e = it) }
}
}
/** 设置装载回调 */
YukiHookAPI.packageParamCallback?.invoke(this)
}
}
/**
* 获得目标装载类名 - 通过 APT 自动设置 TODO 待实现
* @return [String] 目标装载类名
*/
@Keep
private fun hookEntryClassName() = "com.highcapable.yukihookapi.demo.hook.inject.MainInjecter"
}