文档

使用第三方 AAR 资源

更新时间:

本文介绍在使用组件化接入方式(即 Portal&Bundle 接入方式)的场景下如何使用非 com.android.support 的第三方资源。您可以下载并使用本文提供的示例工程,参考下面的使用方法进行体验。

示例工程中包含 SharedResNew、ZHDemo、ZHDemoLauncher 三个工程:

  • SharedResNew:需要被共享的 Bundle,其中包含三方 AAR。

  • ZHDemoLauncher:使用到三方资源的 Bundle。

  • ZHDemo:Portal 工程。

使用第三方资源的过程主要分为以下四步:

  1. 引入第三方资源

  2. 使用 public.xml 导出资源

  3. 验证资源是否导出成功

  4. 使用第三方资源

引入第三方资源

在 SharedResNew 中,com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar 这个包需要外部使用,因此需要通过SharedResNew 的 api 工程使用 compile 方式引入该包。不能使用 implementation 方式。

compile 'com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar'

使用 public.xml 导出资源

app 项目中导出您需要使用的属性。属性将通过 public.xml 文件输出,文件路径固定为 app/src/main/res/values/public.xml

例如,若要导出属性 tl_bar_color,则 public.xml 内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <public name="tl_bar_color" id="0x60010027" type="attr" />
</resources>

其中:

  • name:需所需属性名保持一致。

  • id:在第一次 debug 编译(此时没有 public.xml 文件)之后,您可以从 app/build/generated/source/r/debug/\[com/zh/demo\]\(包名文件夹\)/R.java 中找到 id 值,例如:

      public static final int tl_bar_color=0x60010027;
  • type:指属性所属的类。以 tl_bar_color 为例,其对应的类如下,其 type 值为 attr

       public static final class attr {
           ....
       }

验证资源是否导出成功

进行验证资源是否导出成功前,需确保您已成功构建 SharedResNew。若已完成构建,请完成以下操作进行验证。

步骤 1:找到 aapt 路径

找到 aapt,该文件通常在 Android SDK 中。

假设您的电脑用户名为 username,那么在不同的操作系统下,aapt 的路径如下:

  • Mac 操作系统

    如果您的 Android SDK 位置为:/Users/username/Code/android-sdk 则 aapt 路径一般为: /Users/username/Code/android-sdk/build-tools/28.0.3/aapt

  • Windows 操作系统

    如果您的 Android SDK 位置为:C:\Users\用户名\AppData\Local\Android\Sdk 则 aapt 路径一般为: C:\Users\用户名\AppData\Local\Android\Sdk\build-tools\28.0.3\aapt.exe

说明

build 工具版本需为 26.0.0 或以上版本。

步骤 2:找到本地 bundle 包

打开 SharedResNew > app > build.gradle 后,您会看到如下内容:

version = "1.0.0-SNAPSHOT"
group = "com.zh.demo.shared.res"

其中,group 是 maven gav 中最前面的字段;version 指版本号。

您打开 Android Studio 时可以看见 app 工程的名称是 app [sharedresnew-build],那么该 bundle 的本地 gav 就是 com.zh.demo.shared.res:sharedresnew-build:1.0.0-SNAPSHOT

对应的本地 Maven 仓库目录是:

  • Mac 操作系统

    ~/.m2/repositories/com/zh/demo/shared/res/sharedresnew-build/1.0.0-SNAPSHOT/

  • Windows 操作系统

    C:\Users\用户名\.m2\respositories\com\zh\demo\shared\res\sharedresnew-build\1.0.0-SNAPSHOT

在该目录下,您会看见以下文件:

ivy-1.0.0-SNAPSHOT.xml
ivy-1.0.0-SNAPSHOT.xml.sha1
sharedresnew-build-1.0.0-SNAPSHOT-AndroidManifest.xml
sharedresnew-build-1.0.0-SNAPSHOT-AndroidManifest.xml.sha1
sharedresnew-build-1.0.0-SNAPSHOT-api.jar
sharedresnew-build-1.0.0-SNAPSHOT-api.jar.sha1
sharedresnew-build-1.0.0-SNAPSHOT-raw.jar
sharedresnew-build-1.0.0-SNAPSHOT-raw.jar.sha1

步骤 3:执行验证命令

使用上述步骤中找到的 aapt 路径,执行以下验证命令:

  • Mac 操作系统

      /Users/username/Code/android-sdk/build-tools/28.0.3/aapt d --values resources ./sharedresnew-build-1.0.0-SNAPSHOT-api.jar > res.txt
  • Windows 操作系统

      C:\Users\用户名\AppData\Local\Android\Sdk\build-tools\28.0.3\aapt.exe d --values resources ./sharedresnew-build-1.0.0-SNAPSHOT-api.jar

执行命令后,您会得到一个 res.txt 文件,用记事本之类的软件打开此文件,里面的内容如下所示:

Package Groups (1)
Package Group 0 id=0x60 packageCount=1 name=com.zh.demo
  DynamicRefTable entryCount=22:
    0x3a -> com.alipay.android.liteprocess
    0x7b -> com.alipay.android.multimediaext
    0x6e -> com.alipay.android.phone.falcon.falconlooks
    0x45 -> com.alipay.android.phone.falcon.img

在文件中搜索 tl_bar_color ,会找到如下内容。如果后面有 (PUBLIC) 标记,则表示资源导出成功;若没有,则表示资源导出失败。

resource 0x60010027 com.zh.demo:attr/tl_bar_color: <bag> (PUBLIC)
          Parent=0x00000000(Resolved=0x60000000), Count=1
          #0 (Key=0x01000000): (color) #00000010

使用第三方资源

在使用资源的地方,例如在 ZHDemoLauncher 项目的任意一个 layout 中,在 xml 顶部增加 xml 命名空间。下面以 ZHDemoLauncher/app/src/main/res/layout/main.xml 为例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- xxxx -->
</LinearLayout>
说明

xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo" 这一行中:

  • abc为自定义名称,您可以任意命名;

  • http://schemas.android.com/apk/res/ 为固定值;

  • com.zh.demo 是您在 SharedResNew 的 AndroidManifest.xml 中定义的 package 值。该包值也可以在使用 aapt 导出的 .txt 文件看到,例如 resource 0x60010027 com.zh.demo:attr/tl_bar_color,冒号前面的 com.zh.demo 就是需要用的值。

在使用的地方资源加上以下代码:

<com.flyco.tablayout.SegmentTabLayout
       ....
       abc:tl_bar_color="#f00" />

合起来是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    tools:ignore="ResAuto">
   <com.flyco.tablayout.SegmentTabLayout
       android:id="@+id/myView"
       android:layout_width="wrap_content"
       android:layout_height="32dp"
       android:layout_gravity="center_horizontal"
       android:layout_marginTop="10dp"
       abc:tl_bar_color="#f00"
       tools:visibility="visible" />
</LinearLayout>

至此,您已经完成编译。

代码示例

点击 下载示例代码

  • 本页导读 (0)
文档反馈