# Use as Xposed Module Configs

> Here are the related configuration methods used by `YukiHookAPI` as an Xposed Module.

## Dependency Configs

> As an Xposed Module, `YukiHookAPI` provides an automatic builder.

You need to integrate the latest version of the `com.highcapable.yukihookapi:ksp-xposed` dependency in your `build.gradle`.

## Custom Automatic Builder

> You can configure how `YukiHookAPI` will generate the `xposed_init` entry point.

### InjectYukiHookWithXposed Annotation

```kotlin:no-line-numbers
annotation class InjectYukiHookWithXposed(
    val sourcePath: String,
    val modulePackageName: String,
    val entryClassName: String,
    val isUsingResourcesHook: Boolean
)
```

The `@InjectYukiHookWithXposed` annotation is an important annotation to mark the entry point of a Module App's Hook.

::: danger

The **Class** of the **@InjectYukiHookWithXposed** annotation must implements **IYukiHookXposedInit** interface.

All **Class** tags in your current project can **only exist once**, if there are multiple declaration automatic builder <u>**will throw an exception at compile time**</u>, you can customize its related parameters.

:::

#### sourcePath Parameter

The `sourcePath` parameter determines the important identifier for the automatic builder to automatically find and match your current project path.

The content of this parameter is a relative path match, and the default parameter is `src/main`.

::: danger

If your project is not in **../src/main..** or you set the project path manually using **sourceSets**, you need to set the **sourcePath** parameter manually, otherwise the automatic builder will not recognize your project path and <u>**will throw an exception at compile time**</u>.

:::

> The following example

```kotlin
@InjectYukiHookWithXposed(sourcePath = "src/custom")
```

The file path separator used by `sourcePath` will be automatically recognized according to `Windows` and `Unix`, either `/` or `\` can be used.

#### modulePackageName Parameter

`modulePackageName` is the `applicationId` of your current project, which is your module package name (the final generated application package name).

If left blank or not filled, the automatic builder will analyze and generate the current project file.

::: warning

If you want to use the module package name to be automatically generated, you need to ensure that your project namespace has any of the following definitions in **AndroidManifest.xml**, **build.gradle** or **build.gradle.kts**.

:::

Example namespace `com.example.demo`, any one of the following definitions.

The following definitions are for reference only, usually **as long as your project can generate the `BuildConfig.java` file normally, no additional operations are required**.

> `AndroidManifest.xml` example

```xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.demo">
```

> `build.gradle` example

```groovy
android {
    namespace 'com.example.demo'
}
```

> `build.gradle.kts` example

```kotlin
android {
    namespace = "com.example.demo"
}
```

If your module package name is automatically generated by unconventional means, or you think it is necessary to manually define the module package name, then you can directly set the `modulePackageName` parameter.

> The following example

```kotlin
@InjectYukiHookWithXposed(modulePackageName = "com.example.demo")
```

As long as you customize the `modulePackageName` parameter, you will get a warning at compile time.

> The following example

```:no-line-numbers
You set the customize module package name to "com.example.demo", please check for yourself if it is correct
```

::: warning

In addition to the format of the manually defined module package name, the automatic builder will no longer check whether the module package name is correct, and you need to confirm its validity by yourself.

:::

#### entryClassName Parameter

`entryClassName` determines how the automatic builder generates the entry class name in `xposed_init`.

By default, it will use your entry class package name to insert the `_YukiHookXposedInit` suffix for generation.

Suppose this is your entry class.

> The following example

```kotlin
@InjectYukiHookWithXposed
object HookEntry : IYukiHookXposedInit
```

The Xposed entry class is handled as follows.

> The following example

```kotlin:no-line-numbers
class HookEntry_YukiHookXposedInit : IXposedHookZygoteInit, IXposedHookLoadPackage, ...
```

The compiled class name structure is as follows.

> The following example

```:no-line-numbers
...hook.HookEntry ← Your entry class
...hook.HookEntry_Impl ← Auto-generated Impl class
...hook.HookEntry_YukiHookXposedInit ← Automatically generated Xposed entry class
```

We now define the entry class name as `HookXposedEntry`.

> The following example

```kotlin
@InjectYukiHookWithXposed(entryClassName = "HookXposedEntry")
object HookEntry : IYukiHookXposedInit
```

The Xposed entry class is handled as follows.

> The following example

```kotlin:no-line-numbers
class HookXposedEntry : IXposedHookZygoteInit, IXposedHookLoadPackage, ...
```

The compiled class name structure is as follows.

> The following example

```:no-line-numbers
...hook.HookEntry ← Your entry class
...hook.HookEntry_Impl ← Auto-generated Impl class
...hook.HookXposedEntry ← Automatically generated Xposed entry class
```

::: tip

The entry class can be defined using **class** or **object**, but it is recommended to use **object** definition to ensure that each injected process is a single instance.

:::

::: danger

The **entryClassName** you define must not be the same as the class name in **xposed_init**, otherwise the automatic builder <u>**throws an exception at compile time**</u>.

:::

#### isUsingResourcesHook Parameter

`isUsingResourcesHook` determines whether the automatic builder generates relevant code for the Resources Hook, this feature is enabled by default.

The generated entry class after enabling it will look like the following.

> The following example

```kotlin:no-line-numbers
class _YukiHookXposedInit : IXposedHookZygoteInit, IXposedHookLoadPackage, IXposedHookInitPackageResources {

    override fun initZygote(sparam: IXposedHookZygoteInit.StartupParam?) {
        // ...
    }

    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam?) {
        // ...
    }

    override fun handleInitPackageResources(resparam: XC_InitPackageResources.InitPackageResourcesParam?) {
        // ...
    }
}
```

If your current project does not need to use Resources Hook, you can set `isUsingResourcesHook = false` to disable automatic generation.

> The following example

```kotlin
@InjectYukiHookWithXposed(isUsingResourcesHook = false)
```

The resulting entry class after closing will look like the following.

> The following example

```kotlin:no-line-numbers
class _YukiHookXposedInit : IXposedHookZygoteInit, IXposedHookLoadPackage {

    override fun initZygote(sparam: IXposedHookZygoteInit.StartupParam?) {
        // ...
    }

    override fun handleLoadPackage(lpparam: XC_LoadPackage.LoadPackageParam?) {
        // ...
    }
}
```

::: tip

Since the Xposed entry class is dynamically generated by **YukiHookAPI**, it will generate the following two files at the same time.

- **assets/xposed_init**

- **resources/META-INF/yukihookapi_init**
  
If you are using **Git** code control system, you can add these two files to **.gitignore** file.

:::

### IYukiHookXposedInit Interface

The `IYukiHookXposedInit` interface that your Hook entry class must implements it, which is the entry point for your Module App to start hooking.

::: tip

For more functions, please refer to [IYukiHookXposedInit](../api/public/com/highcapable/yukihookapi/hook/xposed/proxy/IYukiHookXposedInit).

:::

When your Module App is loaded by Xposed, the `onHook` method will be called back, you need to start using `YukiHookAPI` in this method.

> The basic calling process is `_YukiHookXposedInit` → `IYukiHookXposedInit.onXposedEvent` → `IYukiHookXposedInit.onInit` → `IYukiHookXposedInit.onHook`

For details, please refer to [API Basic Configs](../config/api-example).

## Native Xposed API Events

If your current Xposed Module uses third-party resources, but may not be able to transfer them in a short time, you can use `onXposedEvent` to monitor all loading events of the native Xposed API.

> The following example

```kotlin
@InjectYukiHookWithXposed
object HookEntry : IYukiHookXposedInit {

    override fun onHook() {
        // Your code here.
    }

    override fun onXposedEvent() {
        // Listen to the loading events of the native Xposed API
        YukiXposedEvent.events {
            onInitZygote {
                // The it object is [StartupParam]
            }
            onHandleLoadPackage {
                // The it object is [LoadPackageParam]
            }
            onHandleInitPackageResources {
                // The it object is [InitPackageResourcesParam]
            }
        }
    }
}
```

`onXposedEvent` and `onHook` methods exist completely independently and do not affect each other. You can continue to use `YukiHookAPI` in the `onHook` method.

::: tip

For more functions, please refer to the [IYukiHookXposedInit.onXposedEvent](../api/public/com/highcapable/yukihookapi/hook/xposed/proxy/IYukiHookXposedInit#onxposedevent-method) method.

:::