Android Studio - AAPT:错误:文件无法使用 PNG 图像进行编译

问题描述 投票:0回答:8

我当时在 Ubuntu linux 上工作,最近我撤回了我的项目并开始在 Win10 上工作。

从现在开始我无法构建/运行我的应用程序。资源文件夹中的那些PNG文件一定有问题。

这是尝试构建应用程序时的错误输出

Executing tasks: [:app:assembleDebug] in project C:\Users\Psz\Documents\Android-ChatRoom-Client

> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:javaPreCompileDebug UP-TO-DATE
> Task :app:generateDebugResValues UP-TO-DATE
> Task :app:generateDebugResources UP-TO-DATE
> Task :app:createDebugCompatibleScreenManifests UP-TO-DATE
> Task :app:extractDeepLinksDebug UP-TO-DATE
> Task :app:processDebugManifest UP-TO-DATE
> Task :app:mergeDebugShaders UP-TO-DATE
> Task :app:compileDebugShaders NO-SOURCE
> Task :app:generateDebugAssets UP-TO-DATE
> Task :app:mergeDebugAssets UP-TO-DATE
> Task :app:processDebugJavaRes NO-SOURCE

> Task :app:mergeDebugResources FAILED
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-ldpi\\ic_launcher_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-ldpi\\ic_launcher_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-xhdpi\\ic_greensky_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-xhdpi\\ic_greensky_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-hdpi\\ic_launcher_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-hdpi\\ic_launcher_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-mdpi\\ic_greensky_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-mdpi\\ic_greensky_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-mdpi\\ic_launcher_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-mdpi\\ic_launcher_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-xxhdpi\\ic_greensky_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-xxhdpi\\ic_greensky_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-hdpi\\ic_greensky_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-hdpi\\ic_greensky_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android resource compilation failed","sources":[{"file":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-ldpi\\ic_greensky_background.png"}],"original":"C:\\Users\\Psz\\Documents\\Android-ChatRoom-Client\\app\\build\\generated\\res\\pngs\\debug\\drawable-ldpi\\ic_greensky_background.png: AAPT: error: file failed to compile.\n    ","tool":"AAPT"}

> Task :app:mergeDebugJavaResource

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:mergeDebugResources'.
> Multiple task action failures occurred:
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-ldpi\ic_greensky_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-mdpi\ic_greensky_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-ldpi\ic_launcher_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-hdpi\ic_greensky_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-mdpi\ic_launcher_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-xhdpi\ic_greensky_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-hdpi\ic_launcher_background.png: AAPT: error: file failed to compile.
            
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > Android resource compilation failed
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-xxhdpi\ic_greensky_background.png: AAPT: error: file failed to compile.
            

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 1s
10 actionable tasks: 2 executed, 8 up-to-date

build.gradle(:app) 文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30

    configurations.all {
        resolutionStrategy.eachDependency{
            DependencyResolveDetails details ->
                def requested = details.requested
                if (requested.group=="com.android.support"){
                    if (!requested.name.startsWith("multidex")){
                        details.useVersion("26.+")
                    }
                }
        }
    }

    buildToolsVersion "30.0.2"

    defaultConfig {
        applicationId "com.myname.greenskychatroom"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation files('libs/json-simple-1.1.jar')
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

我不知道是什么原因造成的。我清理了缓存,删除并再次拉取项目。我也在网上找不到解决方案。

更新 发现 Android Studio 在 Linux(Ubuntu) 上运行得很好,事实上这个错误只发生在 Windows 的 Android Studio 上..

java android android-studio gradle aapt
8个回答
13
投票

我遇到了与此类似的问题,根本原因是JPEG文件以PNG文件扩展名命名。一旦我修复了文件扩展名 AAPT 就很高兴。


3
投票

如果某些文件已加密,AAPT 可能无法正常工作。请检查与编译错误相关的文件是否被加密。在您的情况下,这些是 PNG 文件。


3
投票

在我的情况下,我尝试使用一些从whatsapp下载的png图像,它们是加密的。

要修复它,请按照以下步骤操作:

1-开漆 2-打开您的图像 3-保存它们

你就可以出发了!


3
投票

如果它是一个博览会应用程序,请删除您刚刚添加的所有 png 资源。您可以将它们重命名为 jpg 就可以了


1
投票

我有完全相同的错误。尝试了一切方法,包括拉出自己的听力,但没有任何效果。然后有人提到运行Android Studio作为

admin
可以解决问题...所以它与
write access
问题有关。答对了!试试这个:

In windows go to Ransomware protection and disable it!

然后再次尝试构建。如果有效,请使用

Allow an app through Controlled folder access
为 gradle 授予正确的访问权限。然后再次启用您的勒索软件防护。


0
投票

在 Windows 中,尽量将项目保存在与 Windows 不同的驱动器(C 盘)中。您可以使用 D 驱动器并尝试保持路径较短(例如 D:\Projects\XyzProject)。这可能会解决您的问题。


0
投票

我遇到了同样的问题,并解决了。这个错误是由于Windows安全性造成的,选择导致问题的文件属性并解锁它。


0
投票

今天我遇到这个问题,这个问题只不过是图像文件扩展名问题,我的图像是jpeg格式,但我将其保存在我的drawable文件夹中为png格式,所以这就是android引发此冲突的原因。当我发现这个问题时,我只是去谷歌搜索 jpg 到 png 转换器,然后我只是将我的图像转换为 png 版本并在我的 android 项目中实现并删除旧的 jpg 图像,然后 android 可以正常工作。希望这能解决您的问题。谢谢你。

© www.soinside.com 2019 - 2024. All rights reserved.