Skip to content

Commit

Permalink
[dex038] write class version 1.7 if dex version > DEX_037
Browse files Browse the repository at this point in the history
  • Loading branch information
pxb1988 committed Jun 6, 2018
1 parent 5341bd1 commit 85b1b3b
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@ public abstract interface DexConstants {
String ANNOTATION_ENCLOSING_METHOD_TYPE = "Ldalvik/annotation/EnclosingMethod;";
String ANNOTATION_INNER_CLASS_TYPE = "Ldalvik/annotation/InnerClass;";
String ANNOTATION_MEMBER_CLASSES_TYPE = "Ldalvik/annotation/MemberClasses;";

int DEX_035 = 0x00303335;
int DEX_036 = 0x00303336;
int DEX_037 = 0x00303337;
int DEX_038 = 0x00303338;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package com.googlecode.d2j.node;

import com.googlecode.d2j.DexConstants;
import com.googlecode.d2j.visitors.DexClassVisitor;
import com.googlecode.d2j.visitors.DexFileVisitor;

Expand All @@ -24,6 +25,13 @@

public class DexFileNode extends DexFileVisitor {
public List<DexClassNode> clzs = new ArrayList<>();
public int dexVersion = DexConstants.DEX_035;

@Override
public void visitDexFileVersion(int version) {
this.dexVersion = version;
super.visitDexFileVersion(version);
}

@Override
public DexClassVisitor visit(int access_flags, String className, String superClass, String[] interfaceNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ public DexFileVisitor(DexFileVisitor visitor) {
this.visitor = visitor;
}

public void visitDexFileVersion(int version) {
if (visitor != null) {
visitor.visitDexFileVersion(version);
}
}

public DexClassVisitor visit(int access_flags, String className, String superClass, String[] interfaceNames) {
if (visitor == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@
import java.util.List;

public interface BaseDexFileReader {
int DEX_035 = 0x00303335;
int DEX_036 = 0x00303336;
int DEX_037 = 0x00303337;
int DEX_038 = 0x00303338;

int getDexVersion();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import com.googlecode.d2j.util.Mutf8;
import com.googlecode.d2j.visitors.*;

import static com.googlecode.d2j.DexConstants.*;

/**
* Open and read a dex file.this is the entrance of dex-reader. to read a dex/odex, use the following code:
*
Expand Down Expand Up @@ -618,6 +620,7 @@ public List<String> getClassNames() {
*/
@Override
public void accept(DexFileVisitor dv, int config) {
dv.visitDexFileVersion(this.dex_version);
for (int cid = 0; cid < class_defs_size; cid++) {
accept(dv, cid, config);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.googlecode.d2j.reader;

import com.googlecode.d2j.DexConstants;
import com.googlecode.d2j.util.zip.AccessBufByteArrayOutputStream;
import com.googlecode.d2j.util.zip.ZipEntry;
import com.googlecode.d2j.util.zip.ZipFile;
Expand Down Expand Up @@ -72,7 +73,7 @@ void init() {

@Override
public int getDexVersion() {
int max = DEX_035;
int max = DexConstants.DEX_035;
for (DexFileReader r : readers) {
int v = r.getDexVersion();
if (v > max) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,15 @@ public void visitEnd() {
new ExDex2Asm(exceptionHandler) {

@Override
public void convertDex(DexFileNode fileNode, final ClassVisitorFactory cvf) {
public void convertDex(final DexFileNode fileNode, final ClassVisitorFactory cvf) {
if (fileNode.clzs != null) {
final Map<String, Clz> classes = collectClzInfo(fileNode);
final List<Future<?>> results = new ArrayList<>(fileNode.clzs.size());
for (final DexClassNode classNode : fileNode.clzs) {
results.add(executorService.submit(new Runnable() {
@Override
public void run() {
convertClass(classNode, cvf, classes);
convertClass(fileNode, classNode, cvf, classes);
}
}));
}
Expand Down
18 changes: 14 additions & 4 deletions dex-translator/src/main/java/com/googlecode/d2j/dex/Dex2Asm.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,14 @@ protected static Map<String, Clz> collectClzInfo(DexFileNode fileNode) {
}

public void convertClass(DexClassNode classNode, ClassVisitorFactory cvf, DexFileNode fileNode) {
convertClass(classNode, cvf, collectClzInfo(fileNode));
convertClass(fileNode.dexVersion, classNode, cvf, collectClzInfo(fileNode));
}

public void convertClass(DexClassNode classNode, ClassVisitorFactory cvf) {
convertClass(classNode, cvf, new HashMap<String, Clz>());
convertClass(DexConstants.DEX_035, classNode, cvf);
}
public void convertClass(int dexVersion, DexClassNode classNode, ClassVisitorFactory cvf) {
convertClass(dexVersion, classNode, cvf, new HashMap<String, Clz>());
}

private static boolean isJavaIdentifier(String str) {
Expand All @@ -348,6 +351,12 @@ private static boolean isJavaIdentifier(String str) {
}

public void convertClass(DexClassNode classNode, ClassVisitorFactory cvf, Map<String, Clz> classes) {
convertClass(DexConstants.DEX_035, classNode, cvf, classes);
}
public void convertClass(DexFileNode dfn, DexClassNode classNode, ClassVisitorFactory cvf, Map<String, Clz> classes) {
convertClass(dfn.dexVersion, classNode, cvf, classes);
}
public void convertClass(int dexVersion, DexClassNode classNode, ClassVisitorFactory cvf, Map<String, Clz> classes) {
ClassVisitor cv = cvf.create(toInternalName(classNode.className));
if (cv == null) {
return;
Expand Down Expand Up @@ -391,7 +400,8 @@ public void convertClass(DexClassNode classNode, ClassVisitorFactory cvf, Map<St
}
access = clearClassAccess(isInnerClass, access);

cv.visit(Opcodes.V1_6, access, toInternalName(classNode.className), signature,
int version = dexVersion >= DexConstants.DEX_037 ? Opcodes.V1_7 : Opcodes.V1_6;

This comment has been minimized.

Copy link
@Lanchon

Lanchon Jun 29, 2018

please see: #194

cv.visit(version, access, toInternalName(classNode.className), signature,
classNode.superClass == null ? null : toInternalName(classNode.superClass), interfaceInterNames);

List<InnerClassNode> innerClassNodes = new ArrayList<InnerClassNode>(5);
Expand Down Expand Up @@ -447,7 +457,7 @@ public void convertDex(DexFileNode fileNode, ClassVisitorFactory cvf) {
if (fileNode.clzs != null) {
Map<String, Clz> classes = collectClzInfo(fileNode);
for (DexClassNode classNode : fileNode.clzs) {
convertClass(classNode, cvf, classes);
convertClass(fileNode, classNode, cvf, classes);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ public ClassVisitor create(String classInternalName) {
CfOptions cfOptions = new CfOptions();
cfOptions.strictNameCheck = false;
DexOptions dexOptions = new DexOptions();
if (fileNode.dexVersion >= DexConstants.DEX_037) {
dexOptions.minSdkVersion = 26;
}

DirectClassFile dcf = new DirectClassFile(data, rca.getClassName() + ".class", true);
dcf.setAttributeFactory(new StdAttributeFactory());
Expand Down

0 comments on commit 85b1b3b

Please sign in to comment.