All Products
Search
Document Center

Use non Android support 3rd resource library

Last Updated: Apr 01, 2021

This topic describes how to use third-party resources other than com.android.support in the scenario of using the component-based access mode, which is also called as Portal&Bundle access. You can download and use the sample project provided in this topic, and then refer to the following usage method.

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

  • SharedResNew: Bundles that need to be shared, including third-party AAR
  • ZHDemoLauncher: the Bundle that uses third-party resources
  • ZHDemo: Portal project

The process of using third-party resources is mainly divided into the following four steps:

  1. Import third-party resources
  2. Use public.xml to export resources
  3. Verify whether the resource is successfully exported
  4. Use the third-party resource

Import third-party resources

In SharedResNew, this package, com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar, needs to be used externally, so you need to import the package with the compile method in the api project of SharedResNew. Note that you cannot use the implementation method.

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

Use public.xml to export resources

Export the properties you need to use in the app project. The properties will be output through the public.xml file, and the file path is fixed as app/src/main/res/values/public.xml.

For example, if you want to export the property tl_bar_color, the content of public.xml is as follows:

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

On this page:

  • name: the required property names must be consistent

  • id: After the first debug compilation (there is no public.xml file at this time), you can find the value of id from app/build/generated/source/r/debug/\[com/zh/demo\ ]\(package name folder\)/R.java:

    1. public static final int tl_bar_color=0x60010027;
  • type: Refers to the class to which the property belongs. Take tl_bar_color as an example, the corresponding class is as follows, and its type value is attr.

    1. public static final class attr {
    2. ....
    3. }

Verify whether the resource is successfully exported

Before you verify whether the resource is successfully exported, you need to make sure that you have successfully built SharedResNew. If the build has been completed, complete the following operations for verification.

Step 1: Find the aapt path.

You can usually find the aapt in the Android SDK.

Assuming your computer user name is “username”, the paths of aapt under different operating systems are as follows:

  • Mac operating system

    If your Android SDK location is: /Users/username/Code/android-sdk,
    then the aapt path is: /Users/username/Code/android-sdk/build-tools/28.0.3/aapt

  • For Windows operating systems

    If your Android SDK location is: C:\Users\Username\AppData\Local\Android\Sdk,
    then the aapt path is: C:\Users\Username\AppData\Local\Android\Sdk\build-tools\28.0.3\aapt.exe

Note: The build tool must be 26.0.0 or later versions.

Step 2: Find the local bundle package.

When you choose SharedResNew> app > build.gradle, you will see the following content:

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

Among them, group is the first field in maven gav; version refers to the version number.

When you open Android Studio, you can see that the name of the app project is app [sharedresnew-build], then the local gav of the Bundle is com.zh.demo.shared.res:sharedresnew- build:1.0.0-SNAPSHOT.

The following URL is the directory of the corresponding local Maven library:

  • Mac operating system

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

  • For Windows operating systems

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

This directory contains the following files:

  1. ivy-1.0.0-SNAPSHOT.xml
  2. ivy-1.0.0-SNAPSHOT.xml.sha1
  3. sharedresnew-build-1.0.0-SNAPSHOT-AndroidManifest.xml
  4. sharedresnew-build-1.0.0-SNAPSHOT-AndroidManifest.xml.sha1
  5. sharedresnew-build-1.0.0-SNAPSHOT-api.jar
  6. sharedresnew-build-1.0.0-SNAPSHOT-api.jar.sha1
  7. sharedresnew-build-1.0.0-SNAPSHOT-raw.jar
  8. sharedresnew-build-1.0.0-SNAPSHOT-raw.jar.sha1

Step 3: Run a command for verification

Based on the aapt URL that is obtained in Step 1, run the following command for verification:

  • Mac operating system

    1. /Users/username/Code/android-sdk/build-tools/28.0.3/aapt d --values resources ./sharedresnew-build-1.0.0-SNAPSHOT-api.jar > res.txt
  • For Windows operating systems

    1. 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. Use software, such as Notepad, to open the file. The following code snippet shows part of the content of this file:

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

Search for “tl_bar_color” in the file to find the following content: If a (PUBLIC) mark appears at the end of the first line, the third-party resource is exported. Otherwise, the export failed.

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

Use the third-party resource

Open the file where you want to use the third-party resource, for example, a layout in the ZHDemoLauncher project. Then, add a line for an XML namespace at the top of the file. The following code snippet shows an example where the third-party resource is used in a layout whose URL is ZHDemoLauncher/app/src/main/res/layout/main.xml:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent">
  7. <!-- xxxx -->
  8. </LinearLayout>
Note: In the xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo" line,
  • abc represents a custom name. You can set the name as you want.
  • http://schemas.android.com/apk/res/ is a fixed directory and cannot be changed.
  • com.zh.demo must be the same as the value of package that you set in AndroidManifest.xml of the SharedResNew project. You can find the value of the package in the TXT file that is exported from aapt. For example, in resource 0x60010027 com.zh.demo:attr/tl_bar_color, the string com.zh.demo before the colon is the value you need.

Next, add another line where you want to use the third-party resource, as shown in the following code snippet:

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

Therefore, to use the third-party resource, you must add two lines, as shown in the following code snippet:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. xmlns:app="http://schemas.android.com/apk/res-auto"
  5. xmlns:abc="http://schemas.android.com/apk/res/com.zh.demo"
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent"
  8. android:orientation="vertical"
  9. tools:ignore="ResAuto">
  10. <com.flyco.tablayout.SegmentTabLayout
  11. android:id="@+id/myView"
  12. android:layout_width="wrap_content"
  13. android:layout_height="32dp"
  14. android:layout_gravity="center_horizontal"
  15. android:layout_marginTop="10dp"
  16. abc:tl_bar_color="#f00"
  17. tools:visibility="visible" />
  18. </LinearLayout>

Now, you have completed the compilation.

Sample code

Click Download Sample Code.