Basic introduction
The main difference between Flutter and react Native and weeX frameworks is that Flutter uses GDI frame-by-frame rendering instead of native controls. Flutter is very similar to DirectUI in the Windows era. This article describes how to add Flutter to an existing Android project so that some of the new features adopt Flutter
The basic idea of porting is to create a new Flutter feature and compare it to the Android project to see what the differences are and copy some code from the Flutter project to the Android project
The directory structure of an Android Studio project looks like this
Project ├ ─ local. The properties ├ ─ build. Gradle ├ ─ Settings. Gradle ├ ─ app │ ├ ─ build │ ├ ─ libs │ └ ─ SRC └ ─ gradle └ ─ wrapperCopy the code
The directory structure of a flutter is:
├─ Build ├─ios ├─lib ├─ pubspec.yamlCopy the code
1, so the first step is to change the project directory name to Android project change 2. Copy pubspec.yaml and lib files to side with Android 3. Modify the androidmanifest.xml file
<application
android:name="io.flutter.app.FlutterApplication"Add this line android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Copy the code
If the modification fails, the following error will be reported
E/FlutterMain(13694): Flutter initialization failed.
E/FlutterMain(13694): java.lang.NullPointerException: Attempt to invoke virtual method 'void io.flutter.view.ResourceExtractor.waitForCompletion()' on a null object reference
Copy the code
4. Modify the Settings. Gradle
include ':app'def flutterProjectRoot = rootProject.projectDir.parentFile.toPath() def plugins = new Properties() def pluginsFile = new File(flutterProjectRoot.toFile(),'.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}
plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}
Copy the code
If this step change failure can prompt GeneratedPluginRegistrant file to compile error: error: package IO. Flutter. Plugins. Pathprovider does not exist error: Package IO. Flutter. Plugins. Sharedpreferences does not exist
5. Modify project build.gradle by adding the following code:
rootProject.buildDir = '.. /build'
subprojects {
project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
project.evaluationDependsOn(':app')}Copy the code
6. Add this section to build. Gradle of your app
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.")
}
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android {
compileSdkVersion 27
defaultConfig {
applicationId "flutter_app2.yourcompany.com.myapplication"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}}} this particular forget flutter {source '.. /.. '
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com. Android. Support: appcompat - v7:27.1.1'
implementation 'com. Android. Support. The constraint, the constraint - layout: 1.1.0'
testImplementation 'junit: junit: 4.12'
androidTestImplementation 'com. Android. Support. Test: runner: 1.0.2'
androidTestImplementation 'com. Android. Support. Test. Espresso: espresso - core: 3.0.2'
}
Copy the code
7. Modify the path for adding local.properties to FlutterSDK
ndk.dir=E\:\\software\\android-sdk-windows\\ndk-bundle
sdk.dir=E\:\\software\\android-sdk-windows
flutter.sdk=D:\\flutter
flutter.buildMode=debug
Copy the code
Then, modify mainactivity.java
import android.os.Bundle; import io.flutter.app.FlutterActivity; import io.flutter.plugins.GeneratedPluginRegistrant; public class MainActivity extends FlutterActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GeneratedPluginRegistrant.registerWith(this); }}Copy the code
With these eight changes, you can run Fluuter code in native Android projects.