Merge "Don't dereference a null pointer"
diff --git a/libs/androidfw/include/androidfw/StringPiece.h b/libs/androidfw/include/androidfw/StringPiece.h
index 99b4245..a33865f 100644
--- a/libs/androidfw/include/androidfw/StringPiece.h
+++ b/libs/androidfw/include/androidfw/StringPiece.h
@@ -32,6 +32,14 @@
 // WARNING: When creating from std::basic_string<>, moving the original
 // std::basic_string<> will invalidate the data held in a BasicStringPiece<>.
 // BasicStringPiece<> should only be used transitively.
+//
+// NOTE: When creating an std::pair<StringPiece, T> using std::make_pair(),
+// passing an std::string will first copy the string, then create a StringPiece
+// on the copy, which is then immediately destroyed.
+// Instead, create a StringPiece explicitly:
+//
+// std::string my_string = "foo";
+// std::make_pair<StringPiece, T>(StringPiece(my_string), ...);
 template <typename TChar>
 class BasicStringPiece {
  public:
diff --git a/tools/aapt2/java/JavaClassGenerator.cpp b/tools/aapt2/java/JavaClassGenerator.cpp
index 6b07b1e..db1561e 100644
--- a/tools/aapt2/java/JavaClassGenerator.cpp
+++ b/tools/aapt2/java/JavaClassGenerator.cpp
@@ -347,7 +347,9 @@
   }
 
   // Add the Styleable array to the Styleable class.
-  out_class_def->AddMember(std::move(array_def));
+  if (out_class_def != nullptr) {
+    out_class_def->AddMember(std::move(array_def));
+  }
 
   // Now we emit the indices into the array.
   for (size_t i = 0; i < attr_count; i++) {
@@ -578,7 +580,6 @@
   if (out_r_txt != nullptr) {
     r_txt_printer = util::make_unique<Printer>(out_r_txt);
   }
-
   // Generate an onResourcesLoaded() callback if requested.
   if (out != nullptr && options_.rewrite_callback_options) {
     rewrite_method =
diff --git a/tools/aapt2/java/JavaClassGenerator_test.cpp b/tools/aapt2/java/JavaClassGenerator_test.cpp
index e449546..10a97d8 100644
--- a/tools/aapt2/java/JavaClassGenerator_test.cpp
+++ b/tools/aapt2/java/JavaClassGenerator_test.cpp
@@ -438,4 +438,22 @@
   EXPECT_THAT(output, HasSubstr("com.boo.R.onResourcesLoaded"));
 }
 
+TEST(JavaClassGeneratorTest, OnlyGenerateRText) {
+  std::unique_ptr<ResourceTable> table =
+      test::ResourceTableBuilder()
+          .SetPackageId("android", 0x01)
+          .AddValue("android:attr/foo", ResourceId(0x01010000), util::make_unique<Attribute>())
+          .AddValue("android:styleable/hey.dude", ResourceId(0x01020000),
+                    test::StyleableBuilder()
+                        .AddItem("android:attr/foo", ResourceId(0x01010000))
+                        .Build())
+          .Build();
+
+  std::unique_ptr<IAaptContext> context =
+      test::ContextBuilder().SetPackageId(0x01).SetCompilationPackage("android").Build();
+  JavaClassGenerator generator(context.get(), table.get(), {});
+
+  ASSERT_TRUE(generator.Generate("android", nullptr));
+}
+
 }  // namespace aapt