### Querying and Manipulating Method Sequences with LazySequence Source: https://context7.com/libxposed/helper/llms.txt Demonstrates how to use LazySequence to filter, chain, and apply boolean logic to method matches. It includes examples of finding specific methods, handling misses with fallbacks, and traversing from classes to parameters. ```java HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { ClassMatch targetClass = builder.exactClass("com.example.Target"); MethodLazySequence publicMethods = builder.methods(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setIsPublic(true); }); MethodMatch firstPublicMethod = publicMethods.first(); MethodMatch firstVoidMethod = publicMethods.first(matcher -> { matcher.setReturnType(builder.exact(void.class)); }); MethodLazySequence staticMethods = publicMethods.all(matcher -> { matcher.setIsStatic(true); }); MethodMatch complexMatch = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setParameters(matcher.conjunction(String.class, int.class)); }); publicMethods .onMatch(methods -> { System.out.println("Found " + ((Collection) methods).size() + " public methods"); for (Method m : methods) { System.out.println(" - " + m.getName()); } }) .onMiss(() -> { System.out.println("No public methods found"); }) .substituteIfMiss(() -> { return builder.methods(m -> { m.setDeclaringClass(targetClass); m.setIsProtected(true); }); }); targetClass.getDeclaredMethods() .first(m -> m.setName(builder.exact("process"))) .getParameters() .onMatch(params -> { for (HookBuilder.Parameter p : params) { System.out.println("Param " + p.getIndex() + ": " + p.getType().getName()); } }); }); ``` -------------------------------- ### Match Constructors by Parameters and Modifiers using ConstructorMatcher Source: https://context7.com/libxposed/helper/llms.txt Illustrates how to use the ConstructorMatcher interface to locate constructors based on their declaring class, parameter types, and modifiers. It covers matching specific constructors, all constructors, and the default constructor, along with examples of hooking constructors to intercept object creation. ```java HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { ClassMatch userClass = builder.exactClass("com.example.app.model.User"); // Match constructor with specific parameters ConstructorMatch mainConstructor = builder.firstConstructor(matcher -> { matcher.setDeclaringClass(userClass); matcher.setParameterCount(3); matcher.setParameters( matcher.conjunction(String.class, String.class, int.class) ); }); // Match all constructors of a class ConstructorLazySequence allConstructors = builder.constructors(matcher -> { matcher.setDeclaringClass(userClass); }); // Match default (no-arg) constructor ConstructorMatch defaultConstructor = builder.firstConstructor(matcher -> { matcher.setDeclaringClass(userClass); matcher.setParameterCount(0); matcher.setIsPublic(true); }); // Hook constructor to intercept object creation mainConstructor.onMatch(constructor -> { xposed.hook(constructor, new XposedInterface.Hooker() { @Override public void before(XposedInterface.BeforeHookCallback callback) { String username = (String) callback.getArgs()[0]; System.out.println("Creating user: " + username); } @Override public void after(XposedInterface.AfterHookCallback callback) { Object newUser = callback.getResult(); System.out.println("User created: " + newUser); } }); }); // Iterate all constructors allConstructors.onMatch(constructors -> { for (Constructor c : constructors) { System.out.println("Constructor: " + c); } }); }); ``` -------------------------------- ### Match Methods by Name, Parameters, and Return Type using MethodMatcher Source: https://context7.com/libxposed/helper/llms.txt Demonstrates how to use the MethodMatcher interface to find specific methods based on their name, parameter types, return type, and modifiers. It shows how to use `firstMethod()` for a single match and `methods()` for multiple matches, including how to hook the matched methods and access their signature information. ```java HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { ClassMatch targetClass = builder.exactClass("com.example.app.NetworkManager"); // Match method by name and parameter types MethodMatch sendRequest = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setName(builder.exact("sendRequest")); matcher.setReturnType(builder.exactClass("java.lang.String")); matcher.setParameterCount(3); matcher.setIsPublic(true); matcher.setIsStatic(false); }); // Match methods by parameter signature MethodLazySequence callbackMethods = builder.methods(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setParameters( matcher.conjunction(String.class, int.class, Object.class) ); matcher.setReturnType(builder.exact(void.class)); }); // Match static methods with specific return type MethodMatch getInstance = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setIsStatic(true); matcher.setReturnType(targetClass); matcher.setParameterCount(0); }); // Hook the matched method sendRequest.onMatch(method -> { xposed.hook(method, new XposedInterface.Hooker() { @Override public void before(XposedInterface.BeforeHookCallback callback) { String url = (String) callback.getArgs()[0]; System.out.println("Intercepted request to: " + url); } }); }); // Access return type and parameter information sendRequest.getReturnType().onMatch(returnType -> { System.out.println("Return type: " + returnType.getName()); }); sendRequest.getParameterTypes().onMatch(paramTypes -> { for (Class type : paramTypes) { System.out.println("Param type: " + type.getName()); } }); }); ``` -------------------------------- ### Build Hooks using Kotlin DSL Source: https://context7.com/libxposed/helper/llms.txt Demonstrates the use of the helper-ktx module to define hooks in a type-safe, declarative manner. It covers class, method, field, and constructor matching, as well as binding logic and exception handling. ```kotlin import io.github.libxposed.helper.ktx.* import io.github.libxposed.api.XposedInterface class MyKotlinModule { fun onPackageLoaded(xposed: XposedInterface, classLoader: BaseDexClassLoader, apkPath: String) { xposed.buildHooks(classLoader, apkPath) { exceptionHandler = { throwable -> println("Error: ${throwable.message}") true } val targetClass = firstClass { name = "com.example.app.Target".exact isPublic = true isFinal = false } val targetMethod = firstMethod { declaringClass = targetClass name = "processData".exact returnType = String::class.java.exact parameterCounts = 2 isStatic = false } targetMethod.onMatch { method -> println("Hooking: ${method.name}") } val configField = firstField { declaringClass = targetClass name = "config".exact type = "android.content.SharedPreferences".exactClass isPrivate = true } val constructor = firstConstructor { declaringClass = targetClass parameterCounts = 1 } val methodWithParams = firstMethod { declaringClass = targetClass parameters = String::class.java[0] and Int::class.java[1] } targetClass.declaredMethods .first { isPublic = true } .onMatch { method -> println("First public method: ${method.name}") } val obfuscatedClass = firstClass { name = "a.b.".prefix superClass = "android.app.Service".exactClass } val bind = object : LazyBind() { override fun onMatch() { println("All bindings resolved") } override fun onMiss() { println("Some bindings failed") } } targetMethod.bind(bind) { method -> } configField.bind(bind) { field -> } } } } ``` -------------------------------- ### Initialize Hooks with HookBuilder Source: https://context7.com/libxposed/helper/llms.txt The HookBuilder.buildHooks method serves as the primary entry point for defining Xposed hooks. It initializes the environment with the Xposed context and class loader, allowing developers to define matchers and hook logic within a consumer function. ```java import io.github.libxposed.api.XposedInterface; import io.github.libxposed.helper.HookBuilder; import dalvik.system.BaseDexClassLoader; public class MyXposedModule { public void onPackageLoaded(XposedInterface xposed, String packageName, BaseDexClassLoader classLoader, String apkPath) { HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { builder.firstMethod(matcher -> { matcher.setDeclaringClass(builder.exactClass("com.example.TargetClass")); matcher.setName(builder.exact("targetMethod")); matcher.setParameterCount(2); }).onMatch(method -> { xposed.hook(method, new XposedInterface.Hooker() { @Override public void before(XposedInterface.BeforeHookCallback callback) { System.out.println("Method called with: " + callback.getArgs()[0]); } @Override public void after(XposedInterface.AfterHookCallback callback) { callback.setResult("Modified result"); } }); }); }).get(); } } ``` -------------------------------- ### Load Members via Reflector Signatures Source: https://context7.com/libxposed/helper/llms.txt Shows how to use the Reflector utility to load classes, methods, fields, and constructors. It supports both JVM-style descriptors and human-readable signatures for flexibility. ```java MethodMatch m1 = builder.exactMethod("com.example.Target->processData(Ljava/lang/String;I)Ljava/lang/String;"); MethodMatch m2 = builder.exactMethod("String com.example.Target.processData(String, int)"); FieldMatch f1 = builder.exactField("com.example.Config->DEBUG:Z"); FieldMatch f2 = builder.exactField("boolean com.example.Config.DEBUG"); ConstructorMatch c1 = builder.exactConstructor("com.example.User->(Ljava/lang/String;I)V"); ConstructorMatch c2 = builder.exactConstructor("com.example.User(String, int)"); ClassMatch cls1 = builder.exactClass("com.example.Target"); ClassMatch cls2 = builder.exactClass("Lcom/example/Target;"); ClassMatch cls3 = builder.exact(String[].class); ``` -------------------------------- ### Match and Access Class Fields with FieldMatcher Source: https://context7.com/libxposed/helper/llms.txt Demonstrates how to use the FieldMatcher interface to locate specific class fields by name, type, and modifiers. It also shows how to retrieve field values, modify them, and iterate over sequences of matched fields. ```java HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { ClassMatch configClass = builder.exactClass("com.example.app.Config"); // Match field by exact name FieldMatch apiKeyField = builder.firstField(matcher -> { matcher.setDeclaringClass(configClass); matcher.setName(builder.exact("API_KEY")); matcher.setType(builder.exactClass("java.lang.String")); matcher.setIsStatic(true); matcher.setIsFinal(true); }); // Match all String fields FieldLazySequence stringFields = builder.fields(matcher -> { matcher.setDeclaringClass(configClass); matcher.setType(builder.exactClass("java.lang.String")); matcher.setIsPrivate(true); }); // Match instance fields by type FieldMatch contextField = builder.firstField(matcher -> { matcher.setDeclaringClass(configClass); matcher.setType(builder.exactClass("android.content.Context")); matcher.setIsStatic(false); }); // Access matched field apiKeyField.onMatch(field -> { try { String apiKey = (String) field.get(null); // static field System.out.println("API Key: " + apiKey); // Modify the field value field.set(null, "new_api_key_value"); } catch (IllegalAccessException e) { e.printStackTrace(); } }); // Get field type information apiKeyField.getType().onMatch(fieldType -> { System.out.println("Field type: " + fieldType.getName()); }); // Process all string fields stringFields.onMatch(fields -> { for (Field f : fields) { System.out.println("String field: " + f.getName()); } }); }); ``` -------------------------------- ### Match Classes using ClassMatcher Source: https://context7.com/libxposed/helper/llms.txt The ClassMatcher interface provides flexible criteria for locating classes, including exact name matching, prefix patterns, and interface requirements. It supports both single-match and sequence-based retrieval, making it ideal for navigating obfuscated codebases. ```java HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { ClassMatch targetClass = builder.firstClass(matcher -> { matcher.setName(builder.exact("com.example.app.MainActivity")); }); ClassLazySequence activityClasses = builder.classes(matcher -> { matcher.setName(builder.prefix("com.example.app.")); matcher.setSuperClass(builder.exactClass("android.app.Activity")); matcher.setIsAbstract(false); matcher.setIsPublic(true); }); ClassMatch serviceClass = builder.firstClass(matcher -> { matcher.setContainsInterfaces( builder.exact(Runnable.class).observe() .and(builder.exact(Comparable.class).observe()) ); }); targetClass.onMatch(clazz -> { System.out.println("Found class: " + clazz.getName()); }).onMiss(() -> { System.err.println("Target class not found!"); }); targetClass.getDeclaredMethods().onMatch(methods -> { for (Method m : methods) { System.out.println("Method: " + m.getName()); } }); }); ``` -------------------------------- ### Implement Error Handling and Fallbacks - Java Source: https://context7.com/libxposed/helper/llms.txt Sets up comprehensive error handling for hook registration, including global exception handlers, miss callbacks, and fallback mechanisms. This ensures robustness when primary hook matches fail, allowing for alternative strategies. ```java HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { // Set global exception handler builder.setExceptionHandler(throwable -> { System.err.println("Hook error: " + throwable.getMessage()); throwable.printStackTrace(); return true; // Continue processing other matches }); // Define match with fallback alternatives MethodMatch targetMethod = builder.firstMethod(matcher -> { matcher.setDeclaringClass(builder.exactClass("com.example.NewClass")); matcher.setName(builder.exact("newMethodName")); }).substituteIfMiss(() -> { // Fallback to legacy method if new one not found return builder.firstMethod(matcher -> { matcher.setDeclaringClass(builder.exactClass("com.example.OldClass")); matcher.setName(builder.exact("legacyMethodName")); }); }).matchFirstIfMiss(matcher -> { // Last resort: match any method with similar signature matcher.setDeclaringClass(builder.firstClass(cm -> { cm.setName(builder.prefix("com.example.")); })); matcher.setReturnType(builder.exact(String.class)); matcher.setParameterCount(1); }); // Handle match and miss separately targetMethod .onMatch(method -> { System.out.println("Successfully matched: " + method); // Apply hook }) .onMiss(() -> { System.err.println("Failed to find target method after all fallbacks"); }); // Class match with fallback ClassMatch targetClass = builder.firstClass(matcher -> { matcher.setName(builder.exact("com.example.TargetV2")); }).substituteIfMiss(() -> { return builder.exactClass("com.example.TargetV1"); }); }); ``` -------------------------------- ### Perform Advanced DEX Bytecode Analysis Source: https://context7.com/libxposed/helper/llms.txt Utilizes DEX analysis mode to identify methods based on internal bytecode characteristics. This includes matching based on referenced strings, method invocations, field access/assignments, and specific opcode sequences. ```java @HookBuilder.DexAnalysis HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { builder.setForceDexAnalysis(true); ClassMatch targetClass = builder.firstClass(matcher -> { matcher.setName(builder.prefix("com.example.")); }); // Match method by referenced strings in bytecode MethodMatch encryptMethod = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setReferredStrings( builder.exact("AES/CBC/PKCS5Padding").observe() .or(builder.exact("RSA/ECB/PKCS1Padding").observe()) ); }); // Match method by invoked methods MethodMatch networkMethod = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setInvokedMethods( builder.exactMethod("java.net.URL->openConnection()Ljava/net/URLConnection;").observe() ); }); // Match method by accessed fields MethodMatch configReader = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setAccessedFields( builder.exactField("com.example.Config->DEBUG:Z").observe() ); }); // Match method by assigned fields MethodMatch setter = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setAssignedFields( builder.firstField(fm -> { fm.setDeclaringClass(targetClass); fm.setName(builder.prefix("m")); }).observe() ); }); // Match by opcode sequence byte[] targetOpcodes = new byte[] { 0x1a, 0x6e, 0x0c }; // const-string, invoke-virtual, move-result MethodMatch specificMethod = builder.firstMethod(matcher -> { matcher.setDeclaringClass(targetClass); matcher.setContainsOpcodes(targetOpcodes); }); encryptMethod.onMatch(method -> { System.out.println("Found encryption method: " + method.getName()); }); }); ``` -------------------------------- ### Enable Caching for Reflection Results - Java Source: https://context7.com/libxposed/helper/llms.txt Configures caching for reflection results to improve performance by avoiding repeated lookups. It involves setting input and output streams for the cache and providing a checker for cache validity. This is useful for speeding up module initialization. ```java import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.File; HookBuilder.buildHooks(xposed, classLoader, apkPath, builder -> { File cacheFile = new File(context.getCacheDir(), "hook_cache.dat"); // Configure cache input (load previous results) if (cacheFile.exists()) { try { builder.setCacheInputStream(new FileInputStream(cacheFile)); } catch (Exception e) { e.printStackTrace(); } } // Configure cache output (save new results) try { builder.setCacheOutputStream(new FileOutputStream(cacheFile)); } catch (Exception e) { e.printStackTrace(); } // Set cache validity checker builder.setCacheChecker(cacheInfo -> { Long lastModified = (Long) cacheInfo.get("lastModifyTime"); if (lastModified == null) return false; return lastModified == new File(apkPath).lastModified(); }); // Define keyed matches for cache storage builder.firstMethod(matcher -> { matcher.setKey("target_method"); // Key for cache lookup matcher.setDeclaringClass(builder.exactClass("com.example.Target")); matcher.setName(builder.exact("doSomething")); }).onMatch(method -> { System.out.println("Matched (possibly from cache): " + method); }); // Keys on class matches builder.firstClass(matcher -> { matcher.setKey("main_activity"); matcher.setName(builder.exact("com.example.MainActivity")); }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.