[英]Maven Dependencies with Android Studio / Gradle
我正在使用與Android Studio捆綁在一起的Gradle構建系統。 到目前為止,我能夠使用存儲在我的項目結構中的依賴項構建多項目設置。 我現在想使用maven依賴,但無濟於事。 我寫了一個非常簡單的build.gradle文件總是失敗:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.android:support-v4:r7'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
以下消息:
* What went wrong:
A problem occurred configuring project ':absabs'.
> Failed to notify project evaluation listener.
> Could not resolve all dependencies for configuration ':absabs:compile'.
> Could not find com.google.android:support-v4:r7.
Required by:
absabs:absabs:unspecified
....
Caused by: org.gradle.api.internal.artifacts.ivyservice.ModuleVersionNotFoundException: Could not find com.google.android:support-v4:r7.
它發生在我迄今為止嘗試的任何工件上。 什么是錯的?
謝謝
buildscript部分中的“存儲庫”塊僅適用於構建環境。 您還需要指定在構建項目時解析依賴項時要使用的存儲庫,因此您需要在build.gradle文件中添加以下內容:
repositories {
mavenCentral()
}
您的完整文件看起來像這樣:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.android:support-v4:r7'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
請注意“存儲庫”的兩個實例。 您可以在gradle文檔中詳細了解這實際意味着什么。
似乎當前的Android Studio版本不會立即獲取新的依賴項。 嘗試重新啟動IDE。
編輯:Android Studio> = 0.1.4v不需要此功能。 它Sync Project with Gradle file
構建了動作Sync Project with Gradle file
。 您可以在Tools > Android > Sync Project with Gradle file
或工具欄中的按鈕下找到它。
http://tools.android.com/tech-docs/new-build-system/user-guide
注意:這僅影響運行構建的代碼,而不影響項目。 項目本身需要聲明自己的存儲庫和依賴項。 這將在稍后介紹。
所以你必須申報
repositories {
mavenCentral()
}
在您的項目范圍內再次。
我是這樣做的:
在Top build.gradle(Project:YourProject)中,我添加了:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.h2database:h2:1.4.187'
//NOTE: you can get the latest version in http://mvnrepository.com/artifact/com.h2database/h2
}
}
allprojects {
repositories {
jcenter()
mavenCentral()
}
}
注意:我將此與預定義的jcenter()存儲庫一起添加。
然后對於我的app / build.gradle文件,我添加了我需要的任何庫或依賴項:
dependencies {
....//Add dependency here
}
我最近不得不在gradle中使用maven依賴,也許這個小例子對某人有用。
在maven中:
<dependency>
<groupId>com.turo</groupId>
<artifactId>pushy</artifactId>
<version>0.12.1</version>
</dependency>
在等級變成:
repositories {
mavenCentral()
}
dependencies {
compile 'com.turo:pushy:0.12.1'
}
maven Maven繼續使用XML作為編寫構建規范的格式。 然而,結構截然不同。 Maven有自己的問題。 依賴關系管理不能處理同一個庫的不同版本之間的沖突(常春藤更好)。 XML作為構建配置格式是嚴格結構化和高度標准化的。 定制目標(目標)很難。 由於Maven主要關注依賴管理,因此復雜的自定義構建腳本在Maven中寫入比在class1中更難。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.