Android modify aar and repackage

Table of contents

1. The tools needed to modify aar (just one tool, the way to use is very simple, don't worry)

2. Modify the business logic of the aar code layer

3. Modify the aar layout layout file

4. Attach recyclerview aar to modify the project source code


1. The tools needed to modify aar (just one tool, the way to use is very simple, don't worry)

Note: (the tool below, click on the name to enter the download link, if the link fails, please search and download on Baidu)

  1. Bandizip , a very useful decompression tool, when installing, please remember the installation directory, it may be used later. If there are other decompression tools can also be used.

2. Modify the business logic of the aar code layer

  1. Code positioning:
    1.1 Method 1: In the development tool, jump to the class you want to modify in the aar according to the code
    1.2 Method 2: In Android Studio, change the directory structure from to Android, Projectand then External Librariesfind the corresponding aar and the desired The modified class, as shown below

                                         

  2. Find the class you want to modify in aar, and create a class with the same package name and same name in your project.  For example: if you want to modify the class named RecyclerView under the
    androidx.recyclerview.widget package name in aar , you need to create a new package androidx.recyclerview.widget in your project, and then create a class named RecyclerView
    ( Note that if it conflicts with the recyclerview1.1.0 introduced by default in the project, you can rename the package name. For example, the newly created package name is xx.androidx.recyclerview.widget, but the code under the widget must be copied) .
  3. Copy all the contents of the class RecyclerView in aar to your newly created class RecyclerView (Note: Some contents are decompiled and generated by the compiler, which do not conform to the code development specification, will report an error and need to be modified by yourself), and in the class RecyclerView, Write the logic code you want to achieve.
  4. After the modification is completed, clean the project, and rebuild the project, the editor will report errors Program type already presentor More than one file was found with OS independent pathother package duplication or conflicts, ignore it ( compile but, you can use compileOnly files('libs/recyclerview-1.2.0-alpha04.aar' ) .
    1. Enter the project folder, 项目根目录/build/intermediates/javac/debug/对应包名(the paths of different Android Studio versions may be different, you need to find it yourself), copy 类名开头.classall the files you modified, as shown in the figure below (Note: You must view it from the folder path, do not search the path from Android Studio to copy the class , because the internal class files are not displayed from the Androi Studio path)

  5. Next, it will Bandizipbe explained according to the decompression tool, please operate according to the picture:
    6.1 Right click in the folder to select aarthe package, select the open method, select Bandizip(if not, select from more applications, and then find Bandizip from the Bandizip directory just installed .exe), as shown below


    6.2 In the opened window, operate as shown below


    6.3 Find the package name of the modified class, and copy the .class class file (including the internal class file) generated in step 5

     

     

     


    6.3 Then re-import the aar package into the project (before importing, delete the original aar, clean and then build, then import the new aar package, and then build, otherwise the project will use the old aar package,
    Modify recyclerview and directly put the new aar under the libs file, and then reference it under the build application-level folder implementation files('libs/recyclerview-1.2.0-alpha04.aar')
    )
    6.4 So far, the step of modifying the business code of aar is over.

3. Modify the aar layout layout file

  1. activity_layout_loginSometimes, you need to modify a certain layout file in aar, for example: you want to add a button to the layout file in aar Button.
  2. Next, it will Bandizipbe explained according to the decompression tool, please operate according to the picture
    2.1 Right-click in the folder to select aarthe package, select the open method, select Bandizip(if not, select from more applications, and then find Bandizip from the Bandizip directory just installed. exe), as shown below


    2.2 Click Edit to modify the layout file. After adding or modifying the resource id, please remember the id name, which will be used later, save and close (you can also edit and preview in Android Studio, and then copy the content to this layout file) .

     


    2.3 Modify aarIn the package R.txt, (must be modified, otherwise the newly added or modified resource id cannot be used in the project), right click R.txt, select edit, save and exit, as shown in the figure below:

     

     


    2.4 Then re-import the aar package into the project to introduce new resources (before importing, delete the original aar, clean and then build, then import the new aar package, and then build, otherwise the project will use the old aar package)
    2.5 So far, the step of modifying the business code of aar is over.

4. Attach recyclerview aar to modify the project source code

Link: https://pan.baidu.com/s/1x0zmXD_uQqw_7OVdF3HJ5Q?pwd=ab7d 
Extraction code: ab7d

Guess you like

Origin blog.csdn.net/s_nshine/article/details/131128603