Android原生项目中执行flutter create -t module module_name
新建了个flutter module(与app这个module同级的odule),然后gradle build的时候报错:
A problem occurred evaluating project ':flutter'.
> Plugin with id 'com.android.library' not found.
报错定位在Project的子Module的build.gradle的apply plugin: 'com.android.library'
这句代码。
原因:新建的flutter module需要用到com.android.library,而在最外面的Project的build.gradle没有相关依赖:
解决:
在Project的build.gradle脚本中在添加
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.6.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
即可。
官方文档写了,使用plugin要两个步骤:
1.解析(resolve)到指定plugin的代码位置;
2.应用(apply)plugin,即调用plugin。如果是脚本plugin,就执行脚本,如果是二进制plugin,就执行Plugin.apply(T)
比如:如果在build.gradle文件中没有写:
buildscript {
repositories {
google()
maven {
url "https://repo1.maven.org/maven2"
}
jcenter {
content {
includeVersion 'org.jetbrains.trove4j', 'trove4j', '20160824'
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
}
}
...
apply plugin: 'com.android.application'
apply plugin: 'com.google.protobuf'
...
那么gradle build时就会报错:
1: Task failed with an exception.
-----------
* Where:
Build file '/xxx/StudioProjects/android_project/app/build.gradle' line: 25
* What went wrong:
A problem occurred evaluating project ':app'.
> Plugin with id 'com.google.protobuf' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
报错定位25行的代码是:
apply plugin: 'com.google.protobuf'
即在使用protobuf插件时报错。
需要加上:classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10'
,如下:
buildscript {
repositories {
google()
maven {
url "https://repo1.maven.org/maven2"
}
jcenter {
content {
includeVersion 'org.jetbrains.trove4j', 'trove4j', '20160824'
}
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.3'
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.10'
}
}
然后再使用
apply plugin: 'com.google.protobuf'
就不会报错。
apply plugin: ‘com.android.application’ 与apply plugin: 'com.android.library’的区别
apply plugin: 'com.android.application'
表示使用gradle插件的应用(application)插件
apply plugin: 'com.android.library'
表示使用gradle插件的库(library)插件
参考:
Plugin with id ‘com.android.library’ not found.