Merge pull request #6875 from hoisie/android-x-issue

Remove instrumentedPackages workaround
diff --git a/.github/workflows/build_native_runtime.yml b/.github/workflows/build_native_runtime.yml
index 04b24cf..2d4f95e 100644
--- a/.github/workflows/build_native_runtime.yml
+++ b/.github/workflows/build_native_runtime.yml
@@ -16,8 +16,12 @@
     runs-on: ${{ matrix.os }}
     strategy:
       matrix:
-        os: [ubuntu-18.04, macos-latest]
+        os: [ubuntu-18.04, macos-latest, self-hosted]
     steps:
+      - name: Detect runner CPU architecture
+        run: |
+          echo "CPU_ARCH=$(uname -m)" >> $GITHUB_ENV
+
       - uses: actions/checkout@v2
         with:
           fetch-depth: 0
@@ -33,7 +37,7 @@
         uses: actions/cache@v2
         with:
           path: ~/icu-bin
-          key: ${{ runner.os }}-icu-${{ hashFiles('nativeruntime/external/icu/**') }}
+          key: ${{ runner.os }}-${{env.CPU_ARCH}}-icu-${{ hashFiles('nativeruntime/external/icu/**') }}
 
       - name: Build ICU for MacOS
         if: runner.os =='macOS' && steps.cache-icu.outputs.cache-hit != 'true'
@@ -63,26 +67,18 @@
       - name: Rename libnativeruntime for Linux
         if: runner.os == 'Linux'
         run: |
+          echo "NATIVERUNTIME_ARTIFACT_FILE=librobolectric-nativeruntime-linux-x86_64.so" >> $GITHUB_ENV
           mv build/libnativeruntime.so build/librobolectric-nativeruntime-linux-x86_64.so
 
       - name: Rename libnativeruntime for macOS
         if: runner.os == 'macOS'
         run: |
-          mv build/libnativeruntime.dylib build/librobolectric-nativeruntime-mac-x86_64.dylib
+          echo "NATIVERUNTIME_ARTIFACT_FILE=librobolectric-nativeruntime-mac-$(uname -m).dylib" >> $GITHUB_ENV
+          mv build/libnativeruntime.dylib build/librobolectric-nativeruntime-mac-$(uname -m).dylib
 
-      - name: Upload libnativeruntime for Linux
-        if: runner.os == 'Linux'
+      - name: Upload libnativeruntime
         uses: actions/upload-artifact@v2
         with:
-          name: librobolectric-nativeruntime-linux-x86_64.so
+          name: ${{env.NATIVERUNTIME_ARTIFACT_FILE}}
           path: |
-            build/librobolectric-nativeruntime-linux-x86_64.so
-            build/*.dylib
-
-      - name: Upload libnativeruntime for macOS
-        if: runner.os == 'macOS'
-        uses: actions/upload-artifact@v2
-        with:
-          name: librobolectric-nativeruntime-mac-x86_64.dylib
-          path: |
-            build/librobolectric-nativeruntime-mac-x86_64.dylib
+            build/${{env.NATIVERUNTIME_ARTIFACT_FILE}}
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
index 2b2a490..63ffb65 100644
--- a/.github/workflows/tests.yml
+++ b/.github/workflows/tests.yml
@@ -33,12 +33,16 @@
         with:
           java-version: 11.0.8
 
+      - name: Detect runner CPU architecture
+        run: |
+          echo "CPU_ARCH=$(uname -m)" >> $GITHUB_ENV
+
       - name: Cache ICU build output
         id: cache-icu
         uses: actions/cache@v2
         with:
           path: ~/icu-bin
-          key: ${{ runner.os }}-icu-${{ hashFiles('nativeruntime/external/icu/**') }}
+          key: ${{ runner.os }}-${{env.CPU_ARCH}}-icu-${{ hashFiles('nativeruntime/external/icu/**') }}
 
       - name: Build ICU
         if: steps.cache-icu.outputs.cache-hit != 'true'
diff --git a/integration_tests/dependency-on-stubs/src/test/java/org/robolectric/LoadWeirdClassesTest.java b/integration_tests/dependency-on-stubs/src/test/java/org/robolectric/LoadWeirdClassesTest.java
index 2d59969..0d4093f 100644
--- a/integration_tests/dependency-on-stubs/src/test/java/org/robolectric/LoadWeirdClassesTest.java
+++ b/integration_tests/dependency-on-stubs/src/test/java/org/robolectric/LoadWeirdClassesTest.java
@@ -22,18 +22,18 @@
 
   @Test
   @Config(sdk = KITKAT)
-  public void shouldLoadDisplay() throws Exception {
+  public void shouldLoadDisplay() {
     ReflectionHelpers.callInstanceMethod(
         Display.class, ShadowDisplay.getDefaultDisplay(), "getDisplayAdjustments");
   }
 
   @Test
-  public void reset_shouldWorkEvenIfSdkIntIsOverridden() throws Exception {
+  public void reset_shouldWorkEvenIfSdkIntIsOverridden() {
     ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 23);
   }
 
   @Test
-  public void shadowOf_shouldCompile() throws Exception {
+  public void shadowOf_shouldCompile() {
     assumeThat("Windows is an affront to decency.",
         File.separator, Matchers.equalTo("/"));
 
@@ -41,7 +41,7 @@
   }
 
   @Test
-  public void packageManager() throws Exception {
+  public void packageManager() {
     PackageInfo packageInfo = new PackageInfo();
     packageInfo.packageName = "test.package";
     shadowOf(RuntimeEnvironment.application.getPackageManager()).addPackage(packageInfo);
diff --git a/nativeruntime/build.gradle b/nativeruntime/build.gradle
index 1df5cfe..f61dbdb 100644
--- a/nativeruntime/build.gradle
+++ b/nativeruntime/build.gradle
@@ -81,28 +81,17 @@
   }
 }
 
-task copyNativeRuntime {
-  dependsOn makeNativeRuntime
-  doLast {
-    copy {
-      from ("$buildDir/cpp") {
-        include '*libnativeruntime.*'
-      }
-      rename { String fileName ->
-        fileName.replace("libnativeruntime", "librobolectric-nativeruntime")
-      }
-      into project.file("$buildDir/resources/main/native/${osName()}/${arch()}/")
-    }
-  }
-}
-
-jar {
-  def os = osName()
-  if (!System.getenv('SKIP_NATIVERUNTIME_BUILD') && (os.contains("linux") || os.contains("mac"))) {
-    dependsOn copyNativeRuntime
-  } else {
-    println("Skipping the nativeruntime build for OS '${System.getProperty("os.name")}'")
-  }
+processResources {
+ def os = osName()
+ onlyIf { !System.getenv('SKIP_NATIVERUNTIME_BUILD') && (os.contains("linux") || os.contains("mac")) }
+ dependsOn makeNativeRuntime
+ from ("$buildDir/cpp") {
+   include '*libnativeruntime.*'
+   rename { String fileName ->
+     fileName.replace("libnativeruntime", "librobolectric-nativeruntime")
+   }
+   into "native/${os}/${arch()}/"
+ }
 }
 
 dependencies {
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ImageUtil.java b/shadows/framework/src/main/java/org/robolectric/shadows/ImageUtil.java
index 3520464..75b4957 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ImageUtil.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ImageUtil.java
@@ -18,6 +18,7 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.util.Iterator;
+import javax.imageio.IIOException;
 import javax.imageio.IIOImage;
 import javax.imageio.ImageIO;
 import javax.imageio.ImageReader;
@@ -59,6 +60,10 @@
   }
 
   static RobolectricBufferedImage getImageFromStream(InputStream is) {
+    return getImageFromStream(null, is);
+  }
+
+  static RobolectricBufferedImage getImageFromStream(String fileName, InputStream is) {
     if (!initialized) {
       // Stops ImageIO from creating temp files when reading images
       // from input stream.
@@ -66,15 +71,18 @@
       initialized = true;
     }
 
+    String format = null;
     try {
       ImageInputStream imageStream = createImageInputStream(is);
       Iterator<ImageReader> readers = ImageIO.getImageReaders(imageStream);
-      if (!readers.hasNext()) return null;
+      if (!readers.hasNext()) {
+        return null;
+      }
 
       ImageReader reader = readers.next();
       try {
         reader.setInput(imageStream);
-        String format = reader.getFormatName();
+        format = reader.getFormatName();
         int minIndex = reader.getMinIndex();
         BufferedImage image = reader.read(minIndex);
         return RobolectricBufferedImage.create(image, ("image/" + format).toLowerCase());
@@ -82,6 +90,18 @@
         reader.dispose();
       }
     } catch (IOException e) {
+      Throwable cause = e.getCause();
+      if (FORMAT_NAME_PNG.equalsIgnoreCase(format)
+          && cause instanceof IIOException
+          && cause.getMessage() != null
+          && cause.getMessage().contains("Invalid chunk length")) {
+        String pngFileName = "(" + (fileName == null ? "not given PNG file name" : fileName) + ")";
+        System.err.println(
+            "The PNG file"
+                + pngFileName
+                + " cannot be decoded. This may be due to an OpenJDK issue with certain PNG files."
+                + " See https://github.com/robolectric/robolectric/issues/6812 for more details.");
+      }
       throw new RuntimeException(e);
     }
   }
diff --git a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBitmapFactory.java b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBitmapFactory.java
index 1e7c415..3be3294 100644
--- a/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBitmapFactory.java
+++ b/shadows/framework/src/main/java/org/robolectric/shadows/ShadowBitmapFactory.java
@@ -75,7 +75,8 @@
     final TypedValue value = new TypedValue();
     InputStream is = res.openRawResource(id, value);
 
-    RobolectricBufferedImage image = getImageFromStream(is);
+    String resourceName = res.getResourceName(id);
+    RobolectricBufferedImage image = getImageFromStream(resourceName, is);
     if (!allowInvalidImageData && image == null) {
       if (options != null) {
         options.outWidth = -1;
@@ -83,7 +84,7 @@
       }
       return null;
     }
-    Bitmap bitmap = create("resource:" + res.getResourceName(id), options, image);
+    Bitmap bitmap = create("resource:" + resourceName, options, image);
     ShadowBitmap shadowBitmap = Shadow.extract(bitmap);
     shadowBitmap.createdFromResId = id;
     return bitmap;
@@ -102,7 +103,7 @@
     if (pathName != null && new File(pathName).exists()) {
       try (FileInputStream fileInputStream = new FileInputStream(pathName);
           BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream)) {
-        image = getImageFromStream(bufferedInputStream);
+        image = getImageFromStream(pathName, bufferedInputStream);
       } catch (IOException e) {
         Logger.warn("Error getting size of bitmap file", e);
       }