匯入專案時遇到的plugin with id com.android.application not found問題解決方案
阿新 • • 發佈:2019-02-10
出現這個問題主要是因為缺少build.gradle檔案導致的。因為一般專案會有兩個build.gradle檔案。其作用如下:
build.gradle(Project: xxxx)
該檔案是整個工程編譯的全域性檔案,優先順序最高
原始碼如下:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
// 優先順序最高的build檔案,先於Module的build.gradle執行
buildscript {
repositories {
jcenter() //指定maven映象,下同
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0' //指定classpath
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir //指定clean的時候清除掉指定目錄
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
從上述的註釋可以看出該檔案的作用:
1. 編譯工程最頂級的檔案,優先順序最高;
2. 指定maven倉庫地址,gradle相關的包是從maven拉取下來的;
3. 指定 classpath,不然無法找到某些類,標題出現的問題就是這個原因。
build.gradle(Module: xxx)
該檔案是某個Module編譯時候用到的檔案
原始碼如下:
//注意:Gradle是通過外掛來區分是可執行的工程還是libiary工程,下面表示可執行工程
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "mashen.graphicsdemo"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
這裡面的配置都是大家耳熟能詳的了,重點看看第一行,apply plugin: ‘com.android.application’ 表示當前Module是可執行工程。 然而找不到這個外掛,原因就是 com.android.application 來源於 com.android.tools.build:gradle:2.2.0 。 這下估計都明白了!
解決方案:
將build.gradle裡面的配置指令碼拷貝到Module下的 build.gradle裡面,也就是下面的指令碼:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
//注意:更換成自己的AS的版本
}
}
allprojects {
repositories {
jcenter()
}
}
並且新建一個gradle.properties檔案,並加上一句android.overridePathCheck=true,然後再重新編譯即可