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 copy class to new dex using dexlib2 #877

Open
yujack008 opened this issue Sep 8, 2023 · 4 comments
Open

how to copy class to new dex using dexlib2 #877

yujack008 opened this issue Sep 8, 2023 · 4 comments

Comments

@yujack008
Copy link

			Set<? extends DexBackedClassDef> defs = dexBackedDexFile.getClasses();

			for (DexBackedClassDef def : defs) {
				dexBuilder.internClassDef(def.getType(), def.getAccessFlags(),
						def.getSuperclass(), def.getInterfaces(), def.getSourceFile(),
						def.getAnnotations(), def.getFields(),def.getMethods());
			}

def.getFields() and def.getMethods() ERROR

@auermich93
Copy link

I use the following code for plain dexlib2 to write the classes to a new dex file:

    public static void writeToDexFile(String filePath, List<ClassDef> classes, int opCode) throws IOException {

        DexFileFactory.writeDexFile(filePath, new DexFile() {
            @Nonnull
            @Override
            public Set<? extends ClassDef> getClasses() {
                return new AbstractSet<ClassDef>() {
                    @Nonnull
                    @Override
                    public Iterator<ClassDef> iterator() {
                        return classes.iterator();
                    }

                    @Override
                    public int size() {
                        return classes.size();
                    }
                };
            }

            @Nonnull
            @Override
            public Opcodes getOpcodes() {
                return Opcodes.forApi(opCode);
            }
        });
    }

And the following code to create/modify an existing class:

        List<Method> instrumentedMethods = Lists.newArrayList(classDef.getMethods()).parallelStream()
                .map(method -> instrumentMethod(dexFile, classDef, method))
                .collect(Collectors.toList());

        return new ImmutableClassDef(
                classDef.getType(),
                classDef.getAccessFlags(),
                classDef.getSuperclass(),
                classDef.getInterfaces(),
                classDef.getSourceFile(),
                classDef.getAnnotations(),
                classDef.getFields(),
                instrumentedMethods);

@yujack008
Copy link
Author

yujack008 commented Sep 30, 2023

@auermich93 thinks.
then,
how to get a class all import class.
i want to write a class and all depends import class to a dex file.

@auermich93
Copy link

@yujack008 There are no explicit imports like in a .java file. You have to iterate over all fields and instructions and collect this information on your own.

@xudabeauty
Copy link

i want to move the useless classes to a new dex ,how to?

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

3 participants