Android Studio 引入flutter项目,gradle 构建时报错:Plugin with id ‘com.android.library‘ not found.

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.

在Gradle中,使用plugins和apply plugin有什么区别?

关于创建Android Library所需要知道的一切

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值