android studio 官方用户指南翻译学习(四)创建 Android library

Android library ****结构上和****Android app ****的****module****是一样的。他可以包含所有的可以在****app****中构建的东西,包括**** ****资源代码**** ****资源文件**** ****或者****Android manifest ****清单文件。不同的是,****app****会编译成****ask****运行在一个设备上,****Android library ****会编译成**** Android archive ****(****AAR****)文件,你可以作为****app module ****的一个依赖**** ****(****dependency****)

一个library module 可能被用在如下情况:
· 当你在构建一个多个部分组成的app ,在app中 会有相同的组件,例如activity service 或者 ui布局
· 当你要构建一个app的但是要求有一些差异的apk包时,例如免费版 和收费版,你需要在两个包中包含相同的核心组件

这两种情况下,只需要简单的包你要重用的部分移动到一个library然后添加这个library作为每个app module的依赖即可。本文将讲解如何创建Android library。

创建 library module

创建一个library module 步骤如下
1.点击File > New > New Module.
2.出现 Create New Module 界面,点击Android Library ,然后点击Next 。另外 还有 Java Library的选项,用来建立传统jar包文件,jar文件可用于很多项目,尤其是当你想把代码分享在不同的平台,jar文件不允许你包含Android 资源文件和manifest文件。那么此文首先关注如何创建 Android Library
3.设置你的library的名称 并选择兼容的最低SDK版本,然后点击Finish

一旦Gradle 项目同步完成,library module 会在左侧的Project 面板出现,如果你没发现新的module文件夹,检查一下是否在Android 视图

《android studio 官方用户指南翻译学习(四)创建 Android library》 Paste_Image.png

把一个****app module ****转换成****library module**

如果已存在一个app module ,你想重用这里面所有的代码,你也可以按照如下步骤把他转换成library module

1.打开这个app 的 build.grale 文件,在顶部,你可以看到一行脚本
apply plugin: 'com.android.application'
‘com.android.library’把这行代码 改成
apply plugin: 'com.android.library'
点击 Sync Project with Gradle Files. 同步代码 即可完成转换

简单吧,此时整个module结构和之前还是一样的,但是现在作为一个Android libray运行,在构建的时候回构建成AAR文件而不是apk文件了。

添加你的 library 作为 dependency

在另一个app module 中使用你的Android library 的代码 方法如下
添加library 到你的项目中 的两种方式(如果你是在你项目内新建的library 可以跳过此步骤)
添加AAR或者jar文件
点击 File > New Module. 2.点击Import .JAR/.AAR Package ****然后****Next 3****。找到****arr****或者****jar****文件点击****finish

导入library module 到你项目中
点击 File > New > Import Module. 2 进入到到导入的目录 找到相应的文件 点击finish。接下来 你的library module 会被拷贝到你的项目目录,所以你可以直接的编辑你的library代码。如果要保持一个单一版本的library 代码,这种方式可能不是你想要的。应该通过导入arr文件如上文描述。
确保library module 在settings.gradle 文件中被列出,类似如下
Open the app module’s build.gradle file and add a new line to the dependencies block as shown in the following snippet:dependencies {
compile project(“:my-library-module”)
}打开app的build.gradle 文件,然后在dependencies 语句块添加一行代码如下
dependencies { compile project(":my-library-module") }
点击Sync Project with Gradle Files. 同步

在这个例子中,名称为my-library- module 的Android library 被构建成了主module的依赖文件,在你的项目中可以任意引用library的资源和代码,同时ARR文件也被打包到apk文件了。
然后 如果你想单独的分享你的arr文件 ,你可以在 project-name/module-name/build/outputs/aar/文件夹中找打arr文件,当然 你也可以点击Build > Make Project ****重新构建

选择性公开资源

默认library中的所有资源都是public 的,让所有的资源变成私有的,你必须定义至少一个具体属性作为public ,在res文件中的都为资源,例如图像。要防止用户在外部引用你打算只在内部引用的资源,你可以声明几个你想设置为public的资源的方式 去私有化其余的资源。
声明public 资源文件,需要在你的library的 public.xml文件中声明。如果你之前还没有添加public资源,你需要在res/values/中先创建public.xml文件;下面是创建连个资源为public 的例子
<resources> <public name="mylib_app_name" type="string"/> <public name="mylib_public_string" type="string"/> </resources>
你应该公开所有你项目开放给使用你的 library的开发者,例如,即v7包中大部分的资源都是私有的,但控制Toolbar控件的属性都是public 的

使你的资源属性私有化,不仅可以防止library使用者从外部获得完整的代码资源,而且允许你重命名或者删除私有资源,而不会使你的用户端崩溃,私有的资源会被编辑提示器过滤掉,如果你试图引用私有资源lint会报出警告。

开发注意事项

当你准备开发你的library依赖到你app时,要了解以下的限制和行为
当你添加library里的引用到你的app module ,你可以设置他们的优先级,当构建时,library和app会从优先级低到高的进行一次合并,

*资源合并冲突

构建工具在合并library 和app的资源时,如果一个资源ID在两者中都有定义相同的资源id。如果冲突出现在多个aar库中,则 按照在build.gradle中 dependencies 语句块中的列出次序,在首位的library 资源将会被使用。 要避免这种资源冲突,可以考虑用加前缀或者其他方式命名的方式,来确保资源名称的唯一性。

library module ****还可以包含一个****jar library

library module可以引用external library.(比如, Maps external library).依赖者app module也必须构建引用相同的external library.注意:library module和依赖者app module都必须在它们的manifest文件中使用uses- library来声明引用external library.

library module不能包含assets资源

编译工具不支持在library modules中使用asset资源(存放在assets/ 文件下)。所有使用到的asset资源都必须存放在app module的assets/ 文件夹下。

app module的minSdkVersion必须大于等于library module的

library module最后被编译成app module的一部分,所以app module使用的SDK版本必须包含library module使用的SDK版本中的所有API。

每个library module都会创建它们自己的R

当你编译app module时,library module都会被编译进一个AAR文件,然后将这个文件加入app module.因此,每个library都有它们自己的R类,按各自的包名命名。

你也可以通过添加PROGUARD 配置文件给你的library混淆压缩你的 library,构建工具会把这个文件嵌入到当你的AAR文件中,当你把此library添加到app module中,library的PROGUARD配置文件被添加到app module的program.txt文件了
By embedding a ProGuard file in your library module, you ensure that app modules that depend on your library do not have to manually update their ProGuard files to use your library. When ProGuard runs on the Android app module, it uses the directives from both the app module and the library so you should not run ProGuard on the library alone.To specify the name of your library’s configuration file, add it to the consumerProguardFiles method, inside the defaultConfig block of your library’s build.gradle file. For example, the following snippet sets lib-proguard-rules.txt as the library’s ProGuard configuration file:android {
**** defaultConfig {
**** consumerProguardFiles ‘lib-proguard-rules.txt’
**** }
**** …
****}
****By default, the app module uses the library’s release build, even when using the app module’s debug build type. To use a different build type from the library, you must add dependencies to the dependencies block of the app’s build.gradle file and set publishNonDefault to true in the library’sbuild.gradle file. For example, the following code snippet in your app’s build.gradle file causes the app to use the library’s debug build type when the app module builds in debug mode and to use the library’s release build type when the app module builds in release mode:dependencies {
**** debugCompile project(path: ‘:library’, configuration: ‘debug’)
**** releaseCompile project(path: ‘:library’, configuration: ‘release’)
****}You must also add the following line inside the android block of your library’s build.gradle file to expose your library’s non-release configurations to projects that use it:android {
**** …
**** publishNonDefault true
****}Note, however, that setting publishNonDefault can increase build times.To ensure that your library’s ProGuard rules do not apply unwanted shrinking side effects to app modules, only include rules that disable ProGuard features that do not work with your library. Rules that attempt to aid developers can conflict with the existing code in an app module or its other libraries and therefore should not be included. For example, your library’s ProGuard file can specify what code needs to be kept during an app module’s minification.Note: The Jack toolchain provides support for only some shrinking and obfuscation options with ProGuard.

    原文作者:Williamzhng
    原文地址: https://www.jianshu.com/p/7d74d58fc5d6
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞