Mixed development of Android and flutter

The android studio version I use here is 2020.3.1; flutter version 2.5.3. Many tutorial versions I searched on the Internet before are different. The new version of IDE and SDK has caused me to encounter many pitfalls, so I will sort it out here.

1. Create a project

1. Click File->New->New Flutter Project in the Android project. File->New->New Flutter Project

  1. Select Flutte in the pop-up panel, select the path where the Flutter SDK is located, and click Next.

  1. Then enter the Project name, Description, and Organization in the pop-up panel. When selecting location, it is recommended to choose to build it in the same directory as android . Select Flutter Module as the type, and click Finish to complete the creation.

2. The Android project is associated with the Flutter Module

As mentioned on the official website of Flutter, it has been established here, but the latest version of Android Studio I use is different from the operation steps in the official website, and the project association has not been completed. Whether there is a connection can be checked in the seeings.gradle of the native project to see if there is the following code, if not, add it manually. My Bingding here will report an error but it does not affect Sync. It may be a bug of flutter.
setBinding(new Binding([gradle: this]))
evaluate(new File(
        settingsDir,
        '../fluttermodule/.android/include_flutter.groovy'
))

Then add in app's build.gradle

//不管你flutter module名称是啥,都用flutter!!!
implementation project(':flutter')

If the module is created correctly, there will be in the configuration file, if not, please delete and rebuild a module

三、Failed to apply plugin class ‘FlutterPlugin’

At this point, the project association should have been completed, but I have tried N many times and every time I get an error, the error message is:
  • Caused by: org.gradle.api.InvalidUserCodeException: Build was configured to prefer settings repositories over project repositories but repository ‘maven’ was added by plugin class ‘FlutterPlugin’

  • aused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin class ‘FlutterPlugin’.
    解决方案为:
    1.把seetings.gradle中的(RepositoriesMode.FAIL_ON_PROJECT_REPOS)改为(RepositoriesMode.PREFER_PROJECT)

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)
    repositories {
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/public' }
        maven { url 'https://maven.aliyun.com/repository/google' }
        maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
    }
}

2.在project的build.gradle中添加

allprojects {
        repositories {
            maven { url 'https://maven.aliyun.com/repository/public' }
            maven { url 'https://maven.aliyun.com/repository/public' }
            maven { url 'https://maven.aliyun.com/repository/google' }
            maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
        }
    }

3.重新build,此时就没有报错了。

链接:https://www.jianshu.com/p/f944e72ca0ed

Guess you like

Origin blog.csdn.net/Goals1989/article/details/129239899