生成带混淆配置的aar库

通过将 ProGuard 配置文件添加到包含其 ProGuard 指令的库,您可以在自己的库上启用代码压缩。构建工具会为库模块将此文件嵌入到生成的 AAR 文件中。在您将库添加到应用模块时,库的 ProGuard 文件将附加至应用模块的 ProGuard 配置文件 (proguard.txt)。

通过将 ProGuard 文件嵌入到您的库模块中,您可以确保依赖于此库的应用模块不必手动更新其 ProGuard 文件即可使用库。当 ProGuard 在 Android 应用模块上运行时,它会同时使用来自应用模块和库的指令,因此您不应当只在库上运行 ProGuard。

要指定您的库的配置文件名称,请将其添加到 consumerProguardFiles 方法中,此方法位于您的库的 build.gradle 文件的 defaultConfig 块内。例如,以下片段会将 lib-proguard-rules.txt 设置为库的 ProGuard 配置文件:

android {
    defaultConfig {
        consumerProguardFiles 'lib-proguard-rules.txt'
    }
    ...
}

默认情况下,应用模块会使用库的发布构建,即使在使用应用模块的调试构建类型时亦是如此。要使用库中不同的构建类型,您必须将依赖项添加到应用的 build.gradle 文件的 dependencies 块中,并在库的 build.gradle 文件中将 publishNonDefault 设置为 true。例如,您应用的 build.gradle 文件中的以下代码段会使应用在应用模块于调试模式下构建时使用库的调试构建类型,以及在应用模块于发布模式下构建时使用库的发布构建类型:

dependencies {
    debugCompile project(path: ':library', configuration: 'debug')
    releaseCompile project(path: ':library', configuration: 'release')
}

您还必须在自己库的 build.gradle 文件的 android 块内添加以下代码行,以便将此库的非发布配置展示给使用它的项目:

android {
    ...
    publishNonDefault true
}

android 官方文档:https://developer.android.com/studio/projects/android-library
https://blog.csdn.net/lihenair/article/details/51671803
https://www.jianshu.com/p/14af4a474d55

[转载]Android Gradle 引用本地 AAR 的几种方式

  • 1. 把 AAR 放入 libs
  • 2. 在 build.gradle 添加 repositories{flatDir{dirs ‘libs’}}
  • 3. 在 build.gradle 添加 dependencies{compile(name:’nameOfYourAARFile’, ext:’aar’)}

其中当某个本地 aar/jar 需要在多个 module 进行依赖时,按照官方本身做法

Handling transitive dependencies for local artifacts (jars and aar)
If you have a local jar or aar library that you want to use in more than one project, you cannot just reference it directly as a local dependency. This is because the android plugin will complain if it finds the same jar file twice when dexing the project and all its dependencies. (Note that right now you can't actually use a local aar file even if you only reference it once).

One way to fix this is to deploy the artifact in a repository. While it's possible, it might not be convenient due to the overhead of managing such a repository.

Another option is to create a new Gradle sub-project, and to make this project's published artifact be the jar or aar file that you want to reuse. Then you can simply have other Gradle sub-projects depend on this new sub-project.

In this new sub-project, simply create a build.gradle with the following:

configurations.create("default")
artifacts.add("default", file('somelib.jar'))

经过实际测试,创建一个放有依赖库的 Module,里面包含所要的依赖库,和 build.gradele 文件

例如:当前 Module 名称为:ShareLibs
configurations.create("default")
artifacts.add("default", file('libs/somelib.jar'))

然后把此 Module 添加到根目录的 settings.gradle 中 'sharelibs'

其他各个 Module 都可以使用
implementation project(':sharelibs')

转自:http://www.cnblogs.com/AsionTang/p/5974254.html