Android jar contains third-party library code solutions

foreword

In order to solve the problem that the jar package cannot contain resource files & third-party library codes, Android launched the aar package, and the packaged aar can contain resource files & third-party library codes, which greatly facilitates our development of SDK.
However, in the process of dynamic loading of dex, dex can only be generated through jar, which limits our sdk to only use jar.

  1. Resource files can be resolved by binary, base64 and other solutions.
  2. There are three solutions for the third-party library code, which can be selected comprehensively according to the size of the library, whether it contains resource files, and other factors.

1.Copy source code

If the amount of third-party library code is small and there is no necessary resource association, we can directly download the source code of the third-party library and copy it to our library.

advantage
  1. The operation is simple, and the copied code is highly maintainable.
  2. If we only need to use individual methods in a large library, we can also copy them to remove unnecessary code, which can reduce the size of the library.
shortcoming
  1. Does not apply to 3rd party libraries that contain resource files & links to other libraries.
  2. Does not apply to libraries that are obfuscated or do not have public source code.
  3. If there is a bug in the copied third-party library code, it may not be found and repaired in time.
  4. After the third-party library is upgraded, the newly added functions cannot be used, and need to be copied and modified again.

2. The main project also references dependencies

After practice testing, the main project that introduces the library project jar adds the same dependencies at the same time, which can ensure that the code in the jar package runs normally, and the resource files can also be used at the same time.

For example, if our library project introduces a picture library glide, then the main project can add the following dependencies at the same time:

    //Glide4.x
    implementation 'com.github.bumptech.glide:glide:4.13.1'
    implementation 'com.github.bumptech.glide:okhttp3-integration:4.13.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.13.1'
advantage
  1. Easiest and theoretically works with any third-party library.
  2. And at the same time, resources in third-party libraries can be used.
  3. The third-party library code is easy to upgrade.
shortcoming
  1. The requirements for the main project are high, and clear documentation is required.
  2. The third-party library version of the main project should preferably be >= the version in the library project. If the lower version is incompatible, it will cause an error in the jar package code.

3. Merge the jar package

If the third-party library does not have the necessary resource association, we can achieve it by merging the jar package.

1. Download jar

Common third-party library jar packages can be downloaded in maven: https://mvnrepository.com/

Take the QR code library zxing as an example:
insert image description here
insert image description here

insert image description here

2. Replace the jar

Put the downloaded jar in the libs directory of the library project.
Delete dependenciesthe remote dependency of the third-party library and replace it with jar package import.

dependencies {
    //使用lib下的jar或者aar
    implementation fileTree(dir: 'libs', include: ['*'])
}    
2. Debug code

After the remote dependency is replaced with jar, is the debugging code normal?

3. Merge jar

Add the following code in the library project build.gradle, andsync now

//根据Library名称生存jar包到build目录下
task makeJar(type: Jar) {
    
    
    //Library名称
    def name = project.name
    //删除之前的旧jar包
    delete 'makejar/' + name + '-' + appVersionName + '.jar'
    //删除之前的旧dex包
    delete 'makejar/' + name + '_dex-' + appVersionName + '.jar'
    //目标jar包名称
    archiveName name + '-' + appVersionName + '.jar'
    //从这个目录下取出默认jar包,不同版本目录均不一样,根据自己项目在build中找classes.jar所在目录
    from(project.zipTree('build/intermediates/aar_main_jar/release/classes.jar'))
    //第三方jar,是需要打包进入jar的
    from(project.zipTree("libs/zxing-core-3.5.1.jar")) 
    include('com/**', '**/')
    //需排除一些无用文件
    exclude('**/META-INF/**', '**/BuildConfig.class')
    //此段代码将相关信息写到META-INF文件中的MANEFEST.MF文件
    manifest {
    
    
        attributes('Library-Name': "${
      
      project.name}", 'Library-Version': "${
      
      appVersionName}")
    }
    //打进jar包后的文件目录
    destinationDirectory = file('makejar')
}
makeJar.dependsOn(build)

Execute the aMakeJar task makejarto generate the jar of the library project in the directory.
insert image description here

The jar can be decompressed, as follows, indicating success. The main project imports the jar package, and the third-party library code can run normally.

insert image description here

advantage
  1. The operation is much more convenient than Copy source code.
  2. Applicable to any third-party library that does not contain resource files (provided that the jar can be obtained).
  3. Third-party libraries are associated with other third-party libraries, and can also be merged at the same time according to this method.
  4. The obfuscated jar package is applicable.
shortcoming
  1. Does not apply to third-party libraries that include resource files.
  2. The maintainability of the third-party library is poor, and the upgrade is troublesome.

reference

Android studio jar contains third-party libraries

Guess you like

Origin blog.csdn.net/DeMonliuhui/article/details/128486132