如何解决使用 react-native 构建 APK 时无法执行 aapt 的错误?

Posted

技术标签:

【中文标题】如何解决使用 react-native 构建 APK 时无法执行 aapt 的错误?【英文标题】:How to solve the error Failed to execute aapt when building an APK with react-native? 【发布时间】:2018-10-09 16:52:21 【问题描述】:

我已经构建了一个 react-native 应用程序。它在 android 模拟器上运行良好,现在我想生成一个 APK。我已按照文档执行此操作which can be seen here 我使用命令

./gradlew assembleRelease

要构建 apk,但在构建 APK 时我不断收到错误消息。我检查了有关该主题的各种堆栈溢出问题,包括this one 和this github issue。我已经包括了这一行

android.enableAapt2=false

应用级 build.gradle 文件如下所示

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**  * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets  * and bundleReleaseJsAndAssets).  * These basically call `react-native bundle` with the correct arguments during the Android build  * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the  * bundle directly from the development server. Below you can see all the possible configurations  * and their defaults. If you decide to add a configuration block, make sure to add it before the  * `apply from: "../../node_modules/react-native/react.gradle"` line.  *  * project.ext.react = [  *   // the name of the generated asset file containing your JS bundle  *   bundleAssetName: "index.android.bundle",  *  *   // the entry file for bundle generation  *   entryFile: "index.android.js",  *  *   // whether to bundle JS and assets in debug mode  *   bundleInDebug: false,  *  *   // whether to bundle JS and assets in release mode  *   bundleInRelease: true,  *  *   // whether to bundle JS and assets in another build variant (if configured).  *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
*   // The configuration property can be in the following formats  *   //         'bundleIn$productFlavor$buildType'  *   //         'bundleIn$buildType'  *   // bundleInFreeDebug: true,  *   // bundleInPaidRelease: true,  *   // bundleInBeta: true,  *  *   // whether to disable dev mode in custom build variants (by default only disabled in release)  *   // for example: to disable dev mode in the staging build type (if configured)  *   devDisabledInStaging: true,  * // The configuration property can be in the following formats  *   //  'devDisabledIn$productFlavor$buildType'  *   //         'devDisabledIn$buildType'  *  *   // the root of your project, i.e. where "package.json" lives  *   root: "../../",  *  *   // where to put the JS bundle asset in debug mode  *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",  *  *   // where to put the JS bundle asset in release mode  *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",  *  *   // where to put drawable resources / React Native assets, e.g. the ones you use via  * // require('./image.png')), in debug mode  *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",  *  *   // where to put drawable resources / React Native assets, e.g. the ones you use via  * // require('./image.png')), in release mode  *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",  *  *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means  *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to  *   // date; if you have any other folders that you want to ignore for performance reasons (gradle 
*   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/  *   // for example, you might want to remove it from here.  *   inputExcludes: ["android/**", "ios/**"],  * 
*   // override which node gets called and with what additional arguments  *   nodeExecutableAndArgs: ["node"],  *  *   // supply additional arguments to the packager  *   extraPackagerArgs: []  * ] 
*/

project.ext.react = [
    entryFile: "index.js" ]

apply from: "../../node_modules/react-native/react.gradle"

/**  * Set this to true to create two separate APKs instead of one:  *
- An APK that only works on ARM devices  *   - An APK that only works on x86 devices  * The advantage is the size of the APK is reduced by about 4MB.  * Upload all the APKs to the Play Store and people will download  * the correct one based on the CPU architecture of their device.  */ def enableSeparateBuildPerCPUArchitecture = false

/**  * Run Proguard to shrink the Java bytecode in release builds.  */ def enableProguardInReleaseBuilds = false

android 
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    defaultConfig 
        applicationId "com.project"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 2
        versionName "1.0"
        ndk 
            abiFilters "armeabi-v7a", "x86"
        
    
    splits 
        abi 
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        
    
    signingConfigs 
        release 
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) 
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            
        
    
    buildTypes 
        release 
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        
    
    // applicationVariants are e.g. debug, release
    applicationVariants.all  variant ->
        variant.outputs.each  output ->
        // For each separate APK per architecture, set a unique version code as described here:
        // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
        def versionCodes = ["armeabi-v7a": 1, "x86": 2]
        def abi = output.getFilter(OutputFile.ABI)
        if (abi != null)   // null for the universal-debug, universal-release variants
            output.versionCodeOverride =
                versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
        
    
    
    productFlavors 
     

dependencies 
    compile project(':react-native-vector-icons')
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.facebook.react:react-native:+'
    // From node_modules
    implementation project(':react-native-maps')
    implementation(project(':react-native-maps')) 
        exclude group: 'com.google.android.gms', module: 'play-services-base'
        exclude group: 'com.google.android.gms', module: 'play-services-maps'
    
    implementation 'com.google.android.gms:play-services-base:10.2.4'
    implementation 'com.google.android.gms:play-services-maps:10.2.4' 

// Run this once to be able to run the application with BUCK // puts all compile dependencies into folder libs for BUCK to use task copyDownloadableDepsToLibs(type: Copy) 
    from configurations.compile
    into 'libs' 

在我的全局 gradle.properties 文件中,我还在撰写本文时将 react-native 更新到了最新的 55.3,但我仍然收到类似这样的错误。

任务:app:processReleaseResources 执行 aapt 失败 com.android.ide.common.process.ProcessException:无法执行 aapt 在 com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:796) 在 com.android.build.gradle.tasks.ProcessAndroidResources.invokeAaptForSplit(ProcessAndroidResources.java:551) 在 com.android.build.gradle.tasks.ProcessAndroidResources.doFullTask​​Action(ProcessAndroidResources.java:285) 在 com.android.build.gradle.internal.tasks.IncrementalTask​​.taskAction(IncrementalTask​​.java:109) 在 org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73) 在 org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTask​​Action.doExecute(DefaultTaskClassInfoStore.java:173) 在 org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134) 在 org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:121) 在 org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter$1.run(ExecuteActionsTaskExecuter.java:122) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107) 在 org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:111) 在 org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:92) 在 org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:70) 在 org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:63) 在 org.gradle.api.internal.tasks.execution.ResolveTaskOutputCachingStateExecuter.execute(ResolveTaskOutputCachingStateExecuter.java:54) 在 org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) 在 org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88) 在 org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:52) 在 org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:52) 在 org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54) 在 org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) 在 org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34) 在 org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker$1.run(DefaultTaskGraphExecuter.java:248) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:336) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:328) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:197) 在 org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:107) 在 org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:241) 在 org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:230) 在 org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.processTask(DefaultTaskPlanExecutor.java:124) 在 org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.access$200(DefaultTaskPlanExecutor.java:80) 在 org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:105) 在 org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker$1.execute(DefaultTaskPlanExecutor.java:99) 在 org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.execute(DefaultTaskExecutionPlan.java:625) 在 org.gradle.execution.taskgraph.DefaultTaskExecutionPlan.executeWithTask(DefaultTaskExecutionPlan.java:580) 在 org.gradle.execution.taskgraph.DefaultTaskPlanExecutor$TaskExecutorWorker.run(DefaultTaskPlanExecutor.java:99) 在 org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63) 在 org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46) 在 org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55) 引起:java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: 使用参数执行进程/Users/danieltuttle/Library/Android/sdk/build-tools/26.0.2/aapt 时出错包 -f --no-crunch -I /Users/danieltuttle/Library/Android/sdk/platforms/android-23/android.jar -M /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app /build/intermediates/manifests/full/release/AndroidManifest.xml -S /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates/res/merged/release -m -J /Users /danieltuttle/project/code/frontend/traveler-mobile/android/app/build/generated/source/r/release -F /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates /res/release/resources-release.ap_ --custom-package com.project -0 apk --output-text-symbols /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates /symbols/release --no-version-vectors 在 com.google.common.util.concurrent.AbstractFuture.getDoneValue(AbstractFuture.java:503) 在 com.google.common.util.concurrent.AbstractFuture.get(AbstractFuture.java:482) 在 com.google.common.util.concurrent.AbstractFuture$TrustedFuture.get(AbstractFuture.java:79) 在 com.android.builder.core.AndroidBuilder.processResources(AndroidBuilder.java:794) ... 41 更多 引起:com.android.ide.common.process.ProcessException:执行进程时出错 /Users/danieltuttle/Library/Android/sdk/build-tools/26.0.2/aapt 参数 package -f --no-crunch -I /Users/danieltuttle/Library/Android/sdk/platforms/android-23/android.jar -M /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates/manifests/full /release/AndroidManifest.xml -S /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates/res/merged/release -m -J /Users/danieltuttle/project/code/frontend /traveler-mobile/android/app/build/generated/source/r/release -F /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates/res/release/resources-release .ap_ --custom-package com.project -0 apk --output-text-symbols /Users/danieltuttle/project/code/frontend/traveler-mobile/android/app/build/intermediates/symbols/release --no-版本向量 在 com.android.build.gradle.internal.process.GradleProcessResult.buildProcessException(GradleProcessResult.java:73) 在 com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:48) 在 com.android.builder.internal.aapt.AbstractProcessExecutionAapt$1.onSuccess(AbstractProcessExecutionAapt.java:78) 在 com.android.builder.internal.aapt.AbstractProcessExecutionAapt$1.onSuccess(AbstractProcessExecutionAapt.java:74) 在 com.google.common.util.concurrent.Futures$CallbackListener.run(Futures.java:1237) 在 com.google.common.util.concurrent.MoreExecutors$DirectExecutor.execute(MoreExecutors.java:399) 在 com.google.common.util.concurrent.AbstractFuture.executeListener(AbstractFuture.java:911) 在 com.google.common.util.concurrent.AbstractFuture.complete(AbstractFuture.java:822) 在 com.google.common.util.concurrent.AbstractFuture.set(AbstractFuture.java:664) 在 com.google.common.util.concurrent.SettableFuture.set(SettableFuture.java:48) 在 com.android.build.gradle.internal.process.GradleProcessExecutor$1.run(GradleProcessExecutor.java:58) 引起:org.gradle.process.internal.ExecException:进程'command'/Users/danieltuttle/Library/Android/sdk/build-tools/26.0.2/aapt''以非零退出值1结束 在 org.gradle.process.internal.DefaultExecHandle$ExecResultImpl.assertNormalExitValue(DefaultExecHandle.java:380) 在 com.android.build.gradle.internal.process.GradleProcessResult.assertNormalExitValue(GradleProcessResult.java:46) ... 9 更多

FAILURE:构建失败并出现异常。

出了什么问题: 任务 ':app:processReleaseResources' 执行失败。

执行 aapt 失败

解决此问题的正确方法是什么,以便我可以成功构建 APK?

【问题讨论】:

请发布更多信息的错误日志,使用更长的堆栈跟踪,也许问题隐藏在那里 @HedShafran 我添加了完整的错误,谢谢。 Mmm.. 也许是你的build tools 版本.. 你也可以发布你的应用级 build.gradle 文件吗? (确保build tools 版本与compile sdk 版本相同..) @HedShafran 添加了应用级 build.gradle 文件...谢谢。 读取堆栈跟踪,您的某个模块似乎正在使用更新版本的构建工具。尝试将您的 buildToolsVersion 版本升级到 26.0.2,并将 compileSdkVersion 版本升级到 26。也许您也必须升级您的 targetSdkVersion,但我相信您可以将其保留在 22... 【参考方案1】:

我和你有同样的问题。我将 gradle.properties 文件中的android.enableAapt2=false 更改为android.enableAapt2=true,这对我有用。我希望它也对你有用。

【讨论】:

遇到同样的错误Task :app:processReleaseResources FAILED Failed to execute aapt 不推荐使用android.enableAapt2=[true|false]。见:developer.android.com/studio/command-line/aapt2【参考方案2】:

您所要做的就是转到文件>设置并搜索即时运行。搜索后您将能够看到启用即时运行。就像在图片中一样取消选中。然后点击应用按钮。现在问题解决了。

【讨论】:

【参考方案3】:

根据 HedeH 分享的链接 删除您可能拥有的文件:

android/app/src/main/res/drawable-mdpi/ android/app/src/main/res/drawable-xhdpi/ android/app/src/main/res/drawable-xxhdpi/

这就像一个魅力!

【讨论】:

不要复制that的答案,一旦你获得足够的声望,你就可以点赞 是的,因为我无法发表评论让我回答。

以上是关于如何解决使用 react-native 构建 APK 时无法执行 aapt 的错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 DrawerLayoutAndroid 组件在 react-native 应用程序中构建抽屉?

如何使用 Xcode 12 Beta 4 构建 React-Native 项目?

React-native:如何从 Windows 构建 ios

部署由React-Native开发的android和IOS应用程序

React-native + Relay.js 的稳定构建

如何构建一个 react-native 应用程序,以便它在 ipad 上作为 ipad 应用程序运行?