Android uses fat-aar packaging, local aar and third-party dependent libraries and problems encountered

Why fat-aar is used

There is a module that needs to be packaged into an aar. If you package it directly, the jars, aar, and third-party dependent libraries referenced in the module will not be packaged. The directly generated aar lacks internal references, so fat-aar is used to package the dependencies used in the module.

how to use

1. First add it to the gradle of the project

classpath 'com.github.kezong:fat-aar:1.3.8'

2. Join in repositories

flatDir {
            dirs 'libs'
        }

Insert image description here
3. Add to build.gradle where aar needs to be packaged

apply plugin: 'com.kezong.fat-aar'

4. The third-party library needs to change the dependency implementation to embed, for example:

implementation('com.squareup.okhttp3:okhttp:4.11.0')
改为:
embed('com.squareup.okhttp3:okhttp:4.11.0')
  1. Local aar introduction, for example:
implementation files("libs/xxx.aar")
改为:
embed(name: 'animplayer', ext: 'aar')
//需要加上这个
compileOnly fileTree(dir: 'libs', include: ['*.jar', '*.aar'])

There are multiple architectures in the typed aarlib, such as x86\x86_64

If we don’t need these so libraries, we can filter them out.
Add the so to be filtered in the module build that needs to be packaged.

android{
		//不需要把这些架构打进去
        packagingOptions {
            exclude 'lib/x86/*.so'
            exclude 'lib/x86_64/*.so'
        }
}

The problems I encountered when packaging okhttp and retrofit

This is how I package okhttp and retrofit into aar

embed('com.squareup.okhttp3:okhttp:4.11.0')
embed('com.squareup.okio:okio:3.2.0')
embed('com.squareup.retrofit2:retrofit:2.9.0')
embed('com.squareup.retrofit2:converter-gson:2.9.0')

I didn't encounter any problems during the packaging process, but it crashed when using it.
Through investigation and positioning, it was found that okhttp relies on okio, and retrofit relies on google's gson
, but it was not packaged when packaging aar.
Insert image description here

I saw the fat-aar official website has instructions:

If you want to include all remote dependencies declared in the pom into the final product at the same time, you need to change the transitive value to true in build.gradle, for example: fataar { transitive = true } You can see the official
website link for yourself



I added this and it still didn't take effect .

The solution
is to add these two manually. The changed code is:

		//okhttp3 依赖了okio
        embed('com.squareup.okhttp3:okhttp:4.11.0')
        embed('com.squareup.okio:okio:3.2.0')
        embed('com.squareup.okhttp3:logging-interceptor:4.11.0')
        //retrofit 依赖了gson
        embed('com.squareup.retrofit2:retrofit:2.9.0')
        embed('com.squareup.retrofit2:converter-gson:2.9.0')
        embed('com.google.code.gson:gson:2.8.5')

How do you deal with other problems you encounter?

I just encountered this problem here. If you have problems using other dependencies, you can check whether the dependencies in the dependencies are not included. You can check the dependency library in aar, and then compare it with the dependency tree of your own project to see if there are any There are no missing libraries.

Comments are welcome and I can add the library in question to the article

Guess you like

Origin blog.csdn.net/Mr_ziheng/article/details/131010454