Error appeared when apply plugin: 'com.huawei.agconnect' in Flutter

1,042

As par with this github issue here, the error is most likely caused by the Android SDK version you use. The Huawei Push Kit for Flutter suggests that you use Android SDK 10.0 (API level 29) or 9.0 (API level 28) versions. You can download these versions from Android Studio by navigating to the File > Settings > Android SDK > SDK Platforms menu.

For the build.gradle, you can compare your gradle files with the ones provided in the Huawei Flutter Push Kit - Demo and make the necessary changes. I am leaving the build.gradle files for the huawei_push: 5.0.2+301 here, for the convenience.

Note: If these operations don't solve the error, please submit an issue to the github repository with the flutter doctor output, your gradle files and a minimum code sample to reproduce the error.

android/build.gradle

buildscript {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/' }
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.0'
        classpath 'com.huawei.agconnect:agcp:1.4.1.300'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url 'https://developer.huawei.com/repo/' }
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

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

android/app/build.gradle

def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

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'
}

apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
apply plugin: 'com.huawei.agconnect'

android {
    compileSdkVersion 29

    lintOptions {
        disable 'InvalidPackage'
    }

    defaultConfig {
        // Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        // The Application ID here should match with the Package Name on the AppGalleryConnect
        applicationId "<package_name>"
        minSdkVersion 17
        targetSdkVersion 29
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    signingConfigs {
        config {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        debug {
            signingConfig signingConfigs.config
        }
        release {
            signingConfig signingConfigs.config
            // Enables code shrinking, obfuscation and optimization for release builds
            minifyEnabled true
            // Unused resources will be removed, resources defined in the res/raw/keep.xml will be kept.
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

flutter {
    source '../..'
}
Share:
1,042
Mouris
Author by

Mouris

Updated on December 26, 2022

Comments

  • Mouris
    Mouris over 1 year

    I was trying to use Huawei SDK in flutter so I apply the plugin in the android/app/build.gradle file as shown in the instruction in https://developer.huawei.com/consumer/en/doc/development/HMS-Plugin-Guides/integrate-plugin-0000001050418527

    apply plugin: 'kotlin-android'
    apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
    **apply plugin: 'com.huawei.agconnect'**
    

    However, it return with an error :

    FAILURE: Build failed with an exception.
    
    * What went wrong:
    Could not determine the dependencies of task ':app_settings:compileDebugAidl'.
    > Failed to find Platform SDK with path: platforms;android-R
    

    I've seen some solution which stated need to add the following code in android/build.gradle file

    subprojects {
        project.evaluationDependsOn(':app')
    }
    
    subprojects {
        project.buildDir = "${rootProject.buildDir}/${project.name}"
    
        project.evaluationDependsOn(':app')
    
        afterEvaluate {project ->
            if (project.hasProperty("android") && project.property("android").compileSdkVersion.equals("android-R")) {
                android {
                    compileSdkVersion 30
                }
            }
        }
    }
    

    I've added it but it still return the same error. When I removed the apply plugin: 'com.huawei.agconnect', the error did not appeared. But in this case I would like to use the plugin because I am planning to use Huawei push kit.

    Kindly tell me if any additional information needed to look through this problem. Thank you.