天天看點

Android Studio 引入flutter項目,gradle 建構時報錯:Plugin with id ‘com.android.library‘ not found.apply plugin: ‘com.android.application’ 與apply plugin: 'com.android.library’的差別

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行的代碼是:

即在使用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.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所需要知道的一切