Android integrated Flutter module stepping on the pit (supplemented content forwarded)

The original link Android integrated Flutter module stepping on the road , made the last addition


foreword

Recently, I want to integrate the Flutter module   in my project according to the official website . There are many problems in this process. I will summarize and record the problems encountered in this process and how to deal with them.


1. Create flutter module

My Android Studio version: 2021.1.1
insert image description here
  This version directly uses File -> New -> new module... The way to directly create a flutter model The official website also provides several ways, here I choose to use File -> New -> new Flutter The Project method directly uses Android studio to create modules for us. Remember to follow the flutter and dart plug-ins
insert image description here
insert image description here
insert image description here
  to understand through the above figure. Creating a module is very simple. Just select the module as the Project Type, and then change the name. Others can be ignored. It is better to have the module in the same directory as the Android project. Because the way I choose is the option B recommended by the official website - relying on the source code of the module to integrate the module.

Dependency scheme: Portal

  After the module is created, we can configure it according to the official website plan B, the configuration is very simple

insert image description here

2. Failed to apply plugin class ‘FlutterPlugin’.

  When we copied it according to the official website, and then clicked to synchronize, we thought everything would be fine, but the first error occurred:

Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin class 'FlutterPlugin'.

  
  
   
   

  Looking at the error message, it probably means that the FlutterPlugin cannot be applied. What the hell is this? After some data searching, it turned out that after gradle6.8, the following configuration was added under the dependencyResolutionManagement of settings.gradle:

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

  
  
   
   

What the hell is this? After searching, it is found that there are three modes:

RepositoriesMode configures how to set up the warehouse during construction. There are three ways in total:
FAIL_ON_PROJECT_REPOS
means that if the project has a separate warehouse, or a project plug-in has set up a warehouse, the build will directly report an error and throw an exception.
PREFER_PROJECT
means that if the project has a separate warehouse, it will be used first For project configuration, ignore
the PREFER_SETTINGS in the settings
to indicate that any warehouse that is set individually by the project or by a plug-in will be ignored

  Looking back at our code, the settings.gradle is configured with FAIL_ON_PROJECT_REPOS, and the Flutter plugin has set up the repository separately, so it will build an error, so we need to change FAIL_ON_PROJECT_REPOS to PREFER_PROJECT, and then we will try to compile.

really! Compilation passed.
Switch to the project to have a look. Everything is normal.
insert image description here
  Binding will report an error here. Importing several classes according to the prompts will not work. I will ignore it here (nothing will be imported, and it will not affect the later stage). It can also be compiled. If anyone knows, comment let me know
insert image description here

  In line with the prudence of programmers, we will definitely run the project at this time to see if there are any problems. Just when I thought there was no mistake when I was fishing, a lot of mistakes came one after another... The
reason for the mistakes is basically that the download address of the dependency cannot be found, and I am full of doubts. I am not both Google and Ali mirrors. How can there be such a problem after configuration?
insert image description here
insert image description here
After looking for information and thinking for a long time, it turned out that I set the

Reason for repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)

  It means that if the project has a separate warehouse, the project configuration (that is, module) will be used first, and the ones in the settings will be ignored. From this sentence, I understand that dependent downloads are found according to the downloads configured in the module warehouse.
  Because of gradle adjustment, our Android warehouse configuration is in settings.gradle, but because PREFER_PROJECT is set, settings.gradle is ignored. Now that we know the reason, how to solve it? So I searched on the Internet and found that although the gradle file of the project has been adjusted, the warehouse can still be configured as before, so I added the configuration shown below, successfully solved the problem and compiled and installed successfully

allprojects {
    repositories {
        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' }
        google()
        mavenCentral()
    }
}

  
  
   
   

The complete code is as follows:
insert image description here

  Why set the above code in project.gradle? This is because configuring the native project in this way will pull the code from the above configuration (normally from stings.gradle but it is ignored because PREFER_PROJECT is set), and the module will pull dependencies from its own warehouse configuration, which are irrelevant and conflict-free.

Summarize

  The first error is because the settings.gradle has been adjusted. I am not very familiar with this area, so it will cause the problem to be solved slowly.
  The second question is also extended from the first question, because the setting of PREFER_PROJECT causes settings.gradle to be ignored, which causes the Android project to download dependencies from the warehouse configured by settings.gradle instead of module The warehouse to download, resulting in no download to the package that needs to be depended on


The above content is reproduced from: Android integrated Flutter module stepping on the road to pitfalls . In addition, you also need to add dependencies to the app's build.gradle:

dependencies {
    
    
  implementation project(':flutter')
}

Otherwise, an error like this will be reported:

insert image description here


foreword

Recently, I want to integrate the Flutter module   in my project according to the official website . There are many problems in this process. I will summarize and record the problems encountered in this process and how to deal with them.


1. Create flutter module

My Android Studio version: 2021.1.1
insert image description here
  This version directly uses File -> New -> new module... The way to directly create a flutter model The official website also provides several ways, here I choose to use File -> New -> new Flutter The Project method directly uses Android studio to create modules for us. Remember to follow the flutter and dart plug-ins
insert image description here
insert image description here
insert image description here
  to understand through the above figure. Creating a module is very simple. Just select the module as the Project Type, and then change the name. Others can be ignored. It is better to have the module in the same directory as the Android project. Because the way I choose is the option B recommended by the official website - relying on the source code of the module to integrate the module.

Dependency scheme: Portal

  After the module is created, we can configure it according to the official website plan B, the configuration is very simple

insert image description here

2. Failed to apply plugin class ‘FlutterPlugin’.

  When we copied it according to the official website, and then clicked to synchronize, we thought everything would be fine, but the first error occurred:

Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin class 'FlutterPlugin'.

  
  
 
 

  Looking at the error message, it probably means that the FlutterPlugin cannot be applied. What the hell is this? After some data searching, it turned out that after gradle6.8, the following configuration was added under the dependencyResolutionManagement of settings.gradle:

repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)

  
  
 
 

What the hell is this? After searching, it is found that there are three modes:

RepositoriesMode configures how to set up the warehouse during construction. There are three ways in total:
FAIL_ON_PROJECT_REPOS
means that if the project has a separate warehouse, or a project plug-in has set up a warehouse, the build will directly report an error and throw an exception.
PREFER_PROJECT
means that if the project has a separate warehouse, it will be used first For project configuration, ignore
the PREFER_SETTINGS in the settings
to indicate that any warehouse that is set individually by the project or by a plug-in will be ignored

  Looking back at our code, the settings.gradle is configured with FAIL_ON_PROJECT_REPOS, and the Flutter plugin has set up the repository separately, so it will build an error, so we need to change FAIL_ON_PROJECT_REPOS to PREFER_PROJECT, and then we will try to compile.

really! Compilation passed.
Switch to the project to have a look. Everything is normal.
insert image description here
  Binding will report an error here. Importing several classes according to the prompts will not work. I will ignore it here (nothing will be imported, and it will not affect the later stage). It can also be compiled. If anyone knows, comment let me know
insert image description here

  In line with the prudence of programmers, we will definitely run the project at this time to see if there are any problems. Just when I thought there was no mistake when I was fishing, a lot of mistakes came one after another... The
reason for the mistakes is basically that the download address of the dependency cannot be found, and I am full of doubts. I am not both Google and Ali mirrors. How can there be such a problem after configuration?
insert image description here
insert image description here
After looking for information and thinking for a long time, it turned out that I set the

Reason for repositoriesMode.set(RepositoriesMode.PREFER_PROJECT)

  It means that if the project has a separate warehouse, the project configuration (that is, module) will be used first, and the ones in the settings will be ignored. From this sentence, I understand that dependent downloads are found according to the downloads configured in the module warehouse.
  Because of gradle adjustment, our Android warehouse configuration is in settings.gradle, but because PREFER_PROJECT is set, settings.gradle is ignored. Now that we know the reason, how to solve it? So I searched on the Internet and found that although the gradle file of the project has been adjusted, the warehouse can still be configured as before, so I added the configuration shown below, successfully solved the problem and compiled and installed successfully

allprojects {
    repositories {
        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' }
        google()
        mavenCentral()
    }
}

  
  
 
 

The complete code is as follows:
insert image description here

  Why set the above code in project.gradle? This is because configuring the native project in this way will pull the code from the above configuration (normally from stings.gradle but it is ignored because PREFER_PROJECT is set), and the module will pull dependencies from its own warehouse configuration, which are irrelevant and conflict-free.

Summarize

  The first error is because the settings.gradle has been adjusted. I am not very familiar with this area, so it will cause the problem to be solved slowly.
  The second question is also extended from the first question, because the setting of PREFER_PROJECT causes settings.gradle to be ignored, which causes the Android project to download dependencies from the warehouse configured by settings.gradle instead of module The warehouse to download, resulting in no download to the package that needs to be depended on

Guess you like

Origin blog.csdn.net/qq_36162336/article/details/129854348