Android Gradle插件3.0构建错误

10

我遇到了迁移到Android gradle插件3.0的问题。

在项目根目录下的build.gradle文件

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
    }
}

Android 应用程序模块build.gradle

buildscript {
    repositories {
        maven { url 'https://maven.fabric.io/public' }
        mavenCentral()
    }

    dependencies {
        classpath 'io.fabric.tools:gradle:1.+'
        classpath testDependencies.spoon
    }
}

repositories {
    maven { url 'https://maven.fabric.io/public' }
}

apply plugin: 'com.android.application'

apply plugin: 'io.fabric'

spoon {
    debug = true
    grantAllPermissions = true
    shard = true
}

android {
    compileSdkVersion 25

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

尝试编译一个项目,但是出现了编译错误。

enter image description here

但是一旦重新添加retrolambda,则该项目成功编译和构建。阅读“已知问题”部分,未找到解决方法。希望有经验的人能提供帮助。

4个回答

5
如果在更新插件后遇到构建错误,请在该页面搜索错误输出或导航到相关主题,并按照说明解决问题。
解决方案:请参考引用页面中的已知问题。
例如,考虑在主项目的build.gradle文件中包含以下类路径依赖项:
buildscript {
    ...
    dependencies {
        classpath "com.android.tools.build:gradle:3.0.1"
        classpath "me.tatarka:gradle-retrolambda:3.7.0"
    }
}

现在考虑以下build.gradle文件,它是组合构建中包含的另一个项目的文件:
buildscript {
    dependencies {
        // Note that the order of plugins differs from that
        // of the main project's build.gradle file. This results
        // in a build error because Gradle registers this as a
        // different classloader.
        classpath "me.tatarka:gradle-retrolambda:3.7.0"
        classpath "com.android.tools.build:gradle:3.0.1"
    }
}

3

你设置了吗?

apply plugin: 'me.tatarka.retrolambda'

me.tatarka:gradle-retrolambda插件作为依赖项添加到您的build.gradle文件中。
buildscript {
   repositories {
      google()
      mavenCentral()
   }

   dependencies {
      classpath "com.android.tools.build:gradle:3.0.1"
      classpath 'me.tatarka:gradle-retrolambda:3.7.0'
   }
}

// Required because retrolambda is on maven central
repositories {
   mavenCentral()
}

然后将源和目标兼容性添加到Java 8中,并在app/build.gradle文件中应用新的插件。

apply plugin: 'com.android.application'
apply plugin: 'me.tatarka.retrolambda' // Add this 

android {
    compileSdkVersion 27
    buildToolsVersion "27.0.0"

    defaultConfig {
        applicationId " "
        minSdkVersion //
        targetSdkVersion //
        versionCode //
        versionName //
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

须知

如果您正在使用 Gradle 3.0.0 或更高版本 的 Android 插件,则您的项目将自动使用插件指定的默认版本构建工具。如果要使用不同版本的构建工具,请在您模块的 build.gradle 中使用 buildToolsVersion 来指定,如下所示:

/**
   * buildToolsVersion specifies the version of the SDK build tools, command-line
   * utilities, and compiler that Gradle should use to build your app. You need to
   * download the build tools using the SDK Manager.
   *
   * If you're using Android plugin 3.0.0 or higher, this property is optional—
   * the plugin uses a recommended version of the build tools by default.
   */
    android {

        compileSdkVersion 26
        buildToolsVersion "26.0.2"
    }

您应该升级您的buildToolsVersion版本。

android { 
    compileSdkVersion 27 
    buildToolsVersion "27.0.0" 

然后执行清理-重建-重启-运行操作。
阅读

2

请确保您的BuildToolsVersion版本号不低于26

新版Android Studio 3.0要求最低的buildToolsVersion版本号为26.0.0,需要在应用模块gradle中更新此版本。

android {
    compileSdkVersion 25
    buildToolsVersion "26.0.3"
}

如果您看到“buildtool not found”错误,则需要安装此构建工具。


不要忘记更新/更改依赖项的appCompat库版本。 - Jigar Patel
构建工具版本是可选的,从3.0 Gradle插件开始,但是我确保了并没有帮助。 - Robertas Setkus

0

请确保这些行在应用程序(module)级别的gradle文件中

buildToolsVersion

defaultConfig 用于应用程序配置

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.example.app"
        minSdkVersion 16
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled true
    }

}

3
抱歉,但是Android Gradle插件3.0不支持低于26.0.2版本的构建工具。 - Robertas Setkus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接