Merge "Workaround crash when calling 'new Path(null)'" into master-layoutlib-native
diff --git a/bridge/src/android/graphics/Path_Delegate.java b/bridge/src/android/graphics/Path_Delegate.java
new file mode 100644
index 0000000..6e84eea
--- /dev/null
+++ b/bridge/src/android/graphics/Path_Delegate.java
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.graphics;
+
+public class Path_Delegate {
+    static long nInit(long nPath) {
+        if (nPath == 0) {
+            throw new IllegalArgumentException(
+                    "A null argument to the Path constructor causes a crash in Android");
+        }
+        return Path.nInit(nPath);
+    }
+}
diff --git a/create/src/com/android/tools/layoutlib/create/CreateInfo.java b/create/src/com/android/tools/layoutlib/create/CreateInfo.java
index 49f263e..34d5c62 100644
--- a/create/src/com/android/tools/layoutlib/create/CreateInfo.java
+++ b/create/src/com/android/tools/layoutlib/create/CreateInfo.java
@@ -146,6 +146,7 @@
         new NioUtilsFreeBufferReplacer(),
         new ProcessInitializerInitSchedReplacer(),
         new ValidateNinePatchChunkReplacer(),
+        new NativeInitPathReplacer(),
     };
 
     /**
@@ -452,6 +453,7 @@
         "android.content.res.StringBlock#getColor",
         "android.graphics.Bitmap#setNinePatchChunk",
         "android.graphics.NinePatch#validateNinePatchChunk",
+        "android.graphics.Path#nInit",
         "android.media.ImageReader#nativeClassInit",
         "android.view.Choreographer#doFrame",
         "android.view.Choreographer#postCallbackDelayedInternal",
@@ -737,4 +739,18 @@
             mi.opcode = Opcodes.INVOKESTATIC;
         }
     }
+
+    public static class NativeInitPathReplacer implements MethodReplacer {
+        @Override
+        public boolean isNeeded(String owner, String name, String desc, String sourceClass) {
+            return "android/graphics/Path".equals(owner) &&
+                    "nInit".equals(name) && "(J)J".equals(desc);
+        }
+
+        @Override
+        public void replace(MethodInformation mi) {
+            mi.owner = "android/graphics/Path_Delegate";
+            mi.opcode = Opcodes.INVOKESTATIC;
+        }
+    }
 }