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

Parse additional dex files created in uncommon way #66

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions parser/src/main/kotlin/com/linkedin/dex/parser/DexParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private class DexParserCommand : CliktCommand() {
@JvmStatic
@JvmOverloads
fun findTestNames(apkPath: String, customAnnotations: List<String> = emptyList()): List<String> {
return findTestMethods(apkPath, customAnnotations).map { it.testName }
return findTestMethods(apkPath, customAnnotations).map { it.testName }.distinct()
}

/**
Expand All @@ -84,14 +84,35 @@ private class DexParserCommand : CliktCommand() {
fun readDexFiles(path: String): List<DexFile> {
ZipInputStream(FileInputStream(File(path))).use { zip ->
return zip.entries()
.filter { it.name.endsWith(".dex") }
.map { zip.readBytes() }
.filter {
it.name.endsWith(".dex") or
(it.name.contains("secondary-program-dex-jars")
and it.name.endsWith(".jar"))
}
.map {
if (it.name.endsWith(".jar")) {
readSecondaryDex(zip.readBytes())
} else {
zip.readBytes()
}
}
.map { ByteBuffer.wrap(it) }
.map(::DexFile)
.toList()
}
}

private fun readSecondaryDex(jardex: ByteArray): ByteArray {
ZipInputStream(jardex.inputStream()).use { zip ->
return zip.entries()
.filter {
it.name.endsWith(".dex")
}
.map{ zip.readBytes() }
.first()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why this is only the first - isn't the goal to supoprt multidex by putting the extra dex filess inside this jar?

}
}

private fun ZipInputStream.entries(): Sequence<ZipEntry> {
return object : Sequence<ZipEntry> {
override fun iterator(): Iterator<ZipEntry> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private fun findJUnit3Tests(dexFiles: List<DexFile>, testNames: MutableSet<TestM
private fun DexFile.findJUnit3Tests(descriptors: MutableSet<String>): List<TestMethod> {
val testClasses = findClassesWithSuperClass(descriptors)
return createTestMethods(testClasses, { classDef, _ -> findMethodIds(classDef) })
.filter { it.testName.contains("#test") }
.filter { it.testName.contains("#test") and !it.testName.endsWith("#testFoo") }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a strange behavior to bake into a library - I imagine your code may use testFoo as a fake test, but from the libraries perspective (as well ass junit afaik) it is a valid test, so I'm not ure we should be making assumptions about intention. Thoughts?

}

fun DexFile.findMethodIds(classDefItem: ClassDefItem): MutableList<MethodIdItem> {
Expand Down