android - 为什么 Gradle 没有接收到我的 Android 模块依赖项?

标签 android gradle android-gradle-plugin

所以我第一次尝试使用 Gradle 在 IntelliJ IDEA 中创建一个多模块项目。我花了一个小时阅读文档、各种教程和 SO 问题,但无法解决这个简单的案例。我的结构:

-Project
   |-vg
   |-vgcommon
vgcommon是一个 Android Gradle 模块,而 vg是一个 Android Gradle 应用程序(技术上也是一个模块)。
  • vg取决于 vgcommon
  • vgcommon构建
  • vg没有,因为 vgcommon 中的所有类是“未找到”。

  • 这是我的 gradle.build 文件和 setup.gradle。谁能看到我做错了什么?

    settings.gradle (根级别)
    include ':vgcommon', ':VG'
    

    vgcommon\build.gradle
    buildscript {
        repositories {
            mavenCentral()
            maven { url 'http://download.crashlytics.com/maven' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.10+'
            classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1+'
        }
    }
    apply plugin: 'android'
    apply plugin: 'crashlytics'
    
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
    }
    
    android {
        compileSdkVersion 19
        buildToolsVersion "19.1.0"
    
        defaultConfig {
            minSdkVersion 8
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
        productFlavors {
            defaultFlavor {
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:19.+'
        compile 'com.koushikdutta.ion:ion:1.2.4'
        compile 'com.crashlytics.android:crashlytics:1+'
    }
    

    vg\build.gradle
    buildscript {
        repositories {
            mavenCentral()
            maven { url 'http://download.crashlytics.com/maven' }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.10+'
            classpath 'com.crashlytics.tools.gradle:crashlytics-gradle:1+'
        }
    }
    apply plugin: 'android'
    apply plugin: 'crashlytics'
    
    repositories {
        mavenCentral()
        maven { url 'http://download.crashlytics.com/maven' }
    }
    
    android {
        compileSdkVersion 19
        buildToolsVersion '19.1.0'
    
        defaultConfig {
            minSdkVersion 8
            targetSdkVersion 19
        }
    
        buildTypes {
            release {
                runProguard false
                signingConfig signingConfigs.release
                debuggable false
            }
        }
        productFlavors {
            defaultFlavor {
            }
        }
        applicationVariants.all { variant ->
            variant.outputFile = file("../build/publish/app.apk")
        }
    }
    
    configurations {
        unitTestCompile.extendsFrom compile
        unitTestRuntime.extendsFrom unitTestCompile
    }
    
    dependencies {
        compile project(':vgcommon')
        compile 'com.android.support:appcompat-v7:19.0.1'
        compile files('libs/gcm.jar')
        compile files('libs/spring-appsensor-android-1.7.1.jar')
        compile files('libs/spring-util-android.jar')
        compile 'com.crashlytics.android:crashlytics:1.+'
        compile 'com.squareup:otto:1.3.4'
        compile 'com.google.code.gson:gson:2.2.4'
        compile 'com.koushikdutta.ion:ion:1.2.4'
        compile 'com.mobsandgeeks:android-saripaar:1.0.2'
        provided 'junit:junit:4.11'
        // Next 2 Just to make IntelliJ happy:
        provided 'org.mockito:mockito-all:1.9.5'
        provided 'org.robolectric:robolectric:2.3'
        unitTestCompile 'com.google.android:android:4+'
        unitTestCompile files("$project.buildDir/classes/defaultFlavor/debug")
        unitTestCompile 'junit:junit:4.11'
        unitTestCompile 'org.mockito:mockito-all:1.9.5'
        unitTestCompile 'org.robolectric:robolectric:2.3'
    }
    
    sourceSets {
        unitTest {
            java.srcDir file('src/test/java')
        }
    }
    
    task unitTestDisplayResults << {
        toOpen = projectDir.toString() + "/build/reports/tests/index.html"
        println "Opening page: " + toOpen
        ("cmd /C start " + toOpen).execute()
    }
    
    task unitTest(type:Test, dependsOn: assemble) {
        description = "run unit tests"
        testClassesDir = project.sourceSets.unitTest.output.classesDir
        classpath = project.sourceSets.unitTest.runtimeClasspath
        finalizedBy unitTestDisplayResults
    }
    

    错误输出摘录:
    :VG:generateDefaultFlavorDebugSources
    :VG:compileDefaultFlavorDebugJava FAILED
    
    C:\source\VG\src\main\java\com\myapp\errorhandling\VgErrorHandler.java:15: error: cannot find symbol
    

    最佳答案

    vgcommon 模块被定义为应用程序模块,而不是库。在其 build.gradle 中,而不是这一行:

    apply plugin: 'android'
    

    用这个:
    apply plugin: 'android-library'
    

    关于android - 为什么 Gradle 没有接收到我的 Android 模块依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24654472/

    相关文章:

    gradle - 如何根据子文件夹复制文件并重命名

    java - Spring Boot gradle多模块应用

    android - Android Studio 中的代码分享

    java - SonarQube:无法读取 7.1.1-coverage.ec 以确定 JaCoCo 二进制格式

    从 Internet 下载的 android 文件出现异常

    Android如何知道选中了哪个复选框

    android - 如何在 View Holder 上的 OnClick Adapter 中不获取 null 的情况下获取 AdapterPositions?

    android - 资源已经定义 gradle Android studio

    Android Studio Gradle 警告

    android - 围绕给定点 2D 沿圆运动