How to integrate leakcanary-android service through AAR form

How to integrate leakcanary-android service through AAR form

leakcanary-android 官网:square/leakcanary: A memory leak detection library for Android and Java. 

How to get all relevant dependency files by online reference?

#1. Disable Gradle offline mode

#2. Add project dependencies as required by the documentation

In your build.gradle:

dependencies {
debugImplementation 'com.squareup.leakcanary:leakcanary-android:1.5.4'
releaseImplementation 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.4'
}

#3.Sync will parse and cache all related dependencies online after synchronization

#4. Find the *.pom file in the directory where the dependency cache is located and analyze its sub-dependencies

The general path of the Windows system cache:

  • .gradle\caches\modules-2\files-2.1\com.squareup.leakcanary\leakcanary-android\1.5.4
  • .gradle\caches\modules-2\files-2.1\com.squareup.leakcanary\leakcanary-android-no-op\1.5.4

The following types of files generally exist in subdirectories: *-sources.jar, .aar or .jar, *.pom 

Open the *.pom file through the text tool, it can be seen that it is the text in XML format, and you can get the sub-dependency information by finding the following node data:

project - dependencies - dependency - artifactId

#5. Copy all dependencies out.

*.aar或*.jarCopy all file dependencies in the cache and put them in the specified flatDirdirectory (such as a libsdirectory.)

#6. Retrofit .aar dependency file

Integrate JAR class library files that are only used in Release mode

#7. Modify the project dependencies in the original online form to dependencies in the AAR form

debugImplementation(name: 'leakcanary-android-1.5.4', ext: 'aar')
releaseImplementation(name: 'leakcanary-android-no-op-1.5.4', ext: 'aar')

Note that dependencies in the form of AAR need to build.gradlebe added with the following settings:

repositories {
flatDir {
dirs 'libs'
}
}

#8. Enable Gradle offline mode to use it normally.

How to transform Release dependencies .aar file to integrate JAR class library files that are only used in Release mode?

The ultimate goal is to:

  1. To ensure that the function  can be used normally in the running mode , debugleakcanary-android
  2. Ensure that no extra code and resources are added in release mode (only no-op classes are added to ensure that compilation does not report errors) Release

Before retrofit: leakcanary-android-1.5.4.aar > libs is empty

After transformation: Add all its dependent JAR packages to the  directory. leakcanary-android-1.5.4.aar > libs

leakcanary-androidDependencies that are originally JARs are:

  1. leakcanary-watcher-1.5.4.jar
  2. haha-2.0.3.jar

Some of the dependencies are AARs that need to be extracted out of the JAR file:

  1. leakcanary-analyzer-1.5.4.aar

After decompressing it, extract it and  change the name to distinguish it. classes.jarleakcanary-analyzer-1.5.4.jar

The final leakcanary-android-1.5.4.aar > libs directory contains the following files:

  1. leakcanary-watcher-1.5.4.jar
  2. haha-2.0.3.jar
  3. leakcanary-analyzer-1.5.4.jar

Result verification

Release mode class structure diagram:

Debug mode class structure diagram:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325283858&siteId=291194637
Recommended