Ignore shrinkunusedprotofields if protobuf-lite runtime is missing

Bug: 185798002
Change-Id: Icff35dcff22f8d7877c2d1490a573357be2cfc99
diff --git a/src/main/java/com/android/tools/r8/graph/AppView.java b/src/main/java/com/android/tools/r8/graph/AppView.java
index 1cd36de..1515bf6 100644
--- a/src/main/java/com/android/tools/r8/graph/AppView.java
+++ b/src/main/java/com/android/tools/r8/graph/AppView.java
@@ -128,12 +128,7 @@
 
     this.libraryMethodSideEffectModelCollection = new LibraryMethodSideEffectModelCollection(this);
     this.libraryMemberOptimizer = new LibraryMemberOptimizer(this);
-
-    if (enableWholeProgramOptimizations() && options().protoShrinking().isProtoShrinkingEnabled()) {
-      this.protoShrinker = new ProtoShrinker(withLiveness());
-    } else {
-      this.protoShrinker = null;
-    }
+    this.protoShrinker = ProtoShrinker.create(withLiveness());
   }
 
   public boolean verifyMainThread() {
diff --git a/src/main/java/com/android/tools/r8/ir/analysis/proto/ProtoShrinker.java b/src/main/java/com/android/tools/r8/ir/analysis/proto/ProtoShrinker.java
index a0f75d7..c83f118 100644
--- a/src/main/java/com/android/tools/r8/ir/analysis/proto/ProtoShrinker.java
+++ b/src/main/java/com/android/tools/r8/ir/analysis/proto/ProtoShrinker.java
@@ -10,6 +10,7 @@
 import com.android.tools.r8.ir.analysis.proto.schema.ProtoFieldTypeFactory;
 import com.android.tools.r8.shaking.AppInfoWithLiveness;
 import com.android.tools.r8.utils.InternalOptions;
+import com.android.tools.r8.utils.StringDiagnostic;
 import com.google.common.collect.Sets;
 import java.util.Set;
 
@@ -26,9 +27,8 @@
 
   private Set<DexType> deadProtoTypes = Sets.newIdentityHashSet();
 
-  public ProtoShrinker(AppView<AppInfoWithLiveness> appView) {
+  public ProtoShrinker(AppView<AppInfoWithLiveness> appView, ProtoReferences references) {
     ProtoFieldTypeFactory factory = new ProtoFieldTypeFactory();
-    ProtoReferences references = new ProtoReferences(appView.dexItemFactory());
     this.decoder = new RawMessageInfoDecoder(factory, references);
     this.factory = factory;
     this.generatedExtensionRegistryShrinker =
@@ -54,6 +54,26 @@
     this.references = references;
   }
 
+  public static ProtoShrinker create(AppView<AppInfoWithLiveness> appView) {
+    if (!appView.enableWholeProgramOptimizations()
+        || !appView.options().protoShrinking().isProtoShrinkingEnabled()) {
+      return null;
+    }
+
+    ProtoReferences references = new ProtoReferences(appView.dexItemFactory());
+    if (appView.definitionFor(references.generatedMessageLiteType) == null) {
+      appView
+          .reporter()
+          .warning(
+              new StringDiagnostic(
+                  "Ignoring -shrinkunusedprotofields since the protobuf-lite runtime is missing"));
+      appView.options().protoShrinking().disable();
+      return null;
+    }
+
+    return new ProtoShrinker(appView, references);
+  }
+
   public Set<DexType> getDeadProtoTypes() {
     return deadProtoTypes;
   }
diff --git a/src/main/java/com/android/tools/r8/utils/InternalOptions.java b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
index 2cb1e9e..f86a918 100644
--- a/src/main/java/com/android/tools/r8/utils/InternalOptions.java
+++ b/src/main/java/com/android/tools/r8/utils/InternalOptions.java
@@ -1228,6 +1228,14 @@
     // See b/174530756 for more details.
     public boolean enableProtoEnumSwitchMapShrinking = true;
 
+    public void disable() {
+      enableGeneratedExtensionRegistryShrinking = false;
+      enableGeneratedMessageLiteShrinking = false;
+      enableGeneratedMessageLiteBuilderShrinking = false;
+      traverseOneOfAndRepeatedProtoFields = false;
+      enableEnumLiteProtoShrinking = false;
+    }
+
     public boolean enableRemoveProtoEnumSwitchMap() {
       return isProtoShrinkingEnabled() && enableProtoEnumSwitchMapShrinking;
     }