Android gradle配置Java 1.8

时间:2018-07-09 14:20:48

标签: java android gradle

我正在尝试使用Android项目的java插件配置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编译中显示的错误如下:

  

无法使用创建规则创建“ tasks.test”   'BaseComponentModelPlugin.Rules#createRemainingTasks(ModelMap,   TaskManager,ModelMap)> create(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'
}

0 个答案:

没有答案