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
This commit is contained in:
Izabela Orlowska
2018-02-12 11:03:42 +00:00
parent 40ce09581d
commit d31bc123a0
2 changed files with 18 additions and 0 deletions

View File

@@ -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 {};

View File

@@ -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 =