简体   繁体   中英

Gradle: classpath dependency not resolved from nested project

I have a multi-project build set-up with gradle that will contain multiple android apps and libraries.

Sample of the project structure:

root (Project Root)
| android
  | module1 (Android Application)
  | module2 (Android Library)
| ios (in the future)

I want to apply certain gradle plugins only to some subprojects. (Such as the android gradle plugin only to the android subproject) Therefore I added the classpath dependency in the :android -> build.gradle and the plugin declaration to the two android subproject:
:android:module1 -> build.gradle -> apply plugin: 'com.android.application'
and
:android:module2 -> build.gradle -> apply plugin: 'com.android.library' The problem is that gradle cannot found the android gradle plugin:

Error:(1, 1) A problem occurred evaluating project ':Shoppr:presentation'. Plugin with id 'com.android.application' not found.

Also it is not a version problem as in some other questions (Gradle Version 3.1; Android Gradle Plugin Version: 2.2.1) because when defining the classpath dependencies in :root -> build.gradle or :android:moduleX -> build.gradle all is working as expected.

:root -> build.gradle

allprojects {
  repositories {
    mavenCentral()
    jcenter()
  }
}

:android -> build.gradle

buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:2.2.1'    <-- Should only be applied for android project
  }
}

:android:module1 -> build.gradle

apply plugin: 'com.android.application' --> Plugin with id 'com.android.application' not found.

:android:module2 -> build.gradle

apply plugin: 'com.android.library'

I have seen such folder arrangements especially with google sample projects that include and android folder and also a web folder. What you can try is to import the project using the android folder as the project folder since you may not be using gradle to build Ios and web if you have the two more folders for you project.

So close the project and re-import it using the android folder as the project root, i believe this way, gradle should run fine. My project folder is also like this because i have both web and android project in a single repo but during build with android studio, i use the android folder as the project root for the android.

I had a somewhat similar problem. I went from having an android folder with 3 subtasks to dividing it into 3 folders with subtasks:

| android  

    | shared
        -- generates aar
    | device1
        -- generates 2 apks
    | device2
        -- generates 1 apk
    | gradle
    gradlew.bat
    gradlew

Both device1 and device2 want to use shared. So I removed settings.gradle and build.grade from the android folder. device1, device2, and shared have their own build.grade and settings.gradle. I build device1 and device2 separately. In order for them to include shared and build, I put symbolic links to shared, gradlew, gradlew.bat, and the gradle folder from the android folder into device1 and device2. And voila, it works! (I'm on an Ubuntu computer, but should be able to do the same on Windows and Mac.)

The disadvantage is that shared is built each time. In our case, that's not too bad, it's not very big.

[tag:multi-level projects]

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