This article is about introducing AArs into flutter
This article only covers android, the iOS framework can be imported directly into podSpec, so this article will skip the list
For those of you who have done Android development, you may know that if the AAR file is imported directly into the Library as a local file, and then the app references the Library, the AAR file will not be found. The disadvantage of this method is that flatDir is used in the app to import the AAR file. You have to configure it in every project, very intrusive
Those familiar with flutter plugin development know that when we developed the Flutter plugin, the plugin was introduced into the project in the form of a Library Module
At this time, if the SDK side only provides AAR for Android, we have the following methods under this general premise:
- Use maven public servers, such as JCenter, or other Maven repositories
- Use the local Maven repository
This article uses the local Maven repository solution
Complete the steps for local Maven
Creating a plug-in project
The first step is definitely to create a Flutter plugin
flutter create -t plugin example_for_flutter_plugin_local_maven
Copy the code
Put the aar
I use the aar of a dandelion as an example:
Download it and place it in the Android directory
Github.com/Pgyer/mvn_r…
Create a folder in your plugin’s Android directory and put the AAR in it, which I’ll call aar here
mkdir android/aar
cdAndroid/aar wget HTTP: / / https://github.com/Pgyer/mvn_repo_pgyer/blob/master/com/pgyersdk/sdk/3.0.9/sdk-3.0.9.aar?raw=true # downloadThe mv SDK - 3.0.9. Aar? raw=true sdk.aar # the name
Copy the code
Create a local Maven repository using the AAR file
- You need a Maven binary, for MAC
$ brew install maven
Other systems search for maven installation - Verify the installation
$ mvn -v
- Using the command line
mvn deploy:deploy-file -Dfile=sdk.aar -Durl="file://." # this step will report an error[ERROR] Failed to execute goal. Org. Apache maven. Plugins: maven deploy - plugin: 2.7: deploy - file (default - cli) on the project standalone-pom: The artifact information is incomplete or not valid: [ERROR] [0]'groupId' is missing.
[ERROR] [1] 'artifactId' is missing.
[ERROR] [2] 'version' is missing.
[ERROR]
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Copy the code
Here -dfile points to aar file, url points to target address, I will deploy in aar folder, so directly use. Represents the current folder, and it tells me that THREE things are missing: group name, item name, and version number
mvn deploy:deploy-file -Dfile=sdk.aar -Durl="file://." -DgroupId="com.pgyer" -DartifactId="sdk" -Dversion="3.0.9"
[INFO] Scanning forprojects... [INFO] [INFO] ------------------< org.apache.maven:standalone-pom >------------------- [INFO] Building Maven Stub Project (No POM) 1 [INFO] --------------------------------[ pom ]--------------------------------- [INFO] [INFO] --- Maven-deploy-plugin :2.7:deploy-file (default-cli) @standalone -pom -- Uploading to remote-repository: file://./com/pgyer/sdk/3.0.9/sdk-3.0.9.aar Uploaded to remote - repository: file://./com/pgyer/sdk/3.0.9/sdk-3.0.9.aar (134 kB at 3.3 MB/s) Uploading to remote - repository: file://./com/pgyer/sdk/3.0.9/sdk-3.0.9.pom Uploaded to remote - repository: file://./com/pgyer/sdk/3.0.9/sdk-3.0.9.pom at 208 kB/s (415 B) Downloading from remote - repository: file://./com/pgyer/sdk/maven-metadata.xml Downloaded from remote-repository: file://./com/pgyer/sdk/maven-metadata.xml (292 B at 32 kB/s) Uploading to remote-repository: file://./com/pgyer/sdk/maven-metadata.xml Uploaded to remote-repository: file://./com/pgyer/sdk/maven-metadata.xml (292 B at 146 kB/s) [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- [INFO] Total time: 0.407 s [INFO] Finished at: 2020-03-30T16:00:25+08:00 [INFO] ------------------------------------------------------------------------Copy the code
That’s it. All the files are generated
Tree.. ├ ─ ─ com │ └ ─ ─ pgyer │ └ ─ ─ the SDK │ ├ ─ ─ 3.0.9 │ │ ├ ─ ─ the SDK - 3.0.9. Aar │ │ ├ ─ ─ the SDK - 3.0.9. Aar. Md5 │ │ ├ ─ ─ The SDK - 3.0.9. Aar. Sha1 │ │ ├ ─ ─ the SDK - 3.0.9. Pom │ │ ├ ─ ─ the SDK - 3.0.9. Pom. Md5 │ │ └ ─ ─ the SDK - 3.0.9. Pom. Sha1 │ ├ ─ ─ maven - metadata. XML │ ├ ─ ─ maven - metadata. XML. Md5 │ └ ─ ─ maven - metadata. XML. Sha1 └ ─ ─ SDK. The aarCopy the code
This is the standard Maven directory structure
The SDK. aar is useless and can be removed
Edit the Gradle file
Open the project Example/Android in Studio
// Define a method to get the dir of the current moudle
def getCurrentProjectDir() {
String result = ""
rootProject.allprojects { project ->
if (project.properties.get("identityPath").toString() == ":example_for_flutter_plugin_local_maven") { // Here is the convention of flutter, the module name of the plugin is the plugin name, and: is the convention of Gradle.
result = project.properties.get("projectDir").toString()
}
}
return result
}
rootProject.allprojects {
// This closure loops through all projects, and we make the repository available to all modules
def dir = getCurrentProjectDir()
repositories {
google()
jcenter()
maven { // Add this to the local repository directory
url "$dir/aar"
}
}
}
dependencies {
implementation "Com. Pgyer: SDK: 3.0.9" Sync Project with Gradle File to refresh the project. API or implementation depends on your situation
}
Copy the code
Afterword.
This article uses Gradle to configure Maven in a non-intrusive manner for main items
In this way, the main project can refer to the AAR for any project. Of course, this solution also applies to non-FLUTTER projects
Warehouse address: Github
The above