2014-04-01 80 views
68

我已經研究了一段時間,可能在這裏看到有關AAR傳遞依賴最流行的答案,但不知何故,它仍然是不明確的,我如何使這個工作沒有解決的AAR庫。傳遞依賴的gradle使用

所以:

我有一個給定的配置的gradle的android庫:

apply plugin: 'android-library' 
apply plugin: 'android-maven' 

version = "1.0.0" 
group = "com.somepackage" 

buildscript { 
    repositories { 
     mavenCentral() 
     mavenLocal() 
    } 

    dependencies { 
     classpath 'com.github.dcendents:android-maven-plugin:1.0' 
    } 
} 

android { 
    compileSdkVersion 19 
    buildToolsVersion '19.0.3' 

    defaultConfig { 
     minSdkVersion 10 
    } 
} 

repositories { 
    maven { url 'http://www.bugsense.com/gradle/' } 
} 

dependencies { 
    provided 'com.google.android.gms:play-services:+' 
    provided 'com.android.support:appcompat-v7:+' 
    compile 'com.google.code.gson:gson:2.2.4' 
    compile 'com.bugsense.trace:bugsense:3.6' 
    compile 'commons-net:commons-net:3.3' 
} 

然後我將其與gradle install部署到本地Maven回購。部署庫的POM文件看起來是這樣的:

<?xml version="1.0" encoding="UTF-8"?> 
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.sprezzat</groupId> 
    <artifactId>app</artifactId> 
    <version>1.0.0</version> 
    <packaging>aar</packaging> 
    <dependencies> 
    <dependency> 
     <groupId>com.bugsense.trace</groupId> 
     <artifactId>bugsense</artifactId> 
     <version>3.6</version> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>commons-net</groupId> 
     <artifactId>commons-net</artifactId> 
     <version>3.3</version> 
     <scope>compile</scope> 
    </dependency> 
    <dependency> 
     <groupId>com.google.code.gson</groupId> 
     <artifactId>gson</artifactId> 
     <version>2.2.4</version> 
     <scope>compile</scope> 
    </dependency> 
    </dependencies> 
</project> 

,並使用上述庫作爲依賴我的Android應用程序的最後gradle這個配置:

buildscript { 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:0.9.+' 
    } 
} 
apply plugin: 'android' 

repositories { 
    mavenCentral() 
    mavenLocal() 
} 

android { 
    compileSdkVersion 15 
    buildToolsVersion "19.0.2" 

    defaultConfig { 
     minSdkVersion 10 
     targetSdkVersion 18 
    } 
} 

dependencies { 
    compile 'com.google.android.gms:play-services:+' 
    compile 'com.android.support:appcompat-v7:+' 
    compile 'com.somepackage:LIBRARY_NAME:[email protected]' 
} 

而且手機上的應用程序部署後我收到NoClassDefFoundError爲屬於我的android庫的編譯依賴關係的類。使用gradle dependencies

檢查我的Android應用程序的依賴關係:

apk - Classpath packaged with the compiled main classes. 
+--- com.google.android.gms:play-services:+ -> 4.3.23 
| \--- com.android.support:support-v4:19.0.1 -> 19.1.0 
+--- com.android.support:appcompat-v7:+ -> 19.1.0 
| \--- com.android.support:support-v4:19.1.0 
\--- com.somepackage:LIBRARY_NAME:1.0.0 

根據上面的樹,都沒有檢測到所有的傳遞依賴。問題在哪裏,應該如何正確完成?

+1

你有研究運行輸出**'gradle這個DEPE爲你的應用程序的**嗎? – CommonsWare

+0

你真的確定你想在那裏提供''關鍵字嗎?根據Xav的說法,[這種依賴關係未包含在APK中](https://groups.google.com/d/msg/adt-dev/WIjtHjgoGwA/h1gS9J5xLUAJ),我想你會希望將這些依賴項打包在APK中。 – CommonsWare

+0

@CommonsWare我的android lib的gradle依賴關係:默認 - 默認工件的配置。 + --- com.google.code.gson:gson:2.2.4 + --- com.bugsense.trace:bugsense:3.6 \ --- commons-net:commons-net:3.3 – mkorszun

回答

72

我已經通過設置transitive屬性爲我的AAR依賴性解決我的問題:

compile ('com.somepackage:LIBRARY_NAME:[email protected]'){ 
    transitive=true 
} 
+3

您是否試圖忽略'@ aar'? –

+1

+1這個作品很有魅力 – Abhijit

+3

@PeterNiederwieser省略「@ aar」會導致Gradle試圖抓取工件作爲.jar文件。這會殺死構建。 – cwc

14

你不應該使用「@aar」,如果用「@」成爲「Artifact only notation」,如果你想使用「@」,並希望有依賴傳遞,你應該添加「傳遞=真」

+1

這個答案很有幫助。我之前的評論中有一個錯字,我刪除了那一個。謝謝你的回答,祝你有個美好的一天:)。 – srain

12

試試這個,如果你使用的是AAR本地:

compile(project(:your-library-name)) { 
    transitive=true 
} 
+4

嗨,它不適合我。我創建了一個內部使用排球庫的圖書館項目。我在應用程序中包含了使用庫項目創建的aar文件。我得到「錯誤:(8,26)錯誤:包com.android.volley不存在」錯誤。在我的庫項目,我已經包括使用編譯(項目(「:凌空」)){凌空傳遞 =真 } – Manish

+2

嘿馬尼什,面臨着同樣的問題,你有沒有找到任何解決辦法? – Jalpesh

+0

不工作,類DEF沒有發現 – yongsunCN