首页 > 解决方案 > 安卓gradle配置java 1.8

问题描述

我正在尝试使用 Java 插件为 Android 项目配置 Gradle。该项目包含一个 Android 库和 Android 应用程序。应用程序使用库来执行一些操作。这是我的应用程序的 gradle 文件:

apply plugin: 'java'
apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion 27
        buildToolsVersion "27.0.2"
        defaultConfig{
            applicationId "xxxxxxxxx"
            minSdkVersion.apiLevel 19
            targetSdkVersion.apiLevel 27
            versionCode 1
            versionName "0.1"

            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file("proguard-rules.pro"))
            }
        }
    }
    compileJava{
        sourceCompatilibity = 1.8
        targetCompatibility = 1.8
    }
}


repositories {
    jcenter()
    maven {
        url 'https://maven.google.com'
    }
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile project(path: ':my_library')

    testCompile 'junit:junit:4.12'

}

gradle编译显示的错误如下:

无法使用创建规则“BaseComponentModelPlugin.Rules#createRemainingTasks(ModelMap, TaskManager, ModelMap) > create(test)”创建“tasks.test”,因为规则“Project..tasks.test()”已注册以创建此模型元素.

我已经读过在 Gradle 中包含 java 插件会自动添加一个测试任务,我会删除它,但我需要使用 Java 1.8 来编译项目。任何有关此错误的帮助/提示将不胜感激,谢谢。

更新: 最后我设法配置了项目。由于我必须使用 gradle-experimental(我正在使用需要它的 NDK 依赖项),我唯一的选择是将最低 API 级别升级到 26 并删除 Java 1.8 兼容性,这是我最终的 build.gradle 文件:

    apply plugin: 'com.android.model.application'

model {
    android {
        compileSdkVersion = 27
        buildToolsVersion = "27.0.2"
        defaultConfig.with {
            applicationId = "xxxxxxxxxx"
            minSdkVersion.apiLevel = 26
            targetSdkVersion.apiLevel = 27
            versionCode = 1
            versionName  = "0.1"

            testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
            multiDexEnabled true
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }
    }
}

repositories {
    jcenter()
    google()
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile project(path: ':my-library')
    compile 'com.android.support:appcompat-v7:27.0.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:design:27.0.2'

    testCompile 'junit:junit:4.12'

    // Brings the new BluetoothLeScanner API to older platforms
    compile 'no.nordicsemi.android.support.v18:scanner:1.1.0'

    // BLE library
    compile 'no.nordicsemi.android:ble:1.2.0'
}

标签: javaandroidgradle

解决方案


推荐阅读