简体   繁体   English

使用Gradle的外部Android库项目

[英]External Android library projects with Gradle

I am trying to build an Android project with Gradle and the Android Gradle plugin . 我正在尝试使用Gradle和Android Gradle插件构建一个Android项目。 I would like to depend on library projects found in external (maven) repositories, eg ActionBarSherlock. 我想依赖于在外部(maven)存储库中找到的库项目,例如ActionBarSherlock。

This seems possible according to the official site : 根据官方网站,这似乎是可能的:

Using a library is done one of the following way: 使用库可以通过以下方式之一完成:

Multi-project setup. 多项目设置。 Read here: http://www.gradle.org/docs/current/userguide/multi_project_builds.html 请阅读此处: http//www.gradle.org/docs/current/userguide/multi_project_builds.html

Dependencies through a repo such as maven or ivy. 通过诸如maven或常春藤等仓库的依赖关系。

The current contents of my build.gradle: build.gradle的当前内容:

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

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.actionbarsherlock:library:4.2.0'
}

android {
    target = 'android-16'
    sourceSets {
        main {
            manifest {
                srcFile 'AndroidManifest.xml'
            }
            java {
                srcDir 'src'
            }
            res {
                srcDir 'res'
            }
            assets {
                srcDir 'assets'
            }
            resources {
                srcDir 'src'
            }
        }
    }
}

I am using Gradle 1.2. 我正在使用Gradle 1.2。 When I try to build with gradle assemble , I get the following error: 当我尝试使用gradle assemble构建时,我收到以下错误:

Error: duplicate files during packaging of APK /[path to project]/build/apk/[project name]-debug-unaligned.apk
    Path in archive: AndroidManifest.xml
    Origin 1: /[path to project]/build/libs/[apk name]-debug.ap_
    Origin 2: /[home directory]/.gradle/caches/artifacts-14/filestore/com.actionbarsherlock/actionbarsherlock/4.2.0/apklib/dd63451a922558005d8c10be1085b488ed731d19/actionbarsherlock-4.2.0.apklib
:packageDebug FAILED

It seems like it is trying to include the AndroidManifest.xml from both the library project and my project. 似乎它试图从库项目和我的项目中包含AndroidManifest.xml。 If I remove the manifest specification in sourceSets , I still get the same error. 如果我删除sourceSets中的manifest规范,我仍然会得到相同的错误。

The site mentions using apply plugin: 'android-library' for library projects; 该网站提到使用apply plugin: 'android-library'用于图书馆项目; I am guessing this is only when building the actual library (with a multi-project setup) since doing so does not produce an APK. 我猜这只是在构建实际库(使用多项目设置)时,因为这样做不会产生APK。

How can I get external Android library project dependencies to work? 如何让外部Android库项目依赖项工作?

While it is possible to get dependencies through a Maven or Ivy repository, those are not packaged in a standard Jar archive. 虽然可以通过Maven或Ivy存储库获取依赖关系,但这些存储库不会打包在标准的Jar存档中。 There is a new format designed specifically for this build system. 有一种专门为此构建系统设计的新格式。 The type is 'aar' 类型是'aar'

Current Android Libraries available on Maven Central were created using the Maven plugin which use a different archive format ('apklib') so they are not compatible. Maven Central上现有的Android库是使用Maven插件创建的,它使用不同的存档格式('apklib'),因此它们不兼容。

If you look at com.actionbarsherlock:actionbacksherlock (which is the new location of that library), you'll see its format is apklib. 如果你看一下com.actionbarsherlock:actionbacksherlock(这是该库的新位置),你会看到它的格式是apklib。 http://search.maven.org/#artifactdetails%7Ccom.actionbarsherlock%7Cactionbarsherlock%7C4.2.0%7Capklib http://search.maven.org/#artifactdetails%7Ccom.actionbarsherlock%7Cactionbarsherlock%7C4.2.0%7Capklib

Looking at the source code, we check for 'aar' packaging and revert to processing all others as 'jar' which is obviously a problem here. 看一下源代码,我们检查'aar'包装并恢复处理所有其他包装'jar',这显然是一个问题。 We should detect apklib and provide a better error message. 我们应该检测apklib并提供更好的错误消息。

You can integrate the ActionBarSherlock as an aar now. 您现在可以将ActionBarSherlock集成为aar。

Just add this to your dependency definition in your build.gradle file: 只需将它添加到build.gradle文件中的依赖项定义中:

dependencies {
    compile 'com.android.support:support-v4:18.0.+'
    compile 'com.actionbarsherlock:actionbarsherlock:4.4.0@aar'

Jake Wharton published an example project that shows how to use ActionBarSherlock with Gradle Jake Wharton发布了一个示例项目 ,展示了如何将ActionBarSherlock与Gradle一起使用

You are using an older version of the android gradle plugin. 您正在使用旧版本的android gradle插件。
In the latest version 0.3 a lot has been improved in this area. 在最新版本0.3中,该领域已经有很多改进。

I would advise you to upstep to version 0.3 of this plugin. 我建议你上升到这个插件的0.3版本。 Be also aware that you then have to use a more recent version of gradle (1.3 or 1.4). 还要注意,您必须使用更新版本的gradle(1.3或1.4)。

More information about how external libraries can be used in with this plugin. 有关如何在此插件中使用外部库的更多信息。

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

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