Home > Software design >  Execution failed for task ':app:compileDebugJavaWithJavac' when building mobile app with C
Execution failed for task ':app:compileDebugJavaWithJavac' when building mobile app with C

Time:01-14

I am trying to build an app using Cordova (with cordova build android), unfortunately I ran into this error "Execution failed for task ':app:compileDebugJavaWithJavac'". I am providing all needed information that I can think of.

  • Cordova version: 11.0.0

  • Cordova Installed platforms: android 9.1.0, ios 5.1.1

  • Java version: 1.8.0_341

  • Npm version: 6.14.15

  • Node version: 14.18.1

  • JAVA_HOME path: /Library/Java/JavaVirtualMachines/jdk1.8.0_341.jdk/Contents/Home

  • Android SDK Platforms: from API level 29 up to 32

  • Android SDK Tools: from API level 29 up to 32

Working machine : MacBook Pro with M1 cpu, macOS: Ventura 13.1

My config.xml file:

<?xml version='1.0' encoding='utf-8'?>
<widget
        android-packageName="com.test.testapp"
        ios-CFBundleIdentifier="com.test.testapp"
        version="1.0.0"
        xmlns="http://www.w3.org/ns/widgets"
        xmlns:cdv="http://cordova.apache.org/ns/1.0"
        xmlns:android="http://schemas.android.com/apk/res/android">
    <name>Test App</name>
    <description>
        Just a Test App.
    </description>
    <author email="[email protected]" href="http://test.com">
        Test Team
    </author>
    <content src="sourcePath"/>
    <plugin name="cordova-plugin-whitelist" spec="1"/>
    <access origin="*"/>
    <allow-intent href="http://*/*"/>
    <allow-intent href="https://*/*"/>
    <allow-intent href="tel:*"/>
    <allow-intent href="sms:*"/>
    <allow-intent href="mailto:*"/>
    <allow-intent href="geo:*"/>
    <allow-navigation href="https://*/*" />
    <preference name="Orientation" value="portrait"/>
    <platform name="android">
        <icon src="../assets/android/icon.png"/>
        <allow-intent href="market:*"/>
        <preference name="android-targetSdkVersion" value="31"/>
        <preference name="AndroidXEnabled" value="true" />
        <config-file parent="/manifest/application" target="AndroidManifest.xml">
            <meta-data
                android:name="com.google.android.gms.wallet.api.enabled"
                android:value="true" />
        </config-file>
        <edit-config file="AndroidManifest.xml" target="/manifest/application/activity" mode="merge">
            <activity android:exported="true"/>
        </edit-config>
    </platform>
    <platform name="ios">
        <feature name="CDVWKWebViewEngine">
            <param name="ios-package" value="CDVWKWebViewEngine"/>
        </feature>
        <allow-navigation href="*://js.stripe.com/*"/>
        <allow-navigation href="*://stripe.com/*"/>
        <allow-intent href="itms:*"/>
        <allow-intent href="itms-apps:*"/>
        <edit-config target="NSLocationWhenInUseUsageDescription" file="*-Info.plist" mode="merge">
            <string>Needs for locating user's order delivery address</string>
        </edit-config>
        <edit-config target="NSLocationAlwaysUsageDescription" file="*-Info.plist" mode="merge">
            <string>Needs for locating user's order delivery address</string>
        </edit-config>
        <preference name="CordovaWebViewEngine" value="CDVWKWebViewEngine"/>
        <preference name="StatusBarOverlaysWebView" value="false"/>
        <preference name="StatusBarBackgroundColor" value="000000"/>
        <preference name="WKWebViewOnly" value="true"/>
    </platform>
    <preference name="SplashScreenDelay" value="0"/>
    <engine name="android" spec="<9.0.0"/>
    <engine name="ios" spec="~5.1.1"/>
</widget>

My package.json file:

{
  "name": "helloworld",
  "displayName": "HelloCordova",
  "version": "1.0.0",
  "description": "A sample Apache Cordova application that responds to the deviceready event.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "ecosystem:cordova"
  ],
  "author": "Apache Cordova Team",
  "license": "Apache-2.0",
  "dependencies": {
    "cordova-android": "^9.0.0",
    "cordova-ios": "^5.1.1",
    "cordova-plugin-device": "^2.0.3",
    "cordova-plugin-geolocation": "^4.0.1",
    "cordova-plugin-inappbrowser": "^3.2.0",
    "cordova-plugin-network-information": "^2.0.1",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.2",
    "cordova-plugin-whitelist": "1.3.4",
    "cordova-plugin-wkwebview-engine": "^1.2.1",
    "cordova-plugin-x-toast": "^2.7.3",
    "es6-promise-plugin": "^4.2.2",
    "xml-js": "^1.6.11"
  },
  "devDependencies": {},
  "cordova": {
    "plugins": {
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-whitelist": {},
      "cordova-plugin-wkwebview-engine": {},
      "cordova-plugin-x-toast": {},
      "cordova-plugin-geolocation": {},
      "cordova-plugin-network-information": {},
      "cordova-plugin-inappbrowser": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {}
    },
    "platforms": [
      "android",
      "ios"
    ]
  }
}

My generate_builds.sh file:


### Constants

# Must be equal to <widget> -> <name> in app/config.xml file
APP_NAME="Test App"

### Operations

# navigate to app/ directory
cd app/

# delete all generated folders
rm -rf platforms/ node_modules/

# run npm install to make sure that new libraries are installed
npm install

# this command will add all plugins listend in package.json file in /plugins folder and also generate ios and android platforms
cordova prepare

# Add Plugman plugins - we use Plugman to install some plugins only for android platform and some only for iOS platform. In this way we avoid plugin conflicts.
# IMPORTANT: Plugins installed with Plugman are not listed in the package.json neither in config.xml files

# for ios platform
plugman install --platform ios --project ./platforms/ios/ --plugin cordova-plugin-firebasex --plugins_dir ./plugins/
plugman install --platform ios --project ./platforms/ios/ --plugin cordova-plugin-stripe --plugins_dir ./plugins/
plugman install --platform ios --project ./platforms/ios/ --plugin cordova-plugin-dynamic-applepay-stripe --plugins_dir ./plugins/

# for android platform
plugman install --platform android --project ./platforms/android/ --plugin cordova-plugin-firebasex --plugins_dir ./plugins/
plugman install --platform android --project ./platforms/android/ --cordova-plugin-androidx --plugins_dir ./plugins/
plugman install --platform android --project ./platforms/android/ --cordova-android-play-services-gradle-release --plugins_dir ./plugins/
plugman install --platform android --project ./platforms/android/ --plugin cordova-plugin-apple-pay-google-pay --plugins_dir ./plugins/
# Replace all iOS image folder with the one from /app/assets/ios/Images.xcassets

# delete generated folders
rm -rf platforms/ios/"$APP_NAME"/Images.xcassets/
cp -r ../assets/ios/Images.xcassets/ platforms/ios/"$APP_NAME"/Images.xcassets/


# Add firebase push notification files in ios and android platforms


# ios

rm -f platforms/ios/"$APP_NAME"/Resources/GoogleService-Info.plist
cp  ../assets/ios/firebase/GoogleService-Info.plist platforms/ios/"$APP_NAME"/Resources/GoogleService-Info.plist

# android

rm -f platforms/android/app/google-services.json
cp  ../assets/android/firebase/google-services.json platforms/android/app/google-services.json


# Replace Google Pay plugin files in android platform.

rm -f platforms/android/app/src/main/java/com/plugin/googlepay/ApplePayGooglePay.java
cp  ../assets/android/gpay/ApplePayGooglePay.java platforms/android/app/src/main/java/com/plugin/googlepay/ApplePayGooglePay.java

More details about the error:

> Task :app:compileDebugJavaWithJavac
/Users/userName/Documents/Projects/TestApp/app/platforms/android/app/src/main/java/org/apache/cordova/firebase/FirebasePluginMessagingService.java:261: error: cannot find symbol
            if(getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                                                                           ^
  symbol:   variable S
  location: class VERSION_CODES
/Users/userName/Documents/Projects/TestApp/app/platforms/android/app/src/main/java/org/apache/cordova/firebase/FirebasePluginMessagingService.java:261: error: cannot find symbol
            if(getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.S && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
                                                                                                                             ^
  symbol:   variable S
  location: class VERSION_CODES
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: /Users/userName/Documents/Projects/TestApp/app/platforms/android/app/src/main/java/org/apache/cordova/firebase/FirebasePlugin.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
2 errors

> Task :app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

I have tried many possible solutions found here in StackOverflow, so far nothing helped for my issue.

CodePudding user response:

The error clearly comes from FirebasePluginMessagingService. But I don't see where it is added. So I guess it is added by plugman, since you log Plugins installed with Plugman are not listed in the package.json neither in config.xml files Can you try to remove that plugin and see if your project builds. The error arises, because S doesn't exist in you older targetSDK (S is 31 I believe) source: Build failed error: cannot find symbol if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R). So see if you can use an older version of the plugin or a higher version of targetSDK. For this you probably also have to increment cordova-android.

I think it is important to get a working project as a starting point, which is why I would remove the plugin first. Then upgrade the required versions and only then add back the plugin. I updated cordova android from 9 to 11 in several steps and also the targetSDK from 29 to 31 in 2 steps. In each steps at least one thing broke, so trying to do several of these at once is pretty much undoable. Some things that broke were:

  • Jdk needs to be upgraded from 8 to at least 11 (I think this was required by targetSDK 30)
  • WhiteList plugin and every reference to it needs to be removed (also in plugins, by patching them) (required by cordova 10 )
  • Node needs to be at least 14
  • this spec: <engine name="android" spec="<9.0.0"/> seems to conflict with the actual cordova-android version you're using. I am not exactly sure which updates require what. But the cordova update docs should tell you, they exist for every version: https://cordova.apache.org/announcements/2021/07/20/cordova-android-10.0.0.html targetSDK versions themselves also have several requirements. So be sure to check those as well.
  • Related