REAndroid / APKEditor

Powerful android apk editor - aapt/aapt2 independent
Apache License 2.0
647 stars 95 forks source link

Merging massive split APK generates broken APK #45

Closed Yehh22 closed 11 months ago

Yehh22 commented 11 months ago

Describe the bug It merges apk just fine without errors but apk is broken. Trying to modify apk via Winrar causes "archive is broken" and signing error causes "Exception in thread "main" com.android.apksig.apk.ApkFormatException: Malformed ZIP Central Directory record #1 at file offset 606531584" I'm using Java 20.0.1 on Windows 11

To Reproduce Steps to reproduce the behavior: java -jar APKEditor.jar m -i (apk folder)

Log/Stacktrace:

00.000 I: [MERGE] Merging ...
   Input: E:\Downloads\apk
 Output: E:\Downloads\apk_merged.apk
 ----------------------------
00.027 I: [MERGE] Searching apk files ...
00.031 I: [MERGE] Found apk files: 15
00.986 [MERGE] Loading: split_Weapon.apk
00.989 I: [MERGE] Found modules: 15
01.055 I: [MERGE] Merging string pools ...
01.071 I: [MERGE] Merged string pools=1, style=0, strings=1506
01.093 I: [MERGE] Merging: base
01.099 I: [MERGE] Added [base] classes.dex -> classes.dex
01.099 I: [MERGE] Added [base] classes2.dex -> classes2.dex
01.099 I: [MERGE] Merging resource table: base
22.982 [MERGE] Added: assets/bin/Data/96689ea54b64b9b48993d6d312021c56
22.983 I: [MERGE] Merging: split_MovieMapAb
23.015 [MERGE] Added: assets/assetpack/Movie/MapAbility/jp/mapability_32.usm
23.016 I: [MERGE] Merging: split_MovieEvent
23.043 [MERGE] Added: assets/assetpack/Movie/Event/sif.usm
23.044 I: [MERGE] Merging: split_Sound
23.060 [MERGE] Added: assets/assetpack/SEP.acb
23.061 I: [MERGE] Merging: split_Map
23.750 [MERGE] Added: assets/assetpack/AssetBundle/map/map1fe.bytes
23.751 I: [MERGE] Merging: split_Gallery
23.845 [MERGE] Added: assets/assetpack/AssetBundle/gallery/illust_007.bytes
23.846 I: [MERGE] Merging: split_GraCom
24.084 [MERGE] Added: assets/assetpack/AssetBundle/disc/jp/gracom/grac0f4_2.bytes
24.085 I: [MERGE] Merging: split_MiniMap
24.443 [MERGE] Added: assets/assetpack/AssetBundle/minimap/minimap_vf_gld02.bytes
24.444 I: [MERGE] Merging: split_MapObj
24.756 [MERGE] Added: assets/assetpack/AssetBundle/mapobj/mapobj0dc.bytes
24.757 I: [MERGE] Merging: split_BgEvent
24.811 [MERGE] Added: assets/assetpack/AssetBundle/disc/jp/bg_event/bg_event25.bytes
24.813 I: [MERGE] Merging: split_Disc
53.002 [MERGE] Added: assets/assetpack/AssetBundle/disc/jp/chranimext/11102/00231.aba.bytes
53.007 I: [MERGE] Merging: split_Weapon
53.141 [MERGE] Added: assets/assetpack/AssetBundle/weapon/w11109.bytes
53.142 I: [MERGE] Merging: split_Chr
53.928 [MERGE] Added: assets/assetpack/AssetBundle/chr/w11103.bytes
53.929 I: [MERGE] Merging: split_Effect
59.136 [MERGE] Added: assets/assetpack/AssetBundle/effect/tex_btl/b0069.bytes
59.139 I: [MERGE] Merging: split_Voice
59.153 [MERGE] Added: assets/assetpack/VOICE_JP.awb
59.416 I: [MERGE] Sanitizing manifest ...
59.417 I: [MERGE] Removed: extractNativeLibs
59.417 I: [MERGE] Removed: <meta-data android:name(@0x01010003)="com.android.stamp.source" android:value(@0x01010024)="https://play.google.com/store"/>
59.418 I: [MERGE] Removed: <meta-data android:name(@0x01010003)="com.android.stamp.type" android:value(@0x01010024)="STAMP_TYPE_DISTRIBUTION_APK"/>
59.426 I: [MERGE] Table size changed = 162904, 163020
59.426 I: [MERGE] Removed xml strings = 1
Xml size changed = 10416, 10408
59.427 I: [MERGE] Writing apk ...
01:00.237 I: [MERGE] Buffering compress changed files ...
01:00.323 I: [MERGE] Zip align ...
01:00.394 I: [MERGE] Writing files: 44616
01:46.498 I: [MERGE] Writing signature block ...
01:46.732 I: [MERGE] Saved to: E:\Downloads\apk_merged.apk

Used apk file https://mega.nz/file/3UwCRKjI#MnrptonVXIrg3IEZTc9YI4n50uNs7hiqR6Eic7-R4CA

Additional context There is an error when choosing zip file as input instead directory

java -jar APKEditor.jar m -i (zip file)

   Input: E:\Downloads\romancing-saga-minstrel-song-1.0.0.zip
 Output: E:\Downloads\romancing-saga-minstrel-song-1.0.0_merged.apk
 ----------------------------
00.026 I: [MERGE] Extracting to: E:\Downloads\tmp_ddb3b536
00.036 I: [MERGE] Searching apk files ...

ERROR:
No such directory: E:\Downloads\tmp_ddb3b536
java.io.FileNotFoundException: No such directory: E:\Downloads\tmp_ddb3b536
        at com.reandroid.apk.ApkBundle.loadApkDirectory(ApkBundle.java:146)
        at com.reandroid.apkeditor.merge.Merger.run(Merger.java:60)
        at com.reandroid.apkeditor.merge.Merger.execute(Merger.java:229)
        at com.reandroid.apkeditor.Main.execute(Main.java:72)
        at com.reandroid.apkeditor.Main.main(Main.java:41)
REAndroid commented 11 months ago

This issue is similar to #44

Yehh22 commented 11 months ago

Oh I see. If it's not possible to fix, could it be possible to recompress APK to maximum to reduce size, just like ordinary zip and 7z?

REAndroid commented 11 months ago

I already fixed the problem and i will push it soon

REAndroid commented 11 months ago

Fixed on 488591d Check the latest release

Yehh22 commented 11 months ago

Archive still broken and unable to zipalign and sign the apk file

Also it is still unable to read split zip files directly even tho APK files exists

00.000 I: [MERGE] Merging ...
   Input: E:\Romancing SaGa -Minstrel Song-\romancing-saga-minstrel-song-1.0.0.zip
 Output: E:\Romancing SaGa -Minstrel Song-\romancing-saga-minstrel-song-1.0.0_merged.apk
 ----------------------------
00.028 I: [MERGE] Extracting to: E:\Romancing SaGa -Minstrel Song-\tmp_b6fb23c3

ERROR:
No *.apk files found on: E:\Romancing SaGa -Minstrel Song-\romancing-saga-minstrel-song-1.0.0.zip
java.io.IOException: No *.apk files found on: E:\Romancing SaGa -Minstrel Song-\romancing-saga-minstrel-song-1.0.0.zip
        at com.reandroid.apkeditor.merge.Merger.extractFile(Merger.java:115)
        at com.reandroid.apkeditor.merge.Merger.run(Merger.java:54)
        at com.reandroid.apkeditor.merge.Merger.execute(Merger.java:228)
        at com.reandroid.apkeditor.Main.execute(Main.java:72)
        at com.reandroid.apkeditor.Main.main(Main.java:41)
REAndroid commented 11 months ago

Archive still broken and unable to zipalign and sign the apk file

It is already zip aligned , just try to sign it

Also it is still unable to read split zip files directly even tho APK files exists

Your zip compression is not supported by java, extract to directory manually and pass the path

Yehh22 commented 11 months ago

Ok. Here is what happen when signing APK

Exception in thread "main" com.android.apksig.apk.ApkFormatException: Malformed ZIP Central Directory record #1 at file offset 606531584
    at com.android.apksig.ApkSigner.parseZipCentralDirectory(ApkSigner.java:879)
    at com.android.apksig.ApkSigner.sign(ApkSigner.java:268)
    at com.android.apksig.ApkSigner.sign(ApkSigner.java:223)
    at com.android.apksigner.ApkSignerTool.sign(ApkSignerTool.java:395)
    at com.android.apksigner.ApkSignerTool.main(ApkSignerTool.java:92)
 Caused by: com.android.apksig.zip.ZipFormatException: Not a Central Directory record. Signature: 0x71748920
    at com.android.apksig.internal.zip.CentralDirectoryRecord.getRecord(CentralDirectoryRecord.java:143)
    at com.android.apksig.ApkSigner.parseZipCentralDirectory(ApkSigner.java:877)
    ... 4 more
REAndroid commented 11 months ago

Oh I confirm this, i am investigating the cause

REAndroid commented 11 months ago

Android do not support Zip64, anyways we have fixed to support huge files including merging from large zip files. Check the latest

Yehh22 commented 11 months ago

Oh, that explains why signing didn't work