81

Gradle build failing with this error:

Error:C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png failed to read PNG signature: file does not start with PNG signature

Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
Error:Execution failed for task ':app:mergeDebugResources'.
Error: java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.internal.aapt.AaptException: AAPT2 compile failed:
aapt2 compile --legacy -o C:\dev\workspace\android2\MatrixCalculator\app\build\intermediates\res\merged\debug C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png
Issues:
 - ERROR: C:\Users\Roman\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.0.2.aar\bab547c3f1b8061ef9426f524a823a15\res\drawable-xhdpi-v4\abc_btn_switch_to_on_mtrl_00001.9.png failed to read PNG signature: file does not start with PNG signature

Some basic things i've tried to solve this issue:

  1. Invalidate caches/restart
  2. Deleting gradle folder
Zoe
  • 27,060
  • 21
  • 118
  • 148
Ooba Elda
  • 1,622
  • 1
  • 12
  • 18
  • 2
    The file `abc_btn_switch_to_on_mtrl_00001.9.png` is corrupt, and is not a valid PNG file. – Matt Clark Sep 12 '17 at 13:21
  • "Deleting gradle folder" -- which specific folder did you delete? – CommonsWare Sep 12 '17 at 13:24
  • @CommonsWare the one at C:\Users\Roman\.gradle\caches and the one in the project – Ooba Elda Sep 12 '17 at 13:25
  • You don't say if this is a new resource or an existing one that is now showing this symptom. This is an important detail that should be in the _body of the question itself_. –  Sep 12 '17 at 13:46
  • @jdv what do you mean by resource? The file is located in appcompat-v7-26.0.2.aar folder, so i suppose it's the part of this library – Ooba Elda Sep 12 '17 at 13:50
  • A "resource" is anything that is not code, is in the resources directory, but is packaged into the final app. By default this is stuff in the /res/ directory of a typical Android project. But I see what you are saying. The PNG in a 3rd party is failing. –  Sep 12 '17 at 13:53
  • @jdv No, i didn't add any resources in the /res/ directory. Moreover i didn't add any new libraries to the build. The appcompat was there from the very start of the project – Ooba Elda Sep 12 '17 at 13:56
  • 1
    Try getting the support lib referenced from https://developer.android.com/topic/libraries/support-library/setup.html instead of your local SDK. You can force it to grab a different version instead of 26.0.2 via dependencies and see if the problem goes away. Looks like your local SDK is corrupt if blowing away the IDE cache is still a problem. –  Sep 12 '17 at 13:56
  • You can also explode this AAR as it lives in your SDK and actually check this resource to see if it is a real PNG. This is only if you want to gather info to post a bug against what looks like a beta version of Android Studio. –  Sep 12 '17 at 14:00
  • I'm also having this issue and I've resaved/re-exported the file using different programs, including photoshop, to be certain it's a png, but I still get this issue! – Elliptica Dec 08 '21 at 16:19

22 Answers22

146

It's likely a JPG renamed to a PNG file, not an actual PNG file
The problem could be because of the wrong extension of images.

In my case, the file was a JPEG image but it was saved as PNG not converted to. In this situation change extension to the real one and convert to PNG then retry.

For instance, you have ic_logo.png but it's actually a JPG image.
You should rename it back to ic_logo.jpg and use something like Photoshop to convert the image to PNG format.

Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
  • 8
    This was our root cause as well - graphic designer accidentally saved a JPEG with a .PNG extension. – Carl Anderson Jun 18 '18 at 22:34
  • 1
    but this image is in android libraries and not for project! – MHSaffari Jul 02 '19 at 10:53
  • @MHSFisher If you can report the developers of that library, do so, otherwise, see if you can replace the image using a standard one of your own. – Mahdi-Malv Jul 02 '19 at 11:44
  • 1
    This was the problem in our project as well. Renamed the file extensions to .jpg and then used Paint 3D to convert the image extensions to .png – Winter MC Jun 20 '21 at 10:35
31

Steps

  1. In build.gradle
aaptOptions {
    cruncherEnabled = false
}
  1. Delete content inside C:\Users\.gradle\caches (or ~/.gradle/caches for Mac and Linux)
  2. Restart Android Studio
Mahdi-Malv
  • 16,677
  • 10
  • 70
  • 117
Diego Venâncio
  • 5,698
  • 2
  • 49
  • 68
31
android {
    buildTypes {
        release {
            crunchPngs false // or true
        }
    }
}







 buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            crunchPngs false // or true

            lintOptions {
                checkReleaseBuilds false
                abortOnError false
            }
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
  • 2
    This worked for me, the error is very confusing b/c it makes it seem like your files aren't pngs when they are. In truth, it's crunching them that's the problem. Since I can crunch other things and only pngs are the issue, I prefer your solution to the `cruncherEnabled` version. – Elliptica Dec 09 '21 at 01:05
11

Failed to read PNG signature - Just put 1 line in your build.gradle

 buildTypes {
    release {
        minifyEnabled true
        shrinkResources true
        crunchPngs false  // Paste this line
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    }
}
Kumar Santanu
  • 603
  • 1
  • 7
  • 14
5

Creating the .apk of an application all the resources are analyzed, if some resource has an incorrect format or is corrup you will get the message:

.png failed to read PNG signature: file does not start with PNG signature

enter image description here

Be sure to have the correct resources, you can check it by opening your file.

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
3

This occurs due to the incorrect extension given to the image. For example, the image is jpg and the extension given is png.

Solutions:

  1. Add { cruncherEnabled = false } in build.gradle
  2. Convert the image into png rather than just changing the extension.
Saqib Naseeb
  • 731
  • 1
  • 9
  • 21
2

I had the same issue, solved by converting the images to webP from Android studio right-click on the image -> tap on Convert to WebP

the conversion will handle file formating and also reduce the size of the PNG

BennyP
  • 1,737
  • 1
  • 18
  • 24
2

if you are facing this issue in Flutter while buildings apk then add crunchPngs false

android {
    buildTypes {
        release {
          ...
           crunchPngs false
        }
 }

into android/app/build.gradle

enter image description here

1

You might have used a JPEG file or some other image file format.

Use PNG images to solve the error.

Drawable directory can only have png type of images.

1

This is a result of wrong file type indeed and can be fixed by saving the files in correct format.

I ran into this issue with PhoneGap Build and wanted to leave my 2 cents for any other PhoneGap / Cordova user who might also run into this.

My problem was that I upgraded my old PhoneGap 6.x to 8.0 and it seems behaviour of the default splash has changed. It used to require jpg as a default splash, but on 8.0 it results in this error on production builds.

This worked before:

<splash src="www/splash.jpg" />

Now you have 2 options. Replace the default splash with png like this:

<splash src="www/res/screen/android/screen-xhdpi-portrait.png" />

Other option is to remove default splash entirely.

Firze
  • 3,939
  • 6
  • 48
  • 61
1

For a quick fix ,Make shrinkResources to false instead of true in app.gradle Hope this will help. if Not then also add this aaptOptions { cruncherEnabled = false }

Prinkal Kumar
  • 3,196
  • 2
  • 10
  • 15
1

i have same error for slow i do some step:

  1. open abc_btn_switch_to_on_mtrl_00001.9.png (Image)file in paint
  2. now select png and save as and re-save in drawable and overwrite the old images and it works

enter image description here

dylan-myers
  • 323
  • 3
  • 18
1

If you don't have any transparent pixels, then renaming the file to .jpg worked for me.

Varun Barve
  • 323
  • 2
  • 8
  • 18
1

If you are using an online platform to generate App Icon(launcher_icon), then use PNG Format or First convert JPEG to PNG and then used APP ICON GENERATOR

paul
  • 519
  • 6
  • 13
1

In my case also there was a jpg file instead of png, so I changed my extension and solve my problem.

1

Open [Root_project]/app/build.gradle and add following lines.

release {
          ...

            crunchPngs false // or true

            lintOptions {
                checkReleaseBuilds false
                abortOnError false
            }

          ...
}
0

I'm not sure if it's a valid solution, but deleting only the file mentioned in the error message helped. While deleting the folder, containing it didn't.

Ooba Elda
  • 1,622
  • 1
  • 12
  • 18
0

I've had the same issue. To solve it just restart your Android Studio and build the gradle file again.

Dharman
  • 30,962
  • 25
  • 85
  • 135
0

You can add code in node_modules/react-native/react.gradle. After doFirst

doLast {
def moveFunc = { resSuffix ->
File originalDir = file("$buildDir/generated/res/react/release/${resSuffix}");
if (originalDir.exists()) {
File destDir = file("$buildDir/../src/main/res/${resSuffix}");
ant.move(file: originalDir, tofile: destDir);
}
}
moveFunc.curry("drawable-ldpi").call()
moveFunc.curry("drawable-mdpi").call()
moveFunc.curry("drawable-hdpi").call()
moveFunc.curry("drawable-xhdpi").call()
moveFunc.curry("drawable-xxhdpi").call()
moveFunc.curry("drawable-xxxhdpi").call()
moveFunc.curry("raw").call()
}

check it if it not fix issue after remove drawable folder github enter link description here

Jameel Nazir
  • 134
  • 2
  • 7
0

I was able to fix it permanently.

I created a new blank expo project, then I added a new app icon (correct png format), then I ejected from expo.

I then copied the mipmap-* folders from the blank project to my real project. It all worked perfectly.

Joseph Ajibodu
  • 1,093
  • 10
  • 14
0

I was getting the same error when signing APK.

This solution fixed the problem:

build.gradle(:app):

...
aaptOptions {
        cruncherEnabled = false
    }
...

You can check for more: https://developer.android.com/studio/build/optimize-your-build

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
-1

You can open the images with windows win10 'Paint', then you can save as .png and overrite it, It will solve the problem

Ucdemir
  • 2,852
  • 2
  • 26
  • 44