How to fix 'Android Gradle plugin 4.0.1 must not be applied to project since version 4.0.1 was already applied to this project'?

I am a beginner in Ionic+Angular. I am having an issue with Gradle Sync. Android Studio says ‘Android Gradle plugin 4.0.1 must not be applied to project ‘/Users/zerobstacle/Desktop/capacitor-test/water-meter-reading/android/capacitor-cordova-android-plugins’ since version 4.0.1 was already applied to this project’. I think this problem started when I tried using the Firebase Vision plugin from Cordova.

Here is my build.gradle:

ext {
    cordovaAndroidVersion =  project.hasProperty('cordovaAndroidVersion') ? rootProject.ext.cordovaAndroidVersion : '7.0.0'
}

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
    defaultConfig {
        minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
        targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
        versionCode 1
        versionName "1.0"
    }
    lintOptions {
        abortOnError false
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    google()
    jcenter()
    mavenCentral()
    flatDir{
        dirs 'src/main/libs', 'libs'
    }
}

dependencies {
    implementation fileTree(dir: 'src/main/libs', include: ['*.jar'])
    implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
    // SUB-PROJECT DEPENDENCIES START
    implementation "com.google.firebase:firebase-ml-vision:24.+"
    implementation "com.google.firebase:firebase-ml-vision-barcode-model:16.+"
    // SUB-PROJECT DEPENDENCIES END
}

// PLUGIN GRADLE EXTENSIONS START
apply from: "cordova.variables.gradle"
apply from: "../../node_modules/cordova-plugin-firebase-mlvision/src/android/build.gradle"
// PLUGIN GRADLE EXTENSIONS END

for (def func : cdvPluginPostBuildExtras) {
    func()
}

Your help will be so much appreciated. Thank you!

This line strikes me as odd. I just opened a random android project of an Ionic app I have lying around and the app’s build.gradle contains instead:

apply plugin: 'com.android.application'

So I would suggest asking whoever put that library line in there why they did that.

Hello sir raprospos, thanks for responding. I have already tried replacing apply plugin: 'com.android.library' with apply plugin: 'com.android.application', and the gradle sync finished, but here’s the error message this time whenever I try to run it: Error running ‘capacitor-cordova-plugins’: Default Activity not found

I wasn’t suggesting blindly replacing the line, I was suggesting investigating why it was there in the first place.

If you didn’t put it there, and don’t know who did, spawn a new project and see if it’s there. If not, add each dependency of your app one by one and see what happens. At some point, you will know what dependency is responsible, or you will have a working project.

1 Like

Okay sir, the Firebase Vision plugin caused the problem. I have already run a Camera app (through Capacitor’s Camera plugin) before through Android Studio emulator but I didn’t get any problem, but when I tried this Firebase Vision plugin, the error Android Gradle plugin 4.0.1 must not be applied…```, that is when this problem happened. :frowning: