Android Studio – AAPT: 错误:文件在编译时与PNG图像失败

huangapple go评论79阅读模式
英文:

Android Studio - AAPT: error: file failed to compile with PNG images

问题

以下是翻译好的内容:

我之前在Ubuntu Linux上工作,最近我拉取了我的项目,并开始在Win10上工作。

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

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

正在执行任务:[:app:assembleDebug] 位于项目路径 C:\Users\Psz\Documents\Android-ChatRoom-Client

> 任务:app:preBuild 已更新
> 任务:app:preDebugBuild 已更新
> 任务:app:compileDebugAidl 无源
> 任务:app:generateDebugBuildConfig 已更新
> 任务:app:compileDebugRenderscript 无源
> 任务:app:javaPreCompileDebug 已更新
> 任务:app:generateDebugResValues 已更新
> 任务:app:generateDebugResources 已更新
> 任务:app:createDebugCompatibleScreenManifests 已更新
> 任务:app:extractDeepLinksDebug 已更新
> 任务:app:processDebugManifest 已更新
> 任务:app:mergeDebugShaders 已更新
> 任务:app:compileDebugShaders 无源
> 任务:app:generateDebugAssets 已更新
> 任务:app:mergeDebugAssets 已更新
> 任务:app:processDebugJavaRes 无源

> 任务:app:mergeDebugResources 失败
AGPBI: {"kind":"error","text":"Android 资源编译失败","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: 无法编译文件。\n    ","tool":"AAPT"}
AGPBI: {"kind":"error","text":"Android 资源编译失败","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: 无法编译文件。\n    ","tool":"AAPT"}
(更多错误信息...)

> 任务:app:mergeDebugJavaResource

构建失败:发生异常。

* 出现了什么问题:
任务“:app:mergeDebugResources”的执行失败。
> 多个任务动作失败:
   > 在执行 com.android.build.gradle.internal.tasks.Workers$ActionFacade 时发生失败
      > Android 资源编译失败
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-ldpi\ic_greensky_background.png: AAPT: error: 无法编译文件。
            
   > 在执行 com.android.build.gradle.internal.tasks.Workers$ActionFacade 时发生失败
      > Android 资源编译失败
        C:\Users\Psz\Documents\Android-ChatRoom-Client\app\build\generated\res\pngs\debug\drawable-mdpi\ic_greensky_background.png: AAPT: error: 无法编译文件。
            
   > 更多失败...

* 尝试:
使用 --stacktrace 选项运行以获取堆栈跟踪。使用 --info 或 --debug 选项运行以获取更多日志输出。使用 --scan 运行以获取完整洞察。
* 在 https://help.gradle.org 获取更多帮助。

1 秒钟内构建失败
已执行的 10 个任务:2 个已执行,8 个最新状态

build.gradle(:app) 文件:

应用插件: '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'
}

我不知道是什么原因导致这个问题。我清理了缓存,删除并重新拉取了项目。而且我在网上找不到解决方案。

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

英文:

I was working on Ubuntu linux and recently i pulled my project and started working on Win10.

From now on i cannot Build/Run my app. There must be a problem with those PNG files in the resources folder.

That is the Error output while trying to build the app :

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) file:

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'
}

I don't know what is causing it. I cleaned cache, delete and pull project again. Also i couldn't find a solution online.

Update
Found out that Android Studio on Linux(Ubuntu) works just fine and as a matter of fact this error occurs only on Windows' Android Studio..

答案1

得分: 11

遇到了类似的问题,根本原因是一个 JPEG 文件被命名为 PNG 文件扩展名。一旦我修正了文件扩展名,AAPT 就恢复正常了。

英文:

I encountered a similar problem to this, and the root cause was that a JPEG file was named with a PNG file extension. Once I fixed the file extension AAPT was happy.

答案2

得分: 3

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

英文:

If some files have been encrypted, AAPT may not work properly. Please check whether the files related to the compilation error are encrypted. In your case, these are PNG files.

答案3

得分: 2

在我的情况下,我尝试使用了一些从WhatsApp下载的PNG图像,它们被加密了。

要解决此问题,请按照以下步骤操作:

  1. 打开画图程序
  2. 打开您的图像
  3. 保存它们

然后您就可以使用它们了!

英文:

im my case i tried to use some png images that i downloaded from whatsapp and they were encrypted.

to fix it follow this steps:

1-open paint
2-open your images
3-save them

and you are ready to go!

答案4

得分: 2

如果这是一个 Expo 应用,请删除您刚刚添加的所有 PNG 资源。您可以将它们重命名为 JPG,那样就可以正常使用。

英文:

If it's an expo app remove whatever png assets you just added. You can rename them as jpg and that would work

答案5

得分: 1

我遇到了完全相同的错误。尝试了包括拔自己的头发在内的一切,但都没有用。然后有人提到以"管理员"身份运行Android Studio会解决问题...所以这与"写入权限"有关。 Bingo!尝试这个:

在Windows中,进入勒索软件保护并将其禁用!

然后再次尝试构建。如果这有效,请使用"通过受控文件夹访问允许应用程序"来赋予Gradle正确的访问权限。然后再次启用您的勒索软件保护。

英文:

I had exactly the same error. Tried everything including pulling my own hear out, but nothing worked. Then someone mentioned running Android Studio as admin would solve the problem... So it has something to do with write access problems. Bingo! Try this:

In windows go to Ransomware protection and disable it!

Then try building again. If that works then use Allow an app through Controlled folder access to give gradle the right access rights. And then enable your Ransomware protection again.

答案6

得分: 0

在Windows中,尝试将您的项目保存在与Windows系统分开的驱动器上(不要使用C驱动器)。您可以使用D驱动器,并尝试保持路径简短(例如D:\Projects\XyzProject)。这可能会解决您的问题。

英文:

In Windows, try to keep your project in a drive apart from windows (C drive). You can use D drive and try keeping the path short (eg. D:\Projects\XyzProject). This might resolve your issue.

答案7

得分: 0

我遇到了同样的问题,并解决了。这个错误是由于Windows安全性引起的,选择引起问题的文件属性并解除其锁定。

英文:

i face same problem ,and solved .this error is due to windows security , select the file properties which causing issue and unblock it.

huangapple
  • 本文由 发表于 2020年9月22日 02:04:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63997710.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定