简体   繁体   English

Gradle / Android:将“ root”指定为子项目的依赖项

[英]Gradle/Android: Specify 'root' as a dependency of subproject

First of all, I've got an Android Studio project setup that works fine from within Studio. 首先,我有一个Android Studio项目设置,可以在Studio中正常工作。 I can build it and run it on a device and in the emulator without problem and can also build a release APK. 我可以构建它并在设备和仿真器中毫无问题地运行它,还可以构建发行版APK。

I'm now trying to get to the point where I can have some command-line tools that will handle my build for me. 现在,我试图达到一些命令行工具可以为我处理构建的地步。 All the reading I've done suggests that Android Studio uses gradle, but from what I've seen that's clearly not the case for my project -- as I had to create my build.gradle, settings.gradle files before gradlew would even attempt to build anything -- and yet Android Studio could still compile and deploy just fine. 我所做的所有阅读都表明Android Studio使用gradle,但是从我看到的情况来看,我的项目显然不是这种情况-因为在gradlew尝试之前,我必须创建build.gradle,settings.gradle文件来构建任何东西-但是Android Studio仍然可以编译和部署。

Here's my project structure (as it sits physically on disk): 这是我的项目结构(因为它实际位于磁盘上):

Root Project (Android library project, contains all the src, logic, UI, activities, etc.)
\--- WrapperProject (Android app, depends on the root project)
\--- SubProject1 (Android library, used by root)
\--- SubProject2 (Android library, used by root)

This is my root project's settings.gradle file 这是我的根项目的settings.gradle文件

include ':SubProject1', ':SubProject2'

Likewise, here's my root project's build.gradle file. 同样,这是我的根项目的build.gradle文件。 Note that this project is marked as an android-library and does not reference the WrapperProject in any way (should it?) 请注意,该项目被标记为android-library,并且不以任何方式引用WrapperProject(应该吗?)

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}
apply plugin: 'android-library'

dependencies {
    compile project(':SubProject1')
    compile project(':SubProject2')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17"

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

        instrumentTest.setRoot('tests')
    }
}

I'm able to build my root project successfully using gradlew build -- but my ultimate goal is to build the APK from the WrapperProject that has a dependency on the root project. 我可以使用gradlew build成功构建我的根项目-但我的最终目标是从WrapperProject中构建依赖于根项目的APK。 Can you suggest what my WrapperProject's build.gradle file should contain? 您能建议我的WrapperProject的build.gradle文件包含什么吗?

I'm here because of searching for similar answers, although my problem is primarily the opposite. 我在这里是因为寻找相似的答案,尽管我的问题主要是相反的。 I'm finding that the integration between command-line builds (gradlew) and Android Studio 0.2.2 is full of secrets. 我发现命令行构建(gradlew)与Android Studio 0.2.2之间的集成充满了秘密。

One thing I see that stands out in your config file: this is pretty beta/alpha stuff. 我看到的一件事在您的配置文件中引人注目:这是漂亮的beta / alpha内容。 You have not revised your project to the latest Android Studio 0.2.0 notes. 您尚未将项目修订为最新的Android Studio 0.2.0注释。 In particular, the 'com.android.tools.build:gradle:0.4' should be revised. 尤其应修改“ com.android.tools.build:gradle:0.4”。 Reference: http://tools.android.com/recent 参考: http : //tools.android.com/recent

There is an open source application that has the same basic need as yours. 有一个开源应用程序具有与您相同的基本需求。 It is a Grocery Shopping app that is provided as a sample for CouchDBLIte https://github.com/couchbaselabs/GrocerySync-Android 这是一个杂货店购物应用程序,作为CouchDBLIte https://github.com/couchbaselabs/GrocerySync-Android的示例提供

it has to reference the existing projects. 它必须参考现有项目。 It seems to use a more explicit syntax for the dependencies than you did. 似乎对依赖性使用比您更明确的语法。 example: "compile 'com.couchbase.cblite:CBLiteEktorp:0.7.4'" - including the full class paths. 示例:“编译'com.couchbase.cblite:CBLiteEktorp:0.7.4'”-包括完整的类路径。 But they define two basic approaches, direct integration and artifact integration. 但是它们定义了两种基本方法,直接集成和工件集成。

Sorry I can't provide a more precise answer to your syntax issue - but I'm sharing what I know so far. 抱歉,我无法为您的语法问题提供更精确的答案-但我要分享到目前为止我所知道的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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