private int count() {

        int size = 0;

        size += 8 + 4;// chunk, pkgcount
        {
            int stringSize = strTable0.getSize();
            if (stringSize % 4 != 0) {
                stringSize += 4 - stringSize % 4;
            }
            size += 8 + stringSize;// global strings
        }
        for (PkgCtx ctx : ctxs) {
            ctx.offset = size;
            int pkgSize = 0;
            pkgSize += 8 + 4 + 256;// chunk,pid+name
            pkgSize += 4 * 4;

            ctx.typeStringOff = pkgSize;
            {
                int stringSize = ctx.typeNames0.getSize();
                if (stringSize % 4 != 0) {
                    stringSize += 4 - stringSize % 4;
                }
                pkgSize += 8 + stringSize;// type names
            }

            ctx.keyStringOff = pkgSize;

            {
                int stringSize = ctx.keyNames0.getSize();
                if (stringSize % 4 != 0) {
                    stringSize += 4 - stringSize % 4;
                }
                pkgSize += 8 + stringSize;// key names
            }

            for (Type type : ctx.pkg.types.values()) {
                type.wPosition = size + pkgSize;
                pkgSize += 8 + 4 + 4 + 4 * type.specs.length; // trunk,id,entryCount,
                // configs

                for (Config config : type.configs) {
                    config.wPosition = pkgSize + size;
                    int configBasePostion = pkgSize;
                    pkgSize += 8 + 4 + 4 + 4; // trunk,id,entryCount,entriesStart
                    int size0 = config.id.length;
                    if (size0 % 4 != 0) {
                        size0 += 4 - size0 % 4;
                    }
                    pkgSize += size0;// config

                    if (pkgSize - configBasePostion > 0x0038) {
                        throw new RuntimeException("config id  too big");
                    } else {
                        pkgSize = configBasePostion + 0x0038;
                    }

                    pkgSize += 4 * config.entryCount;// offset
                    config.wEntryStart = pkgSize - configBasePostion;
                    int entryBase = pkgSize;
                    for (ResEntry e : config.resources.values()) {
                        e.wOffset = pkgSize - entryBase;
                        pkgSize += 8;// size,flag,keyString
                        if (e.value instanceof BagValue) {
                            BagValue big = (BagValue) e.value;
                            pkgSize += 8 + big.map.size() * 12;
                        } else {
                            pkgSize += 8;
                        }
                    }
                    config.wChunkSize = pkgSize - configBasePostion;
                }
            }
            ctx.pkgSize = pkgSize;
            size += pkgSize;
        }

        return size;
    }
Write pkgs to an arsc file

@author bob
@see ArscParser
android/third_party/apkbuilder/src/main/java/pxb/android/arsc/ArscWriter.java