如何在Gradle中为同一目录级别的两个项目表达依赖关系?

时间:2014-08-21 21:41:21

标签: android eclipse gradle dependencies project

编辑:我有主项目编译。但是,测试项目开始编译,然后炸弹说它无法从主项目中找到任何遇到import语句的包。

这是目录结构:

Root Folder A (contains no project)
    settings.gradle

Main Program Folder B (contains Android application project)
    build.gradle

Testing Program Folder C (contains junit test project that references the Main Program)
    build.gradle

我在父文件夹中有一个settings.gradle文件,如下所示:     包括'B','C'

文件夹B有自己的build.gradle文件,没什么特别的。

文件夹C有自己的build.gradle文件,如下所示:

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android'

android {
    compileSdkVersion 16
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
    }       

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        resources.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
        }
    }
}

dependencies {
        compile project (':B')
    }

我在根目录下打开cmd提示符,然后输入gradle build

2 个答案:

答案 0 :(得分:0)

构建文件应该调用build.gradle,而不是gradle.build

settings.gradleinclude 'B', 'C'

请参阅http://www.gradle.org/docs/current/userguide/multi_project_builds.html以获取一个好例子

答案 1 :(得分:0)

解决方案是在Android Studio中创建一个项目,并让它构建Gradle文件。

目录结构:

A
|
settings.gradle
build.gradle
app
  |
  build.gradle
  src
    |
    B
    C

Root settings.gradle:

include ':app'

Root build.gradle:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.2'
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

app build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 19
    buildToolsVersion '20.0.0'

    defaultConfig {
        applicationId "com.example.b"
        minSdkVersion 16
        targetSdkVersion 19
        versionCode 4
        versionName "1.1.0-beta"
    }

    signingConfigs {
        debug {
            storeFile file("./debug.keystore")
        }

        if (System.getenv("KEYSTORE") != null && System.getenv("KEYSTORE_PASSWORD") != null) {
            release {
                println "Release signing key detected"
                storeFile file(System.getenv("KEYSTORE"))
                storePassword System.getenv("KEYSTORE_PASSWORD")
                keyAlias "B"
                keyPassword System.getenv("KEY_PASSWORD")
            }
        }
    }

    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            if (System.getenv("KEYSTORE") != null) {
                signingConfig signingConfigs.release
            }
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile files('libs/ksoap2-android-assembly-3.3.0-jar-with-dependencies.jar')
    androidTestCompile "com.google.dexmaker:dexmaker:1.1"
    androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.1"
    androidTestCompile "org.mockito:mockito-core:1.9.5"
}