具有捆绑依赖项的库(fat aar) [英] library with bundles dependencies (fat aar)

查看:41
本文介绍了具有捆绑依赖项的库(fat aar)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们构建了一个库,并将其分发给我们的客户.我们分发原始 aar 文件供他们使用.此外,我们使用 GitHub 的原始访问 API 来提供 Maven 存储库.

We build a library that we distribute to our customers. We distribute the raw aar files for them to use. Also we use the raw access API of GitHub to provide a Maven repository.

现在为了保持整洁,我们将库分成几个模块:

Now to keep things tidy, we split up the library into several modules:

include ':library'
include ':geohash'
include ':networkstate'
include ':okvolley'
include ':volley'

library 是一个 Android 库,volleyokvolleynetworkstate 也是.

library is an Android library, so are volley and okvolley and networkstate.

现在当我发布library时,依赖树看起来像这样:

Now when I publish library, the dependency tree looks like this:

--- com.sensorberg.sdk:sensorberg-sdk:0.10.0-SNAPSHOT
 +--- com.squareup.okhttp:okhttp-urlconnection:2.2.0
 |    --- com.squareup.okhttp:okhttp:2.2.0
 |         --- com.squareup.okio:okio:1.2.0
 +--- com.google.code.gson:gson:2.3.1 
 +--- android-sdk:okvolley:unspecified
 |    +--- com.squareup.okhttp:okhttp-urlconnection:2.2.0 (*)
 |    +--- com.google.code.gson:gson:2.3.1
 |    +--- android-near-gradle:volley:unspecified
 |    --- com.squareup.okhttp:okhttp:2.2.0 (*)
 +--- android-sdk:networkstate:unspecified
 +--- com.squareup.okhttp:okhttp:2.2.0 (*)
 +--- android-sdk:volley:unspecified
 --- com.loopj.android:android-async-http:1.4.5

如您所见,android-sdk:networkstate:unspecifiedandroid-sdk:okvolley:unspecified 出现在外部依赖项列表中.

As you can see android-sdk:networkstate:unspecified and android-sdk:okvolley:unspecified show up in the list of external dependencies.

我想用捆绑的本地模块创建我的 library aar.它应该做一个本地清单合并和远 jar ......并合并所有模块的依赖项.只应显示外部模块.

I would like to create my library aar with the local modules bundled. It should do a local manifest merge and far jar... and merge the dependcies of all modules. Only external modules should show.

我确实尝试从相应模块的 build/output/aar 文件夹中引用本地 aar 文件,但这似乎也不起作用.它似乎仍然通过名称引用本地模块,而不是合并 jar 的清单...

I did try to reference the local aar file from the build/output/aar folder of the respective modules, but that also seems to not work. It still seems to reference the local modules by their names and not merge the jar's manifests...

有人做过这样的事情吗?在 Android 之外,这将被称为 fatjar,并且有像 musketyr/gradle-fatjar-plugin 这样的插件可以生成一个带 Gradle 的胖子.

Anybody ever done something like this? Outside of Android this would be called a fatjar and there are plugins like musketyr/gradle-fatjar-plugin that produce a fatjar with Gradle.

我在本地模块上试过这个

I tried this on my local modules

if (project.ext.libraryBuild == true) {
    def moduleName = getName()
    File artifactFile = file("/build/outputs/aar/${moduleName}-release.aar")
    if (!artifactFile.exists()) {
        throw new GradleException("Dependency ${moduleName} is not build")
    }
    configurations.create("default")
    artifacts.add("default", artifactFile)
    return;
}


apply plugin: 'com.android.library'

android {
    compileSdkVersion 19
    buildToolsVersion = '21.1.2'
}

直接引用 aar...

to reference the aar directly...

推荐答案

我有一个类似的需求,所以在搜索了很多之后我最终通过 android 插件破解了我的方法.我不太了解 groovy,但我可以设法制作一个 gradle 文件,该文件确实构建了一个被应用程序接受的胖 aar 文件.

I had a similar requirement, so after searching a lot I ending up hacking my way through the android plugin. I do not know groovy much, but I could manage to cook up a gradle file that does build a fat aar file which is accepted by the app.

生成的代码可在 github.com/adwiv/android-fat-aar 获得.详细说明位于 GitHub 站点.结果 build.gradle 看起来像这样:

The resulting code is available at github.com/adwiv/android-fat-aar. The detailed instructions are at the GitHub site. The resulting build.gradle would look like this:

apply from: 'fat-aar.gradle'

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])

  embedded project(':libraryone')
  embedded project('com.example.internal:lib-three:1.2.3')

  compile 'com.android.support:appcompat-v7:22.2.0'
}

当前代码不会尝试合并 AIDL 文件,因为我不使用它们.以下功能对我有用(虽然我的库不是太复杂)

The current code does not attempt to merge AIDL files, since I don't use them. The following features are present and work for me (although my libraries are not too complex)

  1. 类的合并(因此 proguard 在组合类上工作)
  2. 资产和 JNI 库的合并
  3. 合并清单
  4. 资源合并
  5. 嵌入来自同一项目的库
  6. 嵌入 Maven 存储库中的库

只能嵌入android库(aar),不能嵌入jar文件,虽然嵌入库libs文件夹中的jar文件是解压合并的.

Only android libraries (aar) can be embedded, not jar files, although jar files within the libs folders of embedded libraries are unpacked and merged.

尝试一下,让我知道任何问题.您可以浏览代码并根据需要对其进行修改.

Try it out and let me know any issues. You can go through the code and modify it to your needs.

这篇关于具有捆绑依赖项的库(fat aar)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆