From d31bc123a06d2ffc2ca1b76a591a07865744ae12 Mon Sep 17 00:00:00 2001 From: Izabela Orlowska Date: Mon, 12 Feb 2018 11:03:42 +0000 Subject: 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 --- tools/aapt2/java/ManifestClassGenerator.cpp | 6 ++++++ tools/aapt2/java/ManifestClassGenerator_test.cpp | 12 ++++++++++++ 2 files changed, 18 insertions(+) (limited to 'tools/aapt2/java') diff --git a/tools/aapt2/java/ManifestClassGenerator.cpp b/tools/aapt2/java/ManifestClassGenerator.cpp index c4b36176aa71..be67c9c8c03c 100644 --- a/tools/aapt2/java/ManifestClassGenerator.cpp +++ b/tools/aapt2/java/ManifestClassGenerator.cpp @@ -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 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 {}; diff --git a/tools/aapt2/java/ManifestClassGenerator_test.cpp b/tools/aapt2/java/ManifestClassGenerator_test.cpp index f4e10ab2e584..ab7f9a11d971 100644 --- a/tools/aapt2/java/ManifestClassGenerator_test.cpp +++ b/tools/aapt2/java/ManifestClassGenerator_test.cpp @@ -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 context = test::ContextBuilder().Build(); + std::unique_ptr manifest = test::BuildXmlDom(R"( + + + )"); + + 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 manifest_class = -- cgit v1.2.3-59-g8ed1b