Android Gradle 为什么我们需要 "allprojects"?
Android Gradle Why do we need "allprojects"?
我在尝试将 Gson 添加到我现有的 Android gradle 项目时遇到了问题。
例外是这样的
" failed to resolve:com.google.code.gson:gson:2.3.1"
我的build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
.
.
经过一些搜索和反复试验,我发现您需要添加以下内容来解决问题:
allprojects {
repositories {
jcenter()
}
}
为什么我们需要引用 jcenter 两次,为什么 "allprojects" 对解决这个问题很重要?
它们只是存储库的不同存储库。 GSON 显然在 JCentral 中,其他人正在使用 Maven Ventral。
通过阅读 JCenter,它是 Bintray 背后的存储库,来自 JFrog 公司(我以前遇到过,我想这就是 'J' 的来源)。根据 Bintray 博客,Bintray 是 Maven Central 的超集。
After some searching and trail and error i found that you need to add the following to fix the issue
仅当您有一个传统的 Android Studio 项目时,您将应用程序划分为多个模块(例如,app/
)。真正需要的是让模块知道从哪里提取依赖项。
Why to do we need to reference the jcenter two times
你的 buildscript
闭包中的 repositories
闭包说 "these are the artifact repositories from which to pull Gradle plugins and other dependencies
listed for the buildscript
closure itself".
你在 allprojects
闭包中的 repositories
闭包说 "these are the artifact repositories from which to pull your own application dependencies
".
在传统的 Android Studio 项目中,allprojects
闭包位于项目根目录中的 build.gradle
文件中。 allprojects
说 "everything in this closure, please apply to the Gradle configuration for all of the modules in this project, such as that app/
module over there"。
现在,你问题中的 build.gradle
片段暗示你可能 没有 有一个传统的 Android Studio 项目,而是有一个你没有 app/
模块并且只有一个 build.gradle
文件。在那种情况下,allprojects
本身不是必需的(因为没有模块),但您仍然需要指定 repositories
是什么。
例如,这是来自 a module-less project 的 build.gradle
文件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
dependencies {
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.android.support:support-v13:21.0.3'
compile 'com.commonsware.cwac:wakeful:1.0.+'
}
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
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']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
这里,除了 buildscript
中的 repositories
闭包(用于定义插件的来源)之外,我还有一个顶级 repositories
闭包,用于定义顶级dependencies
来自
我在尝试将 Gson 添加到我现有的 Android gradle 项目时遇到了问题。
例外是这样的
" failed to resolve:com.google.code.gson:gson:2.3.1"
我的build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
.
.
经过一些搜索和反复试验,我发现您需要添加以下内容来解决问题:
allprojects {
repositories {
jcenter()
}
}
为什么我们需要引用 jcenter 两次,为什么 "allprojects" 对解决这个问题很重要?
它们只是存储库的不同存储库。 GSON 显然在 JCentral 中,其他人正在使用 Maven Ventral。
通过阅读 JCenter,它是 Bintray 背后的存储库,来自 JFrog 公司(我以前遇到过,我想这就是 'J' 的来源)。根据 Bintray 博客,Bintray 是 Maven Central 的超集。
After some searching and trail and error i found that you need to add the following to fix the issue
仅当您有一个传统的 Android Studio 项目时,您将应用程序划分为多个模块(例如,app/
)。真正需要的是让模块知道从哪里提取依赖项。
Why to do we need to reference the jcenter two times
你的 buildscript
闭包中的 repositories
闭包说 "these are the artifact repositories from which to pull Gradle plugins and other dependencies
listed for the buildscript
closure itself".
你在 allprojects
闭包中的 repositories
闭包说 "these are the artifact repositories from which to pull your own application dependencies
".
在传统的 Android Studio 项目中,allprojects
闭包位于项目根目录中的 build.gradle
文件中。 allprojects
说 "everything in this closure, please apply to the Gradle configuration for all of the modules in this project, such as that app/
module over there"。
现在,你问题中的 build.gradle
片段暗示你可能 没有 有一个传统的 Android Studio 项目,而是有一个你没有 app/
模块并且只有一个 build.gradle
文件。在那种情况下,allprojects
本身不是必需的(因为没有模块),但您仍然需要指定 repositories
是什么。
例如,这是来自 a module-less project 的 build.gradle
文件:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
}
}
apply plugin: 'com.android.application'
repositories {
mavenCentral()
maven {
url "https://repo.commonsware.com.s3.amazonaws.com"
}
}
dependencies {
compile 'de.greenrobot:eventbus:2.4.0'
compile 'com.android.support:support-v13:21.0.3'
compile 'com.commonsware.cwac:wakeful:1.0.+'
}
android {
compileSdkVersion 19
buildToolsVersion "21.1.2"
defaultConfig {
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']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
这里,除了 buildscript
中的 repositories
闭包(用于定义插件的来源)之外,我还有一个顶级 repositories
闭包,用于定义顶级dependencies
来自