Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6cc479b7 authored by Adam Lesinski's avatar Adam Lesinski
Browse files

AAPT2: Remove the need for specifying package name in compile phase

The compile phase doesn't use the AndroidManifest, so we had to specify the
package name on the command line.

We can omit the package name, since we don't resolve external references
in the compile phase. Packages that reference the current package will be encoded
with no package name. When loaded by the link phase, the package name will be supplied
and all the references with no package name will use that one.

Change-Id: I9fe4902b747b06899b45c968f30ba1aa05c5cd69
parent 6cb8e30b
Loading
Loading
Loading
Loading
+22 −2
Original line number Diff line number Diff line
@@ -116,9 +116,11 @@ private:
BinaryResourceParser::BinaryResourceParser(const std::shared_ptr<ResourceTable>& table,
                                           const std::shared_ptr<IResolver>& resolver,
                                           const Source& source,
                                           const std::u16string& defaultPackage,
                                           const void* data,
                                           size_t len) :
        mTable(table), mResolver(resolver), mSource(source), mData(data), mDataLen(len) {
        mTable(table), mResolver(resolver), mSource(source), mDefaultPackage(defaultPackage),
        mData(data), mDataLen(len) {
}

bool BinaryResourceParser::parse() {
@@ -177,6 +179,9 @@ bool BinaryResourceParser::getSymbol(const void* data, ResourceNameRef* outSymbo
            if (!type) {
                return false;
            }
            if (outSymbol->package.empty()) {
                outSymbol->package = mTable->getPackage();
            }
            outSymbol->type = *type;

            // Since we scan the symbol table in order, we can start looking for the
@@ -350,7 +355,22 @@ bool BinaryResourceParser::parsePackage(const ResChunk_header* chunk) {

    size_t len = strnlen16(reinterpret_cast<const char16_t*>(packageHeader->name),
            sizeof(packageHeader->name) / sizeof(packageHeader->name[0]));
    mTable->setPackage(StringPiece16(reinterpret_cast<const char16_t*>(packageHeader->name), len));
    if (mTable->getPackage().empty() && len == 0) {
        mTable->setPackage(mDefaultPackage);
    } else if (len > 0) {
        StringPiece16 thisPackage(reinterpret_cast<const char16_t*>(packageHeader->name), len);
        if (mTable->getPackage().empty()) {
            mTable->setPackage(thisPackage);
        } else if (thisPackage != mTable->getPackage()) {
            Logger::error(mSource)
                    << "incompatible packages: "
                    << mTable->getPackage()
                    << " vs. "
                    << thisPackage
                    << std::endl;
            return false;
        }
    }

    ResChunkPullParser parser(getChunkData(packageHeader->header),
                              getChunkDataLen(packageHeader->header));
+4 −3
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ public:
    BinaryResourceParser(const std::shared_ptr<ResourceTable>& table,
                         const std::shared_ptr<IResolver>& resolver,
                         const Source& source,
                         const std::u16string& defaultPackage,
                         const void* data, size_t len);

    BinaryResourceParser(const BinaryResourceParser&) = delete; // No copy.
@@ -97,12 +98,12 @@ private:

    const Source mSource;

    // The package name of the resource table.
    std::u16string mDefaultPackage;

    const void* mData;
    const size_t mDataLen;

    // The package name of the resource table.
    std::u16string mPackage;

    // The array of symbol entries. Each element points to an offset
    // in the table and an index into the symbol table string pool.
    const SymbolTable_entry* mSymbolEntries = nullptr;
+1 −1
Original line number Diff line number Diff line
@@ -160,7 +160,7 @@ const Attribute* Linker::doResolveAttribute(Reference& attribute, const SourceLi
void Linker::visit(Reference& reference, ValueVisitorArgs& a) {
    Args& args = static_cast<Args&>(a);

    if (!reference.name.isValid()) {
    if (reference.name.entry.empty()) {
        // We can't have a completely bad reference.
        if (!reference.id.isValid()) {
            Logger::error() << "srsly? " << args.referrer << std::endl;
+44 −46
Original line number Diff line number Diff line
@@ -756,8 +756,8 @@ bool link(const AaptOptions& options, const std::shared_ptr<ResourceTable>& outT
                zipFile->uncompress(entry));
        assert(uncompressedData);

        BinaryResourceParser parser(table, resolver, source, uncompressedData.get(),
                                    entry->getUncompressedLen());
        BinaryResourceParser parser(table, resolver, source, options.appInfo.package, 
                                    uncompressedData.get(), entry->getUncompressedLen());
        if (!parser.parse()) {
            return false;
        }
@@ -1085,14 +1085,7 @@ static AaptOptions prepareArgs(int argc, char** argv) {
    }

    bool isStaticLib = false;
    if (options.phase == AaptOptions::Phase::Compile ||
            options.phase == AaptOptions::Phase::Link) {
        if (options.phase == AaptOptions::Phase::Compile) {
            flag::requiredFlag("--package", "Android package name",
                    [&options](const StringPiece& arg) {
                        options.appInfo.package = util::utf8ToUtf16(arg);
                    });
        } else if (options.phase == AaptOptions::Phase::Link) {
    if (options.phase == AaptOptions::Phase::Link) {
        flag::requiredFlag("--manifest", "AndroidManifest.xml of your app",
                [&options](const StringPiece& arg) {
                    options.manifest = Source{ arg.toString() };
@@ -1124,11 +1117,15 @@ static AaptOptions prepareArgs(int argc, char** argv) {
                             false, &options.versionStylesAndLayouts);
    }

    if (options.phase == AaptOptions::Phase::Compile ||
            options.phase == AaptOptions::Phase::Link) {
        // Common flags for all steps.
        flag::requiredFlag("-o", "Output path", [&options](const StringPiece& arg) {
            options.output = Source{ arg.toString() };
        });
    } else if (options.phase == AaptOptions::Phase::DumpStyleGraph) {
    }

    if (options.phase == AaptOptions::Phase::DumpStyleGraph) {
        flag::requiredFlag("--style", "Name of the style to dump",
                [&options](const StringPiece& arg, std::string* outError) -> bool {
                    Reference styleReference;
@@ -1191,7 +1188,7 @@ static bool doDump(const AaptOptions& options) {
                zipFile->uncompress(entry));
        assert(uncompressedData);

        BinaryResourceParser parser(table, resolver, source, uncompressedData.get(),
        BinaryResourceParser parser(table, resolver, source, {}, uncompressedData.get(),
                                    entry->getUncompressedLen());
        if (!parser.parse()) {
            return false;
@@ -1223,16 +1220,17 @@ int main(int argc, char** argv) {
        if (!loadAppInfo(options.manifest, &options.appInfo)) {
            return false;
        }
    }

    // Verify we have some common options set.
        if (options.appInfo.package.empty()) {
            Logger::error() << "no package name specified." << std::endl;
            return false;
        }
    }

    // Every phase needs a resource table.
    std::shared_ptr<ResourceTable> table = std::make_shared<ResourceTable>();

    // The package name is empty when in the compile phase.
    table->setPackage(options.appInfo.package);
    if (options.appInfo.package == u"android") {
        table->setPackageId(0x01);
+1 −8
Original line number Diff line number Diff line
@@ -79,7 +79,7 @@ public:

        // Write the key.
        if (!Res_INTERNALID(key.id.id) && !key.id.isValid()) {
            assert(key.name.isValid());
            assert(!key.name.entry.empty());
            mSymbols->push_back(std::make_pair(ResourceNameRef(key.name),
                    mOut->size() - sizeof(*outMapEntry)));
        }
@@ -284,13 +284,6 @@ bool TableFlattener::flattenValue(BigBuffer* out, const FlatEntry& flatEntry,
bool TableFlattener::flatten(BigBuffer* out, const ResourceTable& table) {
    const size_t beginning = out->size();

    if (table.getPackage().size() == 0) {
        Logger::error()
                << "ResourceTable has no package name."
                << std::endl;
        return false;
    }

    if (table.getPackageId() == ResourceTable::kUnsetPackageId) {
        Logger::error()
                << "ResourceTable has no package ID set."
Loading