Also keep classes with static fields when generating an image.

If a class is initalized and has static fields, it needs to be added to
the image classes.

Test: flash a system image with an empty boot image profile
Bug: 260568122
Change-Id: I8708450ef13a266f441f7a4f646f6d8b26ea031d
diff --git a/dex2oat/driver/compiler_driver.cc b/dex2oat/driver/compiler_driver.cc
index f199ede..4f85140 100644
--- a/dex2oat/driver/compiler_driver.cc
+++ b/dex2oat/driver/compiler_driver.cc
@@ -1285,12 +1285,16 @@
           data_->image_class_descriptors_->erase(it);
         }
       } else if (can_include_in_image) {
-        // Check whether it is initialized and has a clinit. They must be kept, too.
-        if (klass->IsInitialized() && klass->FindClassInitializer(
-            Runtime::Current()->GetClassLinker()->GetImagePointerSize()) != nullptr) {
-          DCHECK(!Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass->GetDexCache()))
-              << klass->PrettyDescriptor();
-          data_->image_classes_.push_back(data_->hs_.NewHandle(klass));
+        // Check whether the class is initialized and has a clinit or static fields.
+        // Such classes must be kept too.
+        if (klass->IsInitialized()) {
+          PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
+          if (klass->FindClassInitializer(pointer_size) != nullptr ||
+              klass->NumStaticFields() != 0) {
+            DCHECK(!Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass->GetDexCache()))
+                << klass->PrettyDescriptor();
+            data_->image_classes_.push_back(data_->hs_.NewHandle(klass));
+          }
         }
       }
       return true;