Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to create a new method using dexlib2 #875

Open
localacct opened this issue Aug 29, 2023 · 2 comments
Open

How to create a new method using dexlib2 #875

localacct opened this issue Aug 29, 2023 · 2 comments

Comments

@localacct
Copy link

Hi

Sorry if I am asking this question in the wrong place. I know we can use Rewriter to modify an existing method. How can we create a new method to add to the dex file? Can anyone give a simple example?

@auermich93
Copy link

Something like that should work. You create a new method implementation and fill it up with instructions. Look up the API for the concrete parameter descriptions.

            int paramIndex = 1;
            MutableMethodImplementation implementation = new MutableMethodImplementation(2 + paramCount);

            implementation.addInstruction(new BuilderInstruction35c(Opcode.INVOKE_SUPER, 1 + paramCount,
                    paramIndex++, paramIndex++, paramIndex++, paramIndex++, paramIndex++,
                    new ImmutableMethodReference(superClass, methodName, params, returnType)));

            implementation.addInstruction(new BuilderInstruction10x(Opcode.RETURN_VOID));

 

       List<MethodParameter> methodParams = params.stream().map(p ->
                new ImmutableMethodParameter(p, null,
                        // use three random letters as param names
                        RandomStringUtils.random(3, true, false).toLowerCase()))
                .collect(Collectors.toList());

        return new ImmutableMethod(
                classDef.toString(),
                methodName,
                methodParams,
                returnType,
                4,
                null,
                null,
                implementation);

@localacct
Copy link
Author

Thanks, we will take a look at that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants