Google’s recommended way to package an AAR
Before we go into the fat-AAR plug-in packaging, let’s talk about the packaging process recommended by Google:
-
Create a Flutter Module project in one of two ways
- Command line interface (CLI)
flutter create -t module --org com.example my_flutter
- Android Studio to create
- Command line interface (CLI)
-
Execute the package command, flutter build aar, which contains multiple ABI’s. If you want to specify the ABI, use the flutter build aar –target-platform XXX platform
-
If the package command is executed without error, the console will have instructions for the location of the final package AAR file and how to use it integrated into your existing Android project. For example:
There is another way to embed the Flutter project into the existing Android project, which is not explained here. If you have a good Command of English, you can read the documents on the website directly. This is clear.
Fat-aar Package Flutter project (Open source Android package AAR plugin)
Look at the headline and I said fat-AAR is an Android packaged AAR plugin. Fat-aar is a packaged AAR plugin for Android projects. What does that have to do with Flutter? Essentially: The principle behind the Google-recommended package AAR and the fat-AAR plug-in package AAR is similar. They are all packaged Android projects into an AAR file
Different engineering modes
- Google packaged the project flutter- Module
- The FAT-AAR package is onePure flutter engineering
This is how the Flutter project was created.
Before we start, let’s talk about the Folder structure of the Flutter project
flutter->open Android module in Android Studio
The fat-AAR integration starts
- Modify the. Android /build.gradle file
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com. Android. Tools. Build: gradle: 3.5.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"// Add the configuration classpath"Com. Kezong: fat - the aar: 1.2.15"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
rootProject.buildDir = '.. /build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Copy the code
2. Modify. Android/app/build gradle
// Add: dependency Boolean isAarLibrary =true
def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
localPropertiesFile.withReader('UTF-8') { reader ->
localProperties.load(reader)
}
}
def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
flutterVersionCode = '1'
}
def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
flutterVersionName = '1.0'} // New: Change the project to Library mode when you need to package an AARif (isAarLibrary) {
apply plugin: 'com.android.library'
} else {
apply plugin: 'com.android.application'
}
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"// New: fat-AAR is introducedif (isAarLibrary) {
apply plugin: 'com.kezong.fat-aar'
}
android {
compileSdkVersion 28
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
lintOptions {
disable 'InvalidPackage'} defaultConfig {// New: No applicationId when an AAR needs to be packagedif(! isAarLibrary) { applicationId"com.flutter.flutter_aar_demo"
}
minSdkVersion 16
targetSdkVersion 28
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
lintOptions {
disable 'InvalidPackage'
}
buildTypes {
release {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig signingConfigs.debug
}
}
}
flutter {
source '.. /.. '
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"// Add libflutter. So and flutter_embedding. Jar to aar along with third-party pluginsif(isAarLibrary) {// Add flutter_embedding"IO. Flutter: flutter_embedding_release: 1.0.0 - ee76268252c22f5c11e82a7b87423ca3982e51a7"// Add comments to all CPU versions of flutter. So ② // Embed"IO. Flutter: arm64_v8a_debug: 1.0.0 - eed171ff3538aa44f061f3768eec3a5908e8e852"
// embed "IO. Flutter: armeabi_v7a_debug: 1.0.0 - eed171ff3538aa44f061f3768eec3a5908e8e852"
// embed "IO. Flutter: x86_64_debug: 1.0.0 - eed171ff3538aa44f061f3768eec3a5908e8e852"
// embed "IO. Flutter: x86_debug: 1.0.0 - eed171ff3538aa44f061f3768eec3a5908e8e852"
embed "IO. Flutter: arm64_v8a_release: 1.0.0 - ee76268252c22f5c11e82a7b87423ca3982e51a7"
embed "IO. Flutter: armeabi_v7a_release: 1.0.0 - ee76268252c22f5c11e82a7b87423ca3982e51a7"/ / add fat - the aar processing flutter packaged in aar in three sides rely on def flutterProjectRoot = rootProject. ProjectDir. ParentFile. ToPath def plugins = new a () Properties() def pluginsFile = new File(flutterProjectRoot.toFile(),'.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}
plugins.each { name, _ ->
embed project(path: ":$name", configuration: 'default')}}}Copy the code
Comment (1) : how to find the latest flutter_embedding. The latest version of the jar file, namely IO. Flutter: flutter_embedding_release: 1.0.0 – ee76268252c22f5c11e82a7b87423ca3982 E51a7,release: later version number. The latest version number is saved here by default. For example, the latest version number is saved in my window.
.gradle\caches\modules-2\files-2.1\ IO. Flutter note ②: Also, flutter. So is stored in. Gradle \caches\modules-2\files-2.1\ IO
Modified. Android/app/AndroidManifest. XML file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flutter.flutter_aar_demo"
xmlns:tools="http://schemas.android.com/tools"> // Only the application tag <application> <! -- Comment from here --> <! -- android:name="io.flutter.app.FlutterApplication"
android:label="flutter_aar_demo"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density |uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"> < ! – Specifies an Android theme to apply to this Activity as soon as the Android process has started. This theme is visible to the userwhilethe Flutter UI initializes. After that, this theme continues to determine the Window background behind the Flutter UI. – > <meta-data android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"/> < ! – Displays an Android View that continues showing the launch screen Drawable until Flutter paints its first frame,then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of Flutter's first frame. – > <meta-data android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>--> <! -- End of comment here --> <! -- Don't delete the meta-data below. This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
Copy the code
Start compiling and packaging
The Gradlew assembleRelease is a time-consuming task to execute under the Android project, so just wait quietly.
Get the AAR file.
Compile files stored in your flutter after the completion of the project directory of the build/app/outputs/arr/app – the aar, get this. Aar file can be directly after throw your classmate to use the Android development. Note that your Android project developer needs to turn on support for Java8
Fat-aar shares the same pros and cons as Google recommendations
Advantages:
- Android developers should not configure the Flutter environment
- Fat-aar: Package all required flutter packages into an AAR file. Including third-party plug-ins under the Flutter project.
Disadvantages:
- Their common disadvantage is the lack of ability to debug flutter code relative to the way source code is integrated.
The last
If there is something wrong, please kindly comment