ionic/cordova编译出现com.android.ide.common.process.ProcessException: Failed to execute aapt错误的解决

问题及描述

昨天在使用ionic运行Android的项目时候,出现下面的错误:

Execution failed for task ':processArmv7DebugResources'.
> com.android.ide.common.process.ProcessException: Failed to execute aapt
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:font
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontStyle
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontWeight
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex

原因及解决方法

根据Android文档使用Fonts相关的API在编译的时候至少需要Support Library26:

To use the Fonts in XML feature on devices running Android 4.1 (API level 16) and higher, use the Support Library 26. For more information on using the support library, refer to the Using the support library section.

而默认情况下Cordova生成的platforms/android/build.gradle文件中设定的编译版本是24,如下:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    // SUB-PROJECT DEPENDENCIES START
    debugCompile(project(path: "CordovaLib", configuration: "debug"))
    releaseCompile(project(path: "CordovaLib", configuration: "release"))
    compile "com.android.support:support-v4:24.1.1+"
    compile "com.android.support:support-v4:+"
    // SUB-PROJECT DEPENDENCIES END
}

解决的方法是,在platforms/android/目录下创建一个 build-extras.gradle 文件,内容如下:

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-v4:26+'
    }
}

上面的文件可以强制使用26及更高的版本。

最佳实践

Github上的有人制作了一个cordova的hooks文件,可以自动将build-extras.gradle文件复制到platform/android目录下面,操作如下:
1. 首先在项目根目录下创建 build-extras.gradle 文件,内容和上面相同;
2. 然后在 hooks/after_prepare 目录(如果目录不存在则需要新建)下创建 011_copy_build_extras.js 文件,内容如下:

#!/usr/bin/env node

//goes in hooks/after_prepare/

var fs = require('fs');
var rootdir = process.argv[2];
var android_dir = rootdir + '/platforms/android';
var gradle_file = rootdir + '/build-extras.gradle';
var dest_gradle_file = android_dir + '/build-extras.gradle';

if (fs.existsSync(android_dir) && fs.existsSync(gradle_file)) {
    console.log('Copy ' + gradle_file + ' to ' + android_dir);
    fs.createReadStream(gradle_file).pipe(fs.createWriteStream(dest_gradle_file));
} else {
    console.log(gradle_file + ' not found. Skipping');
}

上面对原始的文件做了一些小的调整。
3. Mac或Linux下需要设置执行的权限。
这样设置之后,每次在执行Android编译之前会自动执行上面的操作,非常方便。

个人觉得比CookieCookson制作的方案好好一些,如果没有Android平台不会执行。

坑及几点说明

com.android.support的版本

出现这个问题之后首先找到了下面的参考资料,也知道了出现问题的原因,但几乎所有的参考资料中的build.gradle文件都使用了 com.android.support:support-v4:27.1.0 这个版本,但在我电脑上依然报错,偶尔在下面的第一篇参考资料的最后发现有人提出使用v4:24.0.0,于是才恍然大悟,原来我Android SDK中没有安装v4:27.1.0版本的build-tools,将版本修改成了 v4:26+ 即只要在26及以上的版本都可以,而不是只限定某一个版本。

使用cordova-android-support-gradle-release插件无效

很多文章中提到使用一个 cordova-android-support-gradle-release 插件,但我测试下来依然是无效的,不知道是不是使用方法不对。

如果上面的方法无法解决你的问题

可以依次采用下面的方法:
1. 执行下面的命令依次删除、添加Android平台之后再执行上面的操作:

ionic platform rm android
ionic platform add android
  1. How to update cordova and ionic?将ionic和cordova升级到最新版本,然后重复上面的第1点操作:
  2. 如果使用了crosswalk插件的话,先将 cordova-plugin-crosswalk-webview 插件删除之后,再编译项目。如果编译成功则表示是crosswalk的兼容性问题,可以将插件升级到最新版本之后再试。
  3. 最后如果都没办法解决问题的话,建议仔细阅读下面的参考资料。

参考资料:

Problem Isolated - Need Solution. Failed Build: ':app:processArm64DebugResources' attr/ttcIndex attr/fontVariationSettings
Android build broken after gradle dependencies update: Execution failed for task ‘:processDebugResources’. > com.android.ide.common.process.ProcessException: Failed to execute aapt
Running “cordova build android” - unable to find attribute android:fontVariationSettings and android:ttcIndex
Error:In FontFamilyFont, unable to find attribute android:font after Glide 4.4 upgrade

One thought to “ionic/cordova编译出现com.android.ide.common.process.ProcessException: Failed to execute aapt错误的解决”

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注