When our library relies on a third party’s AAR file, the third party’s AAR file will not be packaged when we package and publish our library to maven’s repository. When we reference our library, we will report that we cannot find the class in the third party’s library, thus raising an exception.
Module project Profiling references a number of AAR files as shown below:
Figure 1
Module project Profiling:
apply plugin: 'maven'
uploadArchives {
repositories {
mavenDeployer {
pom.groupId = 'com.mei.profiling'
pom.artifactId = 'profiling'
pom.version = '1.0.0'
// Upload to the local directory
repository(url: uri('.. /repo'))}}}Copy the code
Reference the Profiling library in the main project:
implementation "Com. Mei. Profiling: profiling: 1.0.0"
Copy the code
When running the project, when using the classes associated with the AAR in Figure 1, an exception is reported that the class cannot be found.
This is because when we upload the profiling project to Maven, aar files under the profiling /libs file are not packaged together into the Profiling AAR file.
So how do you solve this problem? There are two ways:
- Put the third party AAR file in the liBS folder of the main project
- Upload the third-party AAR file to our own Maven library
1. Add the aar file of the third party under the LIBS folder of the main project
Add the third-party AAR file under the liBS folder of the main project to solve the problem that the library project references the third-party AAR file, but the third-party AAR file will not be packaged in the package, so that the class of the third-party library cannot be found when using.
1. Add the aar file of the third party under the LIBS folder of the main project
Such as:
2, modify build.gradle, reference aar file of third party:
dependencies {
implementation fileTree(dir: "libs".includes: ['*.jar'.'*.aar'])}Copy the code
Added ‘.aar’ dependency file type.
This approach solves the problem of not finding third party classes, but it’s not elegant enough to encapsulate an SDK for the purpose of aggregating code, and now it doesn’t do that.
Upload a third-party AAR to a private Maven library and reference it
In addition to the first solution, the second solution is to upload the third-party AAR file to a private Maven library so that the third-party AAR file can be imported directly through dependencies.
1. Install Apache Maven
1-1. Install the Apache Maven tool
To upload third-party AAR files to maven private libraries, install Apache Maven tools such as:
Download the ZIP package.
After the download is successful, unzip the file to your own installation directory (any directory), such as my installation directory:
/ Users/mei/Applications/apache maven — 3.8.1
Unzip it and no additional installation steps are required.
1-2. Configure environment variables
/ Users/mei/Applications/apache maven – 3.8.1 / bin directory, configuration to the environment variable, so it is convenient to use maven related commands.
After configuring environment variables, run the following command:
mvn -v
If the version information for Apache Maven is printed, the environment variables are configured successfully. For example, the following information is displayed:
Apache Maven 3.8.1 c21c65bdfed0f71a2f2ada8b84da59348c4c5d (05)
Maven home: / Users/mei/Applications/apache Maven — 3.8.1
Java Version: 1.8.0_291, Vendor: Oracle Corporation, Runtime: / Library/Java/JavaVirtualMachines jdk1.8.0 _291. JDK/Contents/Home/jre
Default locale: zh_CN, platform encoding: UTF-8
Version: “10.14.6”, ARCH: “x86_64”, family: “MAC”
2. Upload the third-party AAR file to the private Maven library
Once the Apache Maven libraries are installed, you can upload third-party AAR files using maven-related commands. The steps are as follows:
- In the settings. XML file, configure the Maven user information
- Executing the upload command
2-1. Configure private Maven user information in settings. XML
Uploading an AAR file to a private Maven library requires verification of the user’s information, namely the user name and password.
Go to the Apache Maven installation directory above and open settings. XML in the conf folder, for example:
/ Users/mei/Applications/apache maven – 3.8.1 / conf/Settings. The XML
Add a server label, for example:
<servers>
<server>
<id>releases</id>
<username>android-jinchuang</username>
<password>jinchuang</password>
</server>
<server>
<id>snapshots</id>
<username>android-jinchuang</username>
<password>jinchuang</password>
</server>
</servers>
Copy the code
2-2. Run the upload command
MVN deploy:deploy-file -DgroupId=com.mei. aspectj-dartifactid = aspectj-dversion =1.0.0 -Dpackaging= AAR - Dfile = / Users/sentinel - the annotation - aspectj - 1.4.0. Aar - Durl=https://xxx.com: 8983 / repository/maven - releases -DrepositoryId=releasesCopy the code
Parameter Description:
mvn deploy:deploy-file
-dgroupid =com.mei.aspectj // groupId can be defined as you like
-DartifactId=aspectj // artifactId
-dversion =1.0.0 //version Version number
-dfile =/Users/sentinel-annotation-aspectj-1.4.0.jar // Jar package path and JAR package name
-dpackAgeing =aar // Upload file format
-dpomfile =pom.xml // You must upload pom.xml from jar separately. If you do not rely on other libraries, you do not need to add pom.xml
-drepositoryid =release // Login name and password for connecting to the Maven private server, which are configured in Maven > settings. XML
– Durl=xxx.com: 8983 / repository /… // Upload the private server path and directory
If the above command is executed successfully, the following information is displayed:
This means that the AAR file has been successfully uploaded to Maven. Open the Maven repository and then release our aar file under Releases.
The following can be imported into our SDK via dependency import, as in:
implementation 'com. Mei. Aspectj: aspectj: 1.0.0'
Copy the code
This is a very elegant way to resolve the dependency of third-party AAR files.
Reference blog:
- Package React Native as an AAR
- How to upload components to a private server library (mvn deployCommand usage)
- Run the MVN deploy command to upload the package