Android existing applications generate aar and integrate into other applications (local AAR)

1. Assuming that there is already project A and intends to generate aar from project A

(1) Change apply plugin: 'com.android.application' to apply plugin: 'com.android.library' under build.gradle of the main app
(2) Change defaultConfig {} under build.gradle of the main app The applicationId is removed
as shown in the figure below: (3) Process the AndroidMenifest.xml
insert image description here
file in the plug-in application . Remove the startup activity, and other activities do not need to be shielded, just keep them. It must be blocked here, because when the application loads aar, the AndroidMenifenst file will be automatically merged into the manifest file of the main application. Otherwise, there will be two startup activities in the main application's manifest file. After completing the modification according to the above steps, just rebuild the project, and then the aar file will be generated. The path is as follows: It should be noted that in my demo, aar internal page jump is added, mainly to test whether aar is the same as installing apk. Can it jump by itself, and whether it has a complete life cycle.
insert image description here


insert image description here

2. Use aar

Create a new demo main application project
(1) Place the aar file just now under app/libs
insert image description here

(2) Add in the android{} node under app/build.gradle:

repositories {
    
    
        flatDir {
    
    
           dirs 'libs'
        }
    }

and introduce dependencies

implementation(name:'app-debug',ext:'arr')

After build, an error is found:

Build was configured to prefer settings repositories over project repositories but repository 'flatDir' was added by build file 'build.gradle'

The above method is no problem below the gradle7 version. If it is in the gradle7+ version, the following solution is required:

dependencies {
    
    
    implementation fileTree(include: ['*.jar','*.aar'], dir: 'libs')
    implementation(name:'app-debug',ext:'arr')
}

or:

implementation fileTree("libs\\sdk_Login.aar")   // 括号内是aar包相对路径

No need to write firDir.

(3) The problem of them conflict occurs during build. Add in the application tag:
insert image description here

(4) Package the apk and analyze the apk to find that all AndroidMenifest in the aar has been merged into the list of the main application.
There was a problem starting the apk test:

  • The main apk opens the home page and jumps to the home page of aar
  • Crash occurs after jumping to the page inside aar.

Solution : Analyze the apk and find that the layout file in the main application and the layout file in the aar have the same name, resulting in the layout file of the aar overwriting the layout file of the main apk after being packaged into an apk. So at this time, modify the layout file name of the main application.

The home page of the main application:
insert image description here

Click "Click" to jump to the home page of aar:
insert image description here
the jump code from the main page to the aar page is as follows: where com.dic.firstandroidproject.MainActivity is the home page of aar

 public void go(View view) {
    
    
        Intent intent = new Intent();
        intent.setClassName(this,"com.dic.firstandroidproject.MainActivity");
        startActivity(intent);
    }

After testing, there is no problem with the internal jump of aar.

The contents of the merged AndroidMenifest manifest file of the actual apk are posted below for your reference:

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionCode="1"
    android:versionName="1.0"
    android:compileSdkVersion="32"
    android:compileSdkVersionCodename="12"
    package="com.hzs.plguin"
    platformBuildVersionCode="32"
    platformBuildVersionName="12">

    <uses-sdk
        android:minSdkVersion="21"
        android:targetSdkVersion="32" />

    <application
        android:theme="@ref/0x7f0f01d3"
        android:label="@ref/0x7f0e001b"
        android:icon="@ref/0x7f0c0000"
        android:debuggable="true"
        android:testOnly="true"
        android:allowBackup="true"
        android:supportsRtl="true"
        android:fullBackupContent="@ref/0x7f110000"
        android:roundIcon="@ref/0x7f0c0001"
        android:appComponentFactory="androidx.core.app.CoreComponentFactory"
        android:dataExtractionRules="@ref/0x7f110001">

        <activity
            android:name="com.hzs.plguin.MainActivity"
            android:exported="true">

            <intent-filter>

                <action
                    android:name="android.intent.action.MAIN" />

                <category
                    android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


// 以下是aar的
        <activity
            android:name="com.dic.firstandroidproject.DisplayMessageActivity"
            android:parentActivityName="com.dic.firstandroidproject.MainActivity">

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>

        <activity
            android:name="com.dic.firstandroidproject.SecondActivity"
            android:parentActivityName="com.dic.firstandroidproject.MainActivity">

            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>

        <activity
            android:name="com.dic.firstandroidproject.MainActivity" />
    </application>
</manifest>


So far, a simple demo is over, but after my guess, there will be many problems

  • resource conflict problem

  • Loading third-party dependency issues

  • application merge issues

    Please refer to the article: How does Android integrate the sdk (aar) of other applications to achieve application merging

  • How to use module to re-encapsulate aar and provide demo, which is more convenient for other applications to call.

  • The package is too large when integrating multiple aars.

  • How does the main application access aar solve the cross-process problem.

  • What should I do if the sub-application has multiple modules? If multiple modules are dependent on packaging into an aar package for other applications to call

  • How to manage aar version issues

The above problems may be encountered in actual projects, and specific problems must be analyzed in detail.

Summarize

If you want to use this method to integrate apk, it is still a bit complicated. Especially when the sub-application is too large or the architecture is complex, it may face more complicated integration problems. In this way, it is quite difficult to do a good job of integration.
I will also discuss the above problems as soon as possible, and find out the corresponding solutions in actual combat. I look forward to your attention.

Guess you like

Origin blog.csdn.net/superzhang6666/article/details/126529475