Cannot finish Capacitor 4 migration due to build error

I’m trying to migrate our Capacitor version from 2 to 4. After making the necessary upgrades and updates listed on the docs, I’m across the following error. Any ideas?

➜  frontend git:(fix/lean/get-mobile-working) ✗ npx cap run android
✔ Copying web assets from build to android/app/src/main/assets/public in 140.69ms
✔ Creating capacitor.config.json in android/app/src/main/assets in 1.73ms
[info] Found 3 Cordova plugins for android:
       @ionic-enterprise/badge@0.8.10
       cordova-plugin-ionic@5.4.6
       cordova-plugin-whitelist@1.3.4
✔ copy android in 258.43ms
✔ Updating Android plugins in 11.20ms
[info] Found 6 Capacitor plugins for android:
       @capacitor/device@4.0.1
       @capacitor/haptics@4.0.1
       @capacitor/keyboard@4.0.1
       @capacitor/network@4.0.1
       @capacitor/push-notifications@4.0.1
       @capacitor/status-bar@4.0.1
[info] Found 3 Cordova plugins for android:
       @ionic-enterprise/badge@0.8.10
       cordova-plugin-ionic@5.4.6
       cordova-plugin-whitelist@1.3.4
✔ update android in 36.69ms
✔ Please choose a target device: › Pixel 3 API 31 (emulator) (Pixel_3_API_31)
✖ Running Gradle build - failed!
[error] 
        FAILURE: Build failed with an exception.
        
        * Where:
        Script '/work/frontend/android/variables.gradle' line: 1
        
        * What went wrong:
        A problem occurred evaluating script.
        > No signature of method: variables_2korpwrcaee01m95nsn5cvof.ext() is applicable for argument types:
        (variables_2korpwrcaee01m95nsn5cvof$_run_closure1) values:
        [variables_2korpwrcaee01m95nsn5cvof$_run_closure1@6c174daa]
        Possible solutions: exec(groovy.lang.Closure), exec(org.gradle.api.Action), wait(), run(), run(), any()
        
        * 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
        
        Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.
        
        You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from
        your own scripts or plugins.
        
        See https://docs.gradle.org/7.4.2/userguide/command_line_interface.html#sec:command_line_warnings
        
        BUILD FAILED in 407ms
        
➜  frontend git:(fix/lean/get-mobile-working) ✗ 
ext {
    minSdkVersion = 22
    compileSdkVersion = 32
    targetSdkVersion = 32
    androidxAppCompatVersion = '1.4.2'
    androidxCoreVersion = 1.8.0
    androidxMaterialVersion =  '1.1.0-rc02'
    androidxBrowserVersion =  '1.2.0'
    androidxLocalbroadcastmanagerVersion =  '1.0.0'
    firebaseMessagingVersion =  '20.1.2'
    playServicesLocationVersion =  '17.0.0'
    junitVersion = 4.13.2
    androidxJunitVersion = 1.1.3
    androidxEspressoCoreVersion = 3.4.0
    cordovaAndroidVersion = 10.1.1
    androidxActivityVersion = '1.4.0'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxFragmentVersion = '1.4.1'
    coreSplashScreenVersion = '1.0.0-rc01'
    androidxWebkitVersion = '1.4.0'
}
1 Like

Have you tried to open and build from Android Studio? You might get some additional information about what’s going on from there.

If I had to wager a guess, it’s likely that gradle isn’t updated to the version it needs to be. I’d recommend checking the two gradle steps listed here in the V4 upgrade docs just to make sure that’s all correct.

I’ve tried opening it and building it from Android Studio as well.

My build.gradle shows the following which I believe puts the google services to the right version.

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.2.1'
        classpath 'com.google.gms:google-services:4.3.13'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

apply from: "variables.gradle"

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

I believe my gradle version is also compatible:

As for the error on Android Studio, this is the only one I’m getting:

Caused by: groovy.lang.MissingMethodException: No signature of method: variables_2korpwrcaee01m95nsn5cvof.ext() is applicable for argument types: (variables_2korpwrcaee01m95nsn5cvof$_run_closure1) values: [variables_2korpwrcaee01m95nsn5cvof$_run_closure1@e3be987]

There was a bug on the migrate command that has been fixed but not released yet that causes the variables.gradle to be broken when there were two white spaces before the variable version, and Capacitor 2 apps had those white spaces in a few variables.
A new release would be published soon with the fix, running migrate again from the new version should fix the issue.

If you can’t wait, you can manually update the variables.gradle file and turn all the integers into strings by wrapping the versions between single quotes (''), except for minSdkVersion, compileSdkVersion and targetSdkVersion as those should be numbers.

3 Likes

I am having the same issue though my variables.gradle I believe is correct as follows:

ext {
    minSdkVersion = 22
    compileSdkVersion = 30
    targetSdkVersion = 30
    androidxActivityVersion = '1.4.0'
    androidxAppCompatVersion = '1.4.2'
    androidxCoordinatorLayoutVersion = '1.2.0'
    androidxCoreVersion = '1.8.0'
    androidxFragmentVersion = '1.4.1'
    coreSplashScreenVersion = '1.0.0-rc01'
    androidxWebkitVersion = '1.4.0'
    junitVersion = '4.13.2'
    androidxJunitVersion = '1.1.3'
    androidxEspressoCoreVersion = '3.4.0'
    cordovaAndroidVersion = '10.1.1'
}

Getting the same error

Caused by: groovy.lang.MissingMethodException: No signature of method: build_66ciegw2jwczi5uqnnem0k4ze.android() is applicable for argument types: (build_66ciegw2jwczi5uqnnem0k4ze$_run_closure2) values: [build_66ciegw2jwczi5uqnnem0k4ze$_run_closure2@19b418d4]

Any help would be greatly appreciated?

Hmm, the only difference I see between my variables.gradle file and yours is that my compileSdkVersion and targetSdkVersion are both 32 instead of 30.

Otherwise, I just ended up scrapping the android and ios folders and then upgrade capacitor to the latest version.

If you are coming from an older version, make sure as well that you’ve installed the latest versions of the capacitor plugins you’re using. This ended up being a crucial fix for the errors I was getting.