flutterandroid-studio

Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl. [Flutter]


I keep getting an error while trying to run a flutter project on android studio, the project runs fine previously on same machine but now raises error after a fresh format of the pc and android studio with other requirement reinstallations.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':wakelock'.
> Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.
   > Namespace not specified. Please specify a namespace in the module's build.gradle file like so:

     android {
         namespace 'com.example.namespace'
     }

     If the package attribute is specified in the source AndroidManifest.xml, it can be migrated automatically to the namespace value in the build.gradle file using the AGP Upgrade Assistant; please refer to https://developer.android.com/studio/build/agp-upgrade-assistant for more information.

This is how the current android/app/build.gradle looks like already there's a namespace but it is still raising same error

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

/*def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}*/

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}


android {
    namespace "com.generalomosco.myapp"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.generalomosco.myapp"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 24
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    /*implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"*/
}

settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }()

    includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}

plugins {
    id "dev.flutter.flutter-plugin-loader" version "1.0.0"
    id "com.android.application" version "8.0.2" apply false
    id "org.jetbrains.kotlin.android" version "1.7.10" apply false
}

include ":app"

Solution

  • It looks like you updated AGP (android gradle plugin) version. It could happened when you reinstall android studio and leveraged the Upgrade Assistant. Anyway, your AGP now is 8+ version and you have two ways:

    1. Go to your app's android/app/build.gradle file (is also can be build.gradle.kts), find android block and modify it like this:
    android {
        namespace = "com.example.myapp" # this should be your namespace from AndroidManifest.xml
    
    ... leave rest unchanged...
    }
    
    1. Downgrade your android gradle plugin to 7.* version. Specific steps depends on your configuration, but generally you will got to android/build.gradle, find gradle plugin version and change it.

    Good luck!

    EDITED:

    It is possible that one of packages you're using are obsolete and not ready to be used with modern android-gradle-plugin versions (8+). In this particular case it is wakelock

    In this case, you either downgrade your AGP or use another package. Gladly in this case it is possible with wakelock_plus package