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 known as Portal&Bundle access mode. 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: Bundle that uses third-party resources
ZHDemo: Portal project
The process of using third-party resources is mainly divided into the following four steps:
Import third-party resources
In SharedResNew, the 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.
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:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<public name="tl_bar_color" id="0x60010027" type="attr" />
</resources>
Where:
name
: It must be consistent with the property name required.id
: After the first debug compilation (there is nopublic.xml
file at this time), you can find the value ofid
fromapp/build/generated/source/r/debug/\[com/zh/demo\ ]\(package name folder\)/R.java
:public static final int tl_bar_color=0x60010027;
type
: Refers to the class to which the property belongs. Taketl_bar_color
as an example, the corresponding class is as follows, and itstype
value isattr
.public static final class attr { .... }
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 is in the directory
/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 is in
C:\Users\Username\AppData\Local\Android\Sdk
, then the aapt path isC:\Users\Username\AppData\Local\Android\Sdk\build-tools\28.0.3\aapt.exe
.
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:
version = "1.0.0-SNAPSHOT"
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:
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 a command for verification
Based on the aapt URL that is obtained in Step 1, run the following command for verification:
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
For Windows operating systems
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:
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 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.
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 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 sample shows an example where the third-party resource is used in a layout whose URL is 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>
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 ofpackage
that you set inAndroidManifest.xml
of the SharedResNew project. You can find the value of the package in the TXT file that is exported from aapt. For example, inresource 0x60010027 com.zh.demo:attr/tl_bar_color
, the stringcom.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:
<com.flyco.tablayout.SegmentTabLayout
....
abc:tl_bar_color="#f00" />
Therefore, to use the third-party resource, you must add two lines, as shown in the following code snippet:
<?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>
Now, you have completed the compilation.
Sample code
Click Download Sample Code.