简体   繁体   中英

How to automatically add the necessary dependencies in Android Studio?

I have a Kotlin Android Studio project I am making that integrates with Firebase. I added all the lines required listed in Firebase's instructions into build.gradle for both the Project level and the App level.

But this doesn't include any of the dependencies my project seems to need, I get an error when trying to build and these warnings:

Gradle 同步警告

The Declared Dependencies list for my app module only has the two Firebase dependencies, and not any of the dependencies as listed by the above warnings:

声明的依赖

I noticed there are some dependencies I assume I need in All Modules :

Is there a way I can add those into my app module folder? I am not sure if that's how it works so let me know.

If this is not the way, please let me know how I can add all these required dependencies quickly onto my Project (preferrably without manually searching each one's repository url and such) so that I can Sync and Build without any errors.

The Gradle Sync was just now successful, here's what happened:

  • During the editing of the build.gradle files, Google Firebase's instructions were to add this line of code to the Project level's build.gradle file:
    buildscript {
      repositories {
        // Check that you have the following line (if not, add it):
        google()  // Google's Maven repository
    
      }
      dependencies {
        ...
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    
      }
    }
    
    allprojects {
      ...
      repositories {
        // Check that you have the following line (if not, add it):
        google()  // Google's Maven repository
    
        ...
      }
    }
  • When Syncing the files through Gradle, an error occurred stating that settings.gradle was prefered to build.gradle , as it also contains repositories and dependencies. The fix was to remove the dependencyResolutionManagement code block in settings.gradle .

Here's what I did to fix all the Sync Warnings and Build errors:

  1. Added the same dependencies found in settings.gradle to build.gradle in the Project level:
        gradlePluginPortal()
        google()
        mavenCentral()
  1. It did not work yet, so then I noticed you also need to add it under the allprojects code block. It will then look something like this:
    buildscript {
      repositories {
        // Check that you have the following line (if not, add it):
        gradlePluginPortal()
        google()    // Google's Maven repository
        mavenCentral()  
    
      }
      dependencies {
        ...
        // Add this line
        classpath 'com.google.gms:google-services:4.3.10'
    
      }
    }
    
    allprojects {
      ...
      repositories {
        // Check that you have the following line (if not, add it):
        gradlePluginPortal()
        google()    // Google's Maven repository
        mavenCentral()  
    
        ...
      }
    }

And that was all. After that the Gradle Sync took a long time this time and completed successfully without any errors, and the Build worked as well and ran fine as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM