104

When I run cordova build android --buildConfig xxxx --release, I get the following error:

ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:fontVariationSettings
ERROR: In <declare-styleable> FontFamilyFont, unable to find attribute android:ttcIndex

The strange thing is I use two macOS machines for the compilation, and I get this error only on one of them for the same code.

Here is the output of ./gradlew cdvPrintProps I get on the two machines:

:cdvPrintProps
cdvCompileSdkVersion=26
cdvBuildToolsVersion=27.0.3
cdvVersionCode=null
cdvMinSdkVersion=21
cdvBuildMultipleApks=true
cdvReleaseSigningPropertiesFile=release-signing.properties
cdvDebugSigningPropertiesFile=null
cdvBuildArch=null
computedVersionCode=152045989
computedArmv7VersionCode=1520459892
computedX86VersionCode=1520459894

Below is the list of plugins used:

$ cordova plugins list
cordova-custom-config 5.0.2 "cordova-custom-config"
cordova-fabric-plugin 1.1.10 "cordova-fabric-plugin"
cordova-open-native-settings 1.5.0 "Native settings"
cordova-plugin-app-event 1.2.1 "Application Events"
cordova-plugin-app-version 0.1.9 "AppVersion"
cordova-plugin-camera 2.4.1 "Camera"
cordova-plugin-compat 1.2.0 "Compat"
cordova-plugin-console 1.1.0 "Console"
cordova-plugin-crosswalk-webview 2.4.0 "Crosswalk WebView Engine"
cordova-plugin-datepicker 0.9.2 "DatePicker"
cordova-plugin-device 2.0.1 "Device"
cordova-plugin-email 1.2.7 "EmailComposer"
cordova-plugin-file 4.3.3 "File"
cordova-plugin-file-transfer 1.6.3 "File Transfer"
cordova-plugin-inappbrowser 1.7.2 "InAppBrowser"
cordova-plugin-network-information 1.3.4 "Network Information"
cordova-plugin-secure-storage 2.6.8 "SecureStorage"
cordova-plugin-splashscreen 4.1.0 "Splashscreen"
cordova-plugin-statusbar 2.4.1 "StatusBar"
cordova-plugin-whitelist 1.3.3 "Whitelist"
cordova.plugins.diagnostic 3.9.2 "Diagnostic"
de.appplant.cordova.plugin.local-notification 0.8.5 "LocalNotification"
ionic-plugin-keyboard 2.2.1 "Keyboard"

How can I fix this problem?

DTavaszi
  • 192
  • 1
  • 10
Stéphane Padovani
  • 1,157
  • 2
  • 9
  • 16

26 Answers26

106

Just put following in build-extras.gradle

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-v4:27.1.0'
    }
}
Mohammed H
  • 6,880
  • 16
  • 81
  • 127
helmson helmson
  • 1,100
  • 1
  • 7
  • 2
  • Thanks! This is a reasonable approach for Cordova to pin to a version. – Deathspike Mar 08 '18 at 14:11
  • 10
    I keep my platforms folder out of source control so in order to accomplish this, I created `build-extras.gradle` in my project root added code to copy it in `hooks\after_platform_add` https://gist.github.com/charlesbedrosian/8a33c866704ff51ab5700b272b7cf150 – cbedrosian Mar 08 '18 at 19:06
  • 9
    i don't have such file :" build-extras.gradle" – Joe Sleiman Mar 09 '18 at 06:39
  • @StéphanePadovani but i will be removed when i remove the platform android and then add it – Joe Sleiman Mar 09 '18 at 08:15
  • i have this error : Could not find com.android.support:support-v4:27.1.0. – Joe Sleiman Mar 09 '18 at 09:19
  • I am using phonegap-build, and can't change this. Is there a solution for phonegap build users that you are aware of? – catu Mar 09 '18 at 11:42
  • 3
    For other users of phonegap-build. The answer is cordova-android-support-gradle-release https://forums.adobe.com/thread/2462835 – catu Mar 09 '18 at 12:41
  • 7
    In addition to @StéphanePadovani's comment, note that it should be created in the /platforms/android/app folder from Cordova Android 7.0.0 – Will Kru Mar 10 '18 at 17:09
  • @JoeSleiman here is a link about build-extras.gradle: https://cordova.apache.org/docs/en/latest/guide/platforms/android/#extending-buildgradle in short, you have to create a script in hooks to copy your build-extras.gradle to /platform/android/ – Gilson Mar 10 '18 at 22:10
  • @JoeSleiman i have created a script to copy gradle-extras.build into /platform/andoid for another issue and i have posted here https://stackoverflow.com/a/49215694/3081007 you can use it. – Gilson Mar 11 '18 at 01:23
  • @user3081007 : i add build-extras.gradle in platform/android , and then try to build i got this error : Could not find com.android.support:support-v4:27.1.0. , any idea ? – Joe Sleiman Mar 12 '18 at 06:51
  • @JoeSleimani am not shure, but i think you should update somethings in your android sdk, those errors are usualy solved by android studio – Gilson Mar 12 '18 at 14:49
  • or you can add these lines to platforms/android/build.gradle – Zeeshan Anjum May 19 '18 at 18:52
  • when I add above mentioned solution in `build-extras.gradle` file under `platforms/android` it throws the following error **Invalid variable name. Must start with a letter but was: ?configurations**. So what is the correct way of defining `configurations` in `build-extras.gadle` file – Shashank HS Aug 08 '18 at 11:01
84

Google released the new version 28.0.0-alpha1 of com.android.support:support-v4 which is adding 2 new attributes(android:fontVariationSettings and android:ttcIndex). Some of the plugins are using the latest android-support libraries which results in unwanted incompatibilities.

Option 1: Install cordova-android-support-gradle-release plugin.

Well documented plugin which "aligns various versions of the Android Support libraries specified by other plugins to a specific version". Tested without any destructive behavior.

cordova plugin add cordova-android-support-gradle-release --fetch

Read the documentation for a full set of options: Readme

Option 2: Add next code snippet in build.gradle under platforms/android

/** 
IMPORTANT - Manually added
Problem: 8 March 2018 - Google released version support-v4:28.0.0-alpha1 
which breaks the project with following error: unable to find attribute 
android:fontVariationSettings and android:ttcIndex

Effect: Force a specific version of the library
*/

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

Warning: code in build.gradle will be overwritten if you remove/add the Android platform. If you don't want to use the plugin for some reason or somehow is not working for you, instead create a hook and overwrite the file every time. Check 2nd comment here.

If the problem is persistent you may try:

cordova platform rm android
cordova platform add android

OR

Make sure you don't have a previous version of the app installed on the device you test because you'll receive an ambiguous error when it tries to downgrade the existing version: "INSTALL_FAILED_VERSION_DOWNGRADE" and "UnhandledPromiseRejectionWarning: Unhandled promise rejection"

Andrew Radulescu
  • 1,862
  • 13
  • 21
  • 2
    Best answer. Installing the plugin is the best and easy solution to this. It automagically resolves the error once the plugin is added. Simple. No need to play around creating files in platform folder that needs to be tracked manually in the event of removing and adding platforms. – Neel Mar 14 '18 at 04:54
  • Option 1 worked for me. It is extremely frustrating that Cordova defines their dependencies in a non-strict manner. I don't know why they do it. Left my project in a working state weeks ago, come back to it to recompile a debug version and suddenly errors occur. – Norman Breau Mar 21 '18 at 17:44
  • Failed to fetch plugin cordova-android-support-gradle-release via registry – netshark1000 Mar 28 '18 at 06:23
  • @netshark1000 This issue is not related to the plugin but with your package.json or some project configuration. Take a look in the package.json and make sure the plugin is not already installed. If is existent just call ionic cordova prepare to reload it. If the problem is persistent remove platforms, the plugins folder and re-add the platforms. This should do the trick. – Andrew Radulescu Mar 28 '18 at 07:59
  • 1
    Perfect, #1 saved me – GBarroso Apr 28 '18 at 16:52
  • Definitely read the readme of cordova-android-support-gradle-release plugin. It works if you choose the correct --variable ANDROID_SUPPORT_VERSION=26.+ (this worked in my case). – plocks Jun 18 '18 at 14:46
  • @AndrewRadulescu I actually came across #3 while I was using a image picker. So what would I have to do to avoid this problem? Deleted the previous app or update the current app version? – Laney Williams Jul 20 '18 at 22:00
31

The same error is happening to me. Apparently, a new version of the com.android.support:support-v4 library was released, and the plugin I'm using defines com.android.support:support-v4:+ as dependency in plugin.xml. The + sign means that it will get the latest version (28.0.0), which seems seems to be incompatible with other plugins.

I was able to build a development version by changing all the plugin dependencies from com.android.support:support-v4:+ to com.android.support:support-v4:27.1.0. Also, I executed ionic cordova platform remove android and ionic cordova platform add android. Hope it helps, at least for development.

avmatte
  • 631
  • 2
  • 9
  • 15
  • Thank you very much, In my side, I changed one thing gradle files of the plugin inside the android platform directory. please checkout if it is not work from @avmatte solution. – Sandun Priyanka Mar 08 '18 at 11:19
  • not working for me, i do the same but when i re-add the android platform the version return to v4:24.1.1+ even when i removed it to 27.1.0 – Joe Sleiman Mar 09 '18 at 06:53
  • i have this error : Could not find com.android.support:support-v4:27.1.0. – Joe Sleiman Mar 09 '18 at 07:11
28

I have just fixed this issue by going to the platform/android folder and edited the project.properties) file and replaced com.android.support:support-v4:+ with com.android.support:support-v4:27.1.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ahmed
  • 1,666
  • 17
  • 23
21

If you really just need a quick fix on that issue to make your build run, you may try adding the following lines into your platforms/android/build.gradle file:

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

Anyhow, setting the version here is not a sustainable fix.

stu
  • 219
  • 1
  • 4
15

It's strange, but it works when I add the below lines with the same versions.

This is my related lines in the platforms/android/build.gradle file:

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:26.+"
  compile "com.android.support:appcompat-v7:26.+"
  // SUB-PROJECT DEPENDENCIES END
}

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

In my project, the problem was occurred because of the 'cordova-plugin-crosswalk-webview' plugin.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Muhammed Tanriverdi
  • 3,230
  • 1
  • 23
  • 24
  • Thanks this really helped with re-downloading the dependencies which were previously overwritten by the update. Although editing your build.gradle is discouraged i tried previous solutions to no avail – Nico Mar 08 '18 at 15:42
9

I found the solution on Ionic Forum, which was the only solution that worked for me:

Run:

ionic cordova platform rm android

Run:

ionic cordova platform add android@8.0.0

Run:

ionic cordova plugin add cordova-plugin-androidx

Run:

ionic cordova plugin add cordova-plugin-androidx-adapter

Make sure your gradle.properties has:

cdvMinSdkVersion=19

Make sure your build.gradle has:

project.ext { defaultBuildToolsVersion="28.0.3" //String 

defaultMinSdkVersion=19 //Integer - Minimum requirement is Android 4.4 

defaultTargetSdkVersion=28 //Integer - We ALWAYS target the latest by default 

defaultCompileSdkVersion=28 //Integer - We ALWAYS compile with the latest by default }

Make sure your config.xml has:

<preference name="android-minSdkVersion" value="19" />

from: https://forum.ionicframework.com/t/firebase-app-unable-to-compile-on-android-with-aapt-error/166564/7

which is also handled in the stackoverflow answer: https://stackoverflow.com/a/56656680/839691

alpere
  • 1,079
  • 17
  • 26
  • This is the only solution works on my old `Ionic 3` app. Thank you! – Sampath Aug 03 '19 at 16:04
  • using ionic 3: This helped me resolve this issue , but got another issue now * What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. – Prem Sanil Mar 06 '20 at 07:40
  • 1
    @PremSanil Not sure this is causing your problem but make sure you have downloaded build tools 28.0.3 in Android Studio's SDK Manager. – alpere Mar 06 '20 at 13:55
  • @alpere I had to update Node version to v10.17.0 and removed unused Plugins started working (Using Ionic 3 blue theme) – Prem Sanil Apr 24 '20 at 13:15
8

I have the same error but not in cordova build. A new version of the com.android.support:appcompat-v7 and dependencies.But the incompatible version is in the third package that dependent on com.android.support:appcompat-v7.So i can't fixed the third package with @avmatte's solution.

Use @Sai Teja's solution to find incompatible package:

gradlew -q dependencies app:dependencies --configuration debugAndroidTestCompileClasspath

Then fixed it with:

configurations.all {
    resolutionStrategy {
        force 'com.android.support:support-compat:{the_same_version}'
        force 'com.android.support:appcompat-v7:{the_same_version}'
        force 'com.android.support:support-core-utils:{the_same_version}'
        force 'com.android.support:support-core-ui:{the_same_version}'
        force 'com.android.support:support-fragment:{the_same_version}'
        force 'com.android.support:support-annotations:{the_same_version}'
        ...
    }
}

Above code force the dependencies version.

doit
  • 81
  • 1
  • 5
8

I was facing the same error. Did a complete research in the plugin-directory for com.android.support:support-v4:+ and replaced it with a static version code.

For me, com.android.support:support-v4:23.4.0 worked just fine. There was no need to remove and re-add the android platform then.

cpro90
  • 176
  • 10
8

Here's an easy way to fix it that will persist when the platform directory is rebuilt and there's no need to go through all your plugins to try and find a culprit. Create a file build-extras.gradle with these contents:

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

Then create the file after_platform_add/010_copy_build_extras.js with the following contents:

#!/usr/bin/env node

var fs = require('fs');

var rootdir = process.argv[2];
var android_dir = `${rootdir}/platforms/android`;
var gradle_filename = 'build-extras.gradle';
var gradle_file = `${rootdir}/${gradle_filename}`;
if (fs.existsSync(android_dir) && fs.existsSync(gradle_file)) {
  fs.createReadStream(gradle_file)
    .pipe(fs.createWriteStream(`${android_dir}/${gradle_filename}`));
}

Now recreate the android platform and it will use the pinned support library.

Brad Pitcher
  • 1,693
  • 17
  • 21
  • the sub directory hooks are deprecated use config.xml ` ` – Exlord Jun 18 '18 at 10:15
  • ok tested and your code seems to be invalid `process.argv[2]` is `build` for me! – Exlord Jun 18 '18 at 10:32
  • Sorry @Exlord I guess it will need some adjustment to work with the new style hook, but I am using this exact code with a sub directory hook presently and it's working great. – Brad Pitcher Jun 19 '18 at 05:26
5

Some of your libraries should be using

com.android.support:support-v4:+

Find which one that is, with

gradlew -q dependencies app:dependencies --configuration debugAndroidTestCompileClasspath

And add that library as a module if it is not using a specific version in it's latest update (Also raise an issue in that library! ;) )

Thanks to @avmatte!

EDIT: You can also request gradle to force the library version

ext {
    supportVersion {latest_version_here}
} // In root project.gradle

configurations.all {
    resolutionStrategy {
        force "com.android.support:support-v4:$supportVersion"
    }
}
Teja Konjeti
  • 343
  • 2
  • 15
5

Add the following lines to your platforms/android/build.gradle

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

if still having issue try running this command:

cordova plugin add cordova-android-support-gradle-release --fetch
karan bhatia
  • 155
  • 2
  • 8
4

I was having the same problem out of the blue yesterday. It started randomly, but from reading around, it looks like it is to do with an update as mentioned above by @cpro90. However, I tried and could not find where to make the necessary manual change.

Eventually I identified that the problem was being caused by my cordova-plugin-crosswalk-webview plugin. On GitHub, I found the issue on the plugin repro this morning, and it had over 520 views by lunch.

@UNUMObile suggested the following in the build.gradle file to force an earlier version globally:

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

This worked immediately for me and may help others with other plugins that also have had their dependency on 'com.android.support:support-4:<28. The new version 28 seems to be the issue.

I hope this helps someone move forward.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
4

Just some tips for Kotlin developer:

If you follow these answers here, you are sure you don't have support-v4 library in your project but you are still seeing this error, please take a look at the ktx library.

I just figure out that I am using the latest 1.0.0-alpha1 version for ktx library and this error show out; after I changed back to version 0.3, everything back to normal now.

Anthonyeef
  • 2,595
  • 1
  • 27
  • 25
  • Failed to resolve: androidx.fragment:fragment-ktx:0.3 , when I change to '1.0.0-alpha1' same problem happen . – Mahmood Ali May 19 '18 at 06:57
  • @MahmoodAli take a look at the other dependencies? I am just posting my situation, and Google may also have the other package make the same wrong. – Anthonyeef May 19 '18 at 08:56
  • @MahmoodAli thanks for the link, but I think most of the project start to use ktx lib before the androidx is announced in this year's Google IO. It's hard to avoid mixing ktx with the support library... maybe migration to androidx should be the final solution for this kind of exception. – Anthonyeef May 20 '18 at 11:38
3

In your build.gradle file add

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

And in your project.properties file change cordova.system.library.3 to cordova.system.library.3=com.android.support:support-v13:27.+.

creativecreatorormaybenot
  • 114,516
  • 58
  • 291
  • 402
aman
  • 75
  • 10
2

For Phonegap Build users, as @catu mentionned in a comment, you can try this plugin which purpose is to prevent build failures caused by including different versions of the support libraries.

user276648
  • 6,018
  • 6
  • 60
  • 86
1

Just fix this issue by putting following lines of code in platforms/android/app/build.gradle file, just after buildscript {} block:

configurations.all {
    resolutionStrategy {
            force 'com.android.support:support-v4:27.1.0'
    }
}
Kush
  • 11
  • 1
1

I had same problem after installing the cordova-plugin-file-opener2 plugin. It resoved after doing: Option 1: Install cordova-android-support-gradle-release plugin. cordova plugin add cordova-android-support-gradle-release --fetch

DineshS
  • 81
  • 1
  • 1
0

Another aproach with same solution is create a hook. It's persistent (after platform reinstall), you can commit it and it don't require re-adding platform.

%project%\scripts\android\android-support-version.js

#!/usr/bin/env node

var fs = require('fs');

function replace_strings_in_file(filename, replacementsObject) {
  if (fs.existsSync(filename)) {
    var data = fs.readFileSync(filename, 'utf8');
    Object.keys(replacementsObject).forEach(function (to_replace) {
      var replace_with = replacementsObject[to_replace];
      data = data.replace(to_replace, replace_with);
    });
    console.log(data);
    fs.writeFileSync(filename, data, 'utf8');
  } else {
    console.log('file not found');
  }
}

module.exports = function (context) {
  var rootdir = process.argv[2];
  if (rootdir) {
    replace_strings_in_file("platforms/android/project.properties", {'com.android.support:support-v4:+': 'com.android.support:support-v4:27.1.0'});
    console.log('com.android.support version fix');
  }
};

Init hook in config

%project%\config.xml

...
<platform name="android">
  <hook src="scripts/android/android-support-version.js" type="before_build" />
  ...

Install fs dependency to your project:

npm i fs --save-dev

Run build:

cordova build android

pavelety
  • 746
  • 6
  • 8
0

SUPER SIMPLE AND CORRECT WAY OF SOLVING IT!

Just update the sdk and add the latest 2 version of android... and restart the computer! Done...

Now we have time to talk about cars and sports...

Ari Waisberg
  • 1,186
  • 1
  • 12
  • 23
0

I had the same problem, and none of the given solutions worked for me. Installing the latest version of Android SDK Build-tools (27.0.3) resolved my problem.

Daniel W.
  • 555
  • 3
  • 8
0

Solution link

This is due to compat plugin. Remove that plugin if you have older version (less than 1.2.0) and set cordova-android@6.3.0

cordova plugin rm cordova-plugin-compat --force

cordova plugin add cordova-plugin-compat@1.2.0

cordova platform rm android

ionic cordova platform add android@6.3.0

Working at my case. Thanks :)

Pranay Kumar
  • 144
  • 5
0

updates to @Brad Pitcher's answer:

hooks/copy_build_extras.js

#!/usr/bin/env node

var fs = require('fs');

module.exports = function (context) {
  var rootdir         = context.opts.projectRoot;
  var android_dir     = `${rootdir}/platforms/android`;
  var gradle_filename = 'build-extras.gradle';
  var gradle_file     = `${rootdir}/${gradle_filename}`;
  if (fs.existsSync(android_dir) && fs.existsSync(gradle_file)) {
    fs.createReadStream(gradle_file)
      .pipe(fs.createWriteStream(`${android_dir}/${gradle_filename}`));
  }
};

config.xml

<platform name="android">
    <allow-intent href="market:*" />
    <hook src="hooks/copy_build_extras.js" type="before_build" />
</platform>

build-extras.gradle in the root dir

configurations.all {
  resolutionStrategy {
    force 'com.android.support:support-v4:27.1.0'
  }
}
Exlord
  • 5,009
  • 4
  • 31
  • 51
0

I was running into this issue with react native and it was caused by the following lines in my android/app/build.gradle:

implementation ("com.google.android.gms:play-services-base:+") {
  force = true;
}
implementation ("com.google.android.gms:play-services-maps:+") {
  force = true;
}

etc...

Apparently this + resolved to a new 17.0.0 version which broke my build.
Changing the + to 16.0.0 (or 16.0.1 for play-services-base) solved my issue

Uri Klar
  • 3,800
  • 3
  • 37
  • 75
0

This problem had been killing me for a week.

Ultimately I ended up on android@6.4.0

I altered android/project.properties by changing

#cordova.system.library.3=com.google.android.gms:play-services-analytics:+
cordova.system.library.3=com.google.android.gms:play-services-analytics:15+

That change finally got me out of the ttcIndex error message pit of darkness.

Then I got this error

com.android.builder.dexing.DexArchiveBuilderException: 
  com.android.builder.dexing.DexArchiveBuilderException

The error was apparently due to a Java version issue. I then altered android/build.gradle with the following change

compileOptions {
  #sourceCompatibility JavaVersion.VERSION_1_6
  #targetCompatibility JavaVersion.VERSION_1_6
  sourceCompatibility JavaVersion.VERSION_1_8
  targetCompatibility JavaVersion.VERSION_1_8
}

Also note I do have the cordova-android-support-gradle-release plugin installed, but have no idea if I need it.

riyaz-ali
  • 8,677
  • 2
  • 22
  • 35
-1

It's a duplicate entry in values.xml in a folder called support-compat-28.0.0-alpha1.aar.

You'll find this file on Windows at \users\YOURUSERID\.gradle\caches\transforms-1\files-1.1

Once inside that folder you have to go some levels deeper to values.xml.

In that file, search for an element <declare-styleable name="FontFamilyFont>.

In that element, remove the FOUR lines with android: in it.

After I made that change I can build again without errors.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131