Use third-party AAR resources

更新时间:
复制 MD 格式

This topic describes how to use third-party resources, other than com.android.support, in a component-based project that uses the Portal & Bundle connection type. You can download and use the sample project provided in this topic to try the following procedures.

The sample project contains three projects: SharedResNew, ZHDemo, and ZHDemoLauncher.

  • SharedResNew: The bundle to be shared, which contains the third-party Android Archive (AAR).

  • ZHDemoLauncher: The bundle that uses the third-party resources.

  • ZHDemo: The Portal project.

The procedure for using third-party resources includes the following four steps:

  1. Import third-party resources

  2. Export resources using public.xml

  3. Verify the resource export

  4. Use the third-party resources

Import third-party resources

In SharedResNew, the com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar package must be used externally. Therefore, import the package into the api project of SharedResNew using the compile method instead of the implementation method.

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

Export resources using public.xml

Export the properties that you want to use in the app project. The properties are exported using the public.xml file. The file path is fixed at app/src/main/res/values/public.xml.

For example, to export the tl_bar_color property, the public.xml file contains the following content:

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

Where:

  • name: Set this to the name of the property.

  • id: After the first debug compilation, if a public.xml file does not exist, you can find the id value in app/build/generated/source/r/debug/[com/zh/demo](package folder)/R.java. For example:

      public static final int tl_bar_color=0x60010027;
  • type: The class to which the property belongs. For example, the class that corresponds to tl_bar_color is shown in the following code. The type value is attr.

       public static final class attr {
           ....
       }

Verify the resource export

Before you verify the resource export, ensure that you have built SharedResNew. After the build is complete, perform the following steps to verify the export.

Step 1: Find the aapt path

Find the aapt tool. This tool is usually located in the Android SDK.

Assume that your computer username is username. The aapt path varies based on the operating system:

  • Mac operating system

    If your Android SDK is located at /Users/username/Code/android-sdk, the aapt path is typically /Users/username/Code/android-sdk/build-tools/28.0.3/aapt.

  • Windows operating system

    If your Android SDK is located at C:\Users\username\AppData\Local\Android\Sdk, the aapt path is typically C:\Users\username\AppData\Local\Android\Sdk\build-tools\28.0.3\aapt.exe.

Note

The build tools version must be 26.0.0 or later.

Step 2: Find the local bundle package

Open the build.gradle file in SharedResNew > app. You will see the following content:

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

group is the first field of the Maven GAV. version is the version number.

When you open Android Studio, the name of the app project is app [sharedresnew-build]. Therefore, the local GAV of the bundle is com.zh.demo.shared.res:sharedresnew-build:1.0.0-SNAPSHOT.

The corresponding local Maven repository folder is:

  • Mac operating system

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

  • Windows operating system

    C:\Users\username\.m2\repository\com\zh\demo\shared\res\sharedresnew-build\1.0.0-SNAPSHOT

In this folder, you will see the following files:

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

Step 3: Run the verification command

Execute the following validation command using the aapt path from the preceding steps:

  • Mac operating system

      /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 operating system

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

After you run the command, a res.txt file is generated. Open this file with a text editor, such as Notepad. The file contains the following content:

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

Search for tl_bar_color in the file. You should find the following content. If the content is followed by the (PUBLIC) mark, the resource was exported successfully. Otherwise, the export failed.

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

Use the third-party resources

In the layout file where you want to use the resources, such as a layout file in the ZHDemoLauncher project, add an XML namespace to the top of the file. The following example uses 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>
Note

In the xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo" line:

  • abc is a custom name. You can specify any name.

  • http://schemas.android.com/apk/res/ is a static field.

  • com.zh.demo is the package value defined in the AndroidManifest.xml file of SharedResNew. You can also find this package value in the .txt file that was exported by aapt. For example, in resource 0x60010027 com.zh.demo:attr/tl_bar_color, the com.zh.demo part before the colon is the value to use.

Add the following code where the resource is used:

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

The following is the result:

<?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>

The compilation is complete.

Code sample

Click Download sample code.