In hybrid development, packaging the Flutter modules as AArs and providing them to the host project to rely on can greatly reduce the team’s learning cost of Flutter. This is also the first choice of many hybrid development teams. However, how to upload the built AAR package to the Maven repository is a topic worth discussing. Uploading to a remote Maven repository has the following problems:
1. How to automatically upload all AAR and POM files?
When a Flutter module is packaged, it is rare to print only one AAR module. Many of the dependent libraries generate AAR and POM files when the Flutter module is packaged. Manual upload to the Maven repository is not appropriate. So we need an automated script to walk through and upload all aar and POM files under build/host/outputs/repo.
2. How to isolate the risk of the same three-party dependency conflict in different PROJECT FLUTTER modules?
If there are 5 different projects that have a FLUTTER module, all of these modules depend on webview_FLUTTER, but the version may be different. The poM file generated by webview_flutter has the same organization name. If uploaded directly to Maven, other projects may be affected. Aars generated by the same framework referenced in different Flaps need to be isolated to avoid uncertain risks that can be difficult to detect if they occur.
Here are my answers to the appeal questions:
Start by defining a package.properties file in the root of the Flutter project with the following contents:
projectName=demo
packageName=com.example.upload
buildVersion=1.0.5
mavenRepository=http://admin:admin123@localhost:8081/repository/flutter_modules/
mavenSnapshots=http://admin:admin123@localhost:8081/repository/maven-snapshots/
Copy the code
ProjectName: The name of the Flutter project that acts as the artifactId in the Maven repository for isolation.
PackageName: The androidPackage defined in the YAML file. This is the groupId that generates the FLUTTER AAR
BuildVersion: The AAR version to be packaged, which is changed here with each upgrade and is also the version in Maven, which is also better documented by showing the definition in a file.
MavenRepository: The repository address to upload the Release version to
“MavenSnapshots” : Describes the addresses of stores that report Debug versions. Snapshots can be described as “snapshots” and “snapshots”.
After the configuration file is ready, create a file named PackageHelper. sh. The content of the file is as follows. I annotate the key nodes in the script:
#! /bin/bash
# shellcheck disable=SC2086
# shellcheck disable=SC2001
# shellcheck disable=SC2005
# shellcheck disable=SC2016
Import the configuration file
PROPERTIES="package.properties"
# Maven relies on three elements
packageNameV=""
projectNameV=""
buildVersionV=""
# Maven repository address
mavenRepositoryV=""
mavenSnapshotsV=""
# Is the release version
isRelease=The $1
The repository currently in use varies according to isRelease
usedMavenRepo=""
Read configuration file information and perform fault tolerance
if [ -f "$PROPERTIES" ]
then
. $PROPERTIES
packageNameV=$packageName
projectNameV=$projectName
buildVersionV=$buildVersion
if [ -z "$packageNameV" ]; then
echo ERROR:"PackageName is not configured in package.properties!"
exit 1
fi
if [ -z "$projectNameV" ]; then
echo ERROR:"ProjectName is not configured in package.properties file!"
exit 1
fi
if [ -z "$buildVersionV" ]; then
echo ERROR:"BuildVersion is not configured in package.properties!"
exit 1
fi
mavenRepositoryV=$mavenRepository
mavenSnapshotsV=$mavenSnapshots
if [[ -z "$mavenRepositoryV" ]] && [[ -z "$mavenSnapshotsV" ]]
then
echo ERROR:"Please check that the Maven repository information is correctly configured in package.properties!"
exit 1
fi
else
echo ERROR:"Please configure the package.properties file as required and then execute the script!"
exit 1
fi
Select a repository address based on whether it is a release
if [ $isRelease = "true" ]
then
usedMavenRepo=$mavenRepositoryV
echo "Current packaged Release version, version number:$buildVersion,running..."
else
usedMavenRepo=$mavenSnapshotsV
echo "Current package Debug version, fixed version number 1.0-snapshot, running..."
fi
If it is not a release version, the version number is forcibly specified as 1.0-snapshotShowBuildVersion = 1.0 - the SNAPSHOT# Clean up before packing
echo "flutter clear..."
flutter clean
Get dependencies before packaging
echo "flutter pub get..."
flutter pub get
echo "flutter pub get..."
Perform different packaging logic depending on isRelease
if [ $isRelease = "true" ]
then
showBuildVersion=$buildVersionV
Package the release version to avoid waste of resources by generating extra packages
flutter build aar --target-platform android-arm --no-profile --no-debug --build-number "$showBuildVersion"
else
Package debug versions to avoid generating redundant packages and resulting in resource waste
flutter build aar --target-platform android-arm --no-profile --no-release --build-number "$showBuildVersion"
fi
echo "Aar packaged, upload Maven..."
The # method changes the names of all POM files
function renameAllPomArtifactId() {
find build/host/outputs/repo -name "*.pom" | while read -r file
do
sed -i -e "s/>The $1</>$2</g" $file
done
}
For the first time, pass through all the files in the repo folder with the suffix.aar
find build/host/outputs/repo -name "*.aar" | while read -r file
do
The directory where the current AAR file resides
currDirName=$(dirname $file)
aarName=$file
# PoM file corresponding to the current AAR file (aar and POM one-to-one correspondence)
pomName="$currDirName/$(basename $file .aar).pom"
Fetch groupId information from POM file
groupId=$(awk '/
[^<]+
/{gsub(/
|
/,"",$1); print $1; exit; } ' $pomName)
# Read the artifactId information from the POM file, but the artifactId is not configured in properties, it is automatically generated by the compiler
artifactId=$(awk '/
[^<]+
/{gsub(/
|
/,"",$1); print $1; exit; } ' $pomName)
# Modify artifactId to be configured in Properties
renameAllPomArtifactId $artifactId $projectName
done
Repo repo repo repo repo repo repo repo repo repo
# To prevent problems caused by poM artifactId being uploaded to Maven before all modifications are made
find build/host/outputs/repo -name "*.aar" | while read -r file
do
The directory where the current AAR file resides
currDirName=$(dirname $file)
aarName=$file
# PoM file corresponding to the current AAR file (aar and POM one-to-one correspondence)
pomName="$currDirName/$(basename $file .aar).pom"
Fetch groupId information from POM file
groupId=$(awk '/
[^<]+
/{gsub(/
|
/,"",$1); print $1; exit; } ' $pomName)
Read the artifactId information from the POM file
artifactId=$(awk '/
[^<]+
/{gsub(/
|
/,"",$1); print $1; exit; } ' $pomName)
echo "Uploading =$aarName ..."
Run the upload maven repository command
mvn deploy:deploy-file \
-DgroupId=$groupId \
-DartifactId=$projectName \
-Dpackaging=aar \
-Dversion=$showBuildVersion \
-Dfile=$aarName \
-DpomFile=$pomName \
-Durl=$usedMavenRepo
done
Give a friendly prompt when all operations are complete
function finishEcho() {
echo "Upload complete! To import a Flutter module into a host project, do the following: 1. Open the build.gradle file in the root directory of the host project and add the following reference: String storageUrl = system.enw.flutter_storage_base_URL? :"https://storage.googleapis.comMaven {repository} //TODO Delete the maven repository url.The $1" } } maven { url "$storageUrl/download.flutter.io}} 2. Add dependencies to the module that uses Flutter:$2:$3:$4'
//or
releaseImplementation '$2:$3:$4'} 3. Synchronize projects to complete dependencies on Flutter modules. "
}
finishEcho $usedMavenRepo $packageNameV $projectName $showBuildVersion
Copy the code
After introduction, the project structure is as follows:
Run the command in the root directory:
The default value is Debug version./packageHelper.sh false./ / Package release version./packageHelper.sh trueCopy the code
Tips: Check whether the packageHelper.sh file has permission to run
The final result is as follows:
Finally, the dependencies of the FLUTTER module can be happily introduced into the host project
Demo address: flutter_aar_upload