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

Commit d31bc123 authored by Izabela Orlowska's avatar Izabela Orlowska
Browse files

AAPT2: normalize Manifest java identifiers.

Currently AAPT2 does not allow permissions which last piece contains the
"-" symbol (since it is an illegal character for a java identifier).

AAPT1 would normalize the last piece, therefore creating a valid java
identifier.

This CL makes AAPT2 behave in a similar way to AAPT1, but instead of
modifying the original value of the permission string, modifies only the
java identifier part, leaving the permission string unchanged.

Fixes: 72980877
Test: updated
Change-Id: Ie44317e07407341ba3e91a84d9b06980547b3448
parent 40ce0958
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@
#include "Source.h"
#include "java/AnnotationProcessor.h"
#include "java/ClassDefinition.h"
#include "java/JavaClassGenerator.h"
#include "text/Unicode.h"
#include "util/Maybe.h"
#include "xml/XmlDom.h"
@@ -38,6 +39,11 @@ static Maybe<StringPiece> ExtractJavaIdentifier(IDiagnostics* diag, const Source
    result = result.substr(pos + 1);
  }

  // Normalize only the java identifier, leave the original value unchanged.
  if (result.contains("-")) {
    result = JavaClassGenerator::TransformToFieldName(result);
  }

  if (result.empty()) {
    diag->Error(DiagMessage(source) << "empty symbol");
    return {};
+12 −0
Original line number Diff line number Diff line
@@ -141,6 +141,18 @@ TEST(ManifestClassGeneratorTest, LastSeenPermissionWithSameLeafNameTakesPreceden
  EXPECT_THAT(actual, Not(HasSubstr("ACCESS_INTERNET=\"com.android.sample.ACCESS_INTERNET\";")));
}

TEST(ManifestClassGeneratorTest, NormalizePermissionNames) {
  std::unique_ptr<IAaptContext> context = test::ContextBuilder().Build();
  std::unique_ptr<xml::XmlResource> manifest = test::BuildXmlDom(R"(
        <manifest xmlns:android="http://schemas.android.com/apk/res/android">
          <permission android:name="android.permission.access-internet" />
        </manifest>)");

  std::string actual;
  ASSERT_TRUE(GetManifestClassText(context.get(), manifest.get(), &actual));
  EXPECT_THAT(actual, HasSubstr("access_internet=\"android.permission.access-internet\";"));
}

static ::testing::AssertionResult GetManifestClassText(IAaptContext* context, xml::XmlResource* res,
                                                       std::string* out_str) {
  std::unique_ptr<ClassDefinition> manifest_class =