Skip to content

Commit

Permalink
fix: Extract assets from APK file
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-psi committed Jan 19, 2024
1 parent 29e3589 commit 2f1eab8
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 7 deletions.
9 changes: 4 additions & 5 deletions AndroidCompat/getAndroid.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fi
echo "Getting required Android.jar..."
rm -rf "tmp"
mkdir -p "tmp"
pushd "tmp"
pushd "tmp" || exit 1

curl "https://android.googlesource.com/platform/prebuilts/sdk/+/6cd31be5e4e25901aadf838120d71a79b46d9add/30/public/android.jar?format=TEXT" | base64 --decode > android.jar

Expand Down Expand Up @@ -71,20 +71,19 @@ zip --delete android.jar android/text/Html.class
# Dedup overridden Android classes
ABS_JAR="$(realpath android.jar)"
function dedup() {
pushd "$1"
pushd "$1" || exit 1
CLASSES="$(find ./* -type f)"
echo "$CLASSES" | while read -r class
do
NAME="${class%.*}"
echo "Processing class: $NAME"
zip --delete "$ABS_JAR" "$NAME.class" "$NAME\$*.class" "${NAME}Kt.class" "${NAME}Kt\$*.class" > /dev/null
done
popd
popd || exit 1
}

popd
popd || exit 1
dedup AndroidCompat/src/main/java
dedup server/src/main/kotlin

echo "Copying Android.jar to library folder..."
mv tmp/android.jar AndroidCompat/lib
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ configure(projects) {
implementation("io.github.config4k:config4k:0.4.2")

// dex2jar
val dex2jarVersion = "v71"
val dex2jarVersion = "v76"
implementation("com.github.ThexXTURBOXx.dex2jar:dex-translator:$dex2jarVersion")
implementation("com.github.ThexXTURBOXx.dex2jar:dex-tools:$dex2jarVersion")

Expand Down
2 changes: 1 addition & 1 deletion inspector/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ sourceSets {
}

// should be bumped with each stable release
val inspectorVersion = "v1.4.3"
val inspectorVersion = "v1.5.0"

// counts commit count on master
val inspectorRevision = runCatching {
Expand Down
55 changes: 55 additions & 0 deletions inspector/src/main/kotlin/inspector/util/Extension.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import inspector.util.PackageTools.getPackageInfo
import inspector.util.PackageTools.loadExtensionSources
import mu.KotlinLogging
import java.io.File
import java.io.FileOutputStream
import java.util.zip.ZipEntry
import java.util.zip.ZipInputStream
import java.util.zip.ZipOutputStream

object Extension {
private val logger = KotlinLogging.logger {}
Expand Down Expand Up @@ -54,6 +58,7 @@ object Extension {
logger.trace("Main class for extension is $className")

dex2jar(apkFile, jarFile)
extractAssetsFromApk(apkFile, jarFile)

// collect sources from the extension
return packageInfo.packageName to when (
Expand All @@ -65,4 +70,54 @@ object Extension {
else -> throw RuntimeException("Unknown source class type! ${instance.javaClass}")
}
}

private fun extractAssetsFromApk(apkFile: File, jarFile: File) {
val assetsFolder = File("${apkFile.parent}/${apkFile.nameWithoutExtension}_assets")

assetsFolder.mkdirs()

ZipInputStream(apkFile.inputStream()).use { zis ->
generateSequence { zis.nextEntry }
.filter { it.name.startsWith("assets/") }
.forEach {
val assetFile = File(assetsFolder, it.name)

assetFile.parentFile.mkdirs()
FileOutputStream(assetFile).use { os -> zis.copyTo(os) }
}
zis.closeEntry()
zis.close()
}

val tempJarFile = File("${jarFile.parent}/${jarFile.nameWithoutExtension}_temp.jar")

ZipInputStream(jarFile.inputStream()).use { zis ->
val zos = ZipOutputStream(FileOutputStream(tempJarFile))

generateSequence { zis.nextEntry }
.filterNot { it.name.startsWith("META-INF/") }
.forEach {
zos.putNextEntry(ZipEntry(it.name))
zis.copyTo(zos)
zos.closeEntry()
}

assetsFolder.walkTopDown()
.filter { it.isFile }
.forEach {
zos.putNextEntry(ZipEntry(it.relativeTo(assetsFolder).toString().replace('\\', '/')))
it.inputStream().copyTo(zos)
zos.closeEntry()
}

zis.closeEntry()
zis.close()
zos.closeEntry()
zos.close()
}

jarFile.delete()
tempJarFile.renameTo(jarFile)
assetsFolder.deleteRecursively()
}
}

0 comments on commit 2f1eab8

Please sign in to comment.