Ad

How To Fix In Android The Error "Could Not Find Com.android.tools.build:gradle:3.4.1"?

I have updated the version of Android Studio at the 3.4.1.
Immediately the version of Android Gradle Plugin Version and Gradle Version weren't the last so I have changed with 3.4.1 and 5.1.1 (File > Project structure > Project).
Now, I'm trying to create an apk but I have the error "Could not find com.android.tools.build:gradle:3.4.1".
The project is created with cordova and first I have updated cordova at the version 9.0.0.
I also have noticed that I don't have Build > Generated Signed Apk but only Build > Build Bundle.

After a long search, I have tried to:
1. change task wrapper with

wrapper {gradleVersion = '2.14.1'}
  1. add google() in

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

  2. Verify that the link "distributionUrl=https://services.gradle.org/distributions/gradle-5.1.1-all.zip" is correct

  3. Verify that there is the function mavenCentral()

  4. In File > Settings > Build, Execution... > Build Tools > Gradle "Use default gradle wrapper" is checked and "Offline work" not checked

build.gradle

apply plugin: 'com.android.application'

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }

    // Switch the Android Gradle plugin version requirement depending on the
    // installed version of Gradle. This dependency is documented at
    // http://tools.android.com/tech-docs/new-build-system/version-compatibility
    // and https://issues.apache.org/jira/browse/CB-8143
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
    }
}

// Allow plugins to declare Maven dependencies via build-extras.gradle.
allprojects {
    repositories {
        mavenCentral();
        jcenter()
    }
}

task wrapper(type: Wrapper) {
    gradleVersion = '2.14.1'
    }

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle- 
   5.1.1-all.zip

enter image description here

enter image description here What can I do to fix the problem? And what can I do to have the function Generated Signed Apk?

Ad

Answer

You have to add the google() maven repo in the buildscript block

buildscript {
    repositories {
        google()  // <-- add this
        mavenCentral()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.1'
    }
}

It is not related to your issue but for the other dependencies you should add the same repo also in the allprojects block

allprojects {
    repositories {
        google()// <-- add this. 
        jcenter()
    }
}

Pay attention to wrapper task with gradle 5.x.

Defining a custom wrapper or init task is deprecated with gradle 4.8 and removed in gradle 5.x. Attempting to replace a built-in task will produce an error similar to the following:

Cannot add task 'wrapper' as a task with that name already exists.

To avoid this issue, instead of this:

task wrapper(type:Wrapper) {
    //configuration
}

Do this:

wrapper {
    //configuration
}
Ad
source: stackoverflow.com
Ad