Android Studio 在 Make Project 期间不会为 Java 库模块编译测试类

Ale*_*nov 5 intellij-idea gradle android-studio

我在 Android Studio 2.2.3 中有一个多项目 Gradle 构建,包含 Java 库 ( coreand codegen) 和 Android 库 ( android) 模块。Make Project在 Gradle 控制台中显示已执行的任务:

Executing tasks: [:android:generateDebugSources, :android:generateDebugAndroidTestSources, :android:mockableAndroidJar, :android:prepareDebugUnitTestDependencies, :android:compileDebugSources, :android:compileDebugAndroidTestSources, :android:compileDebugUnitTestSources, :codegen:compileJava, :core:compileJava]
Run Code Online (Sandbox Code Playgroud)

但是,compileJava编译只有主类路径,而不是测试,以及testClasses对任务corecodegen未列出。另一方面,./gradlew build确实运行testClasses

如何让 Android Studio 也为 Java 库模块编译测试?

settings.gradle

include 'core'
include 'codegen'
include 'android'
Run Code Online (Sandbox Code Playgroud)

主要build.gradle

buildscript {
    repositories {
        maven {
            url "http://10.122.85.159:9081/nexus/content/groups/public"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        maven {
            url "http://10.122.85.159:9081/nexus/content/groups/public"
        }
    }
}

dependencies {
}
Run Code Online (Sandbox Code Playgroud)

core/build.gradle

apply plugin: 'java'

version = '1.0.0'
sourceCompatibility = '1.7'

sourceSets {
    generated {
        java {
            srcDirs = ['src/generated/java']
        }
    }
}

test {
    filter {
    }
}

dependencies {
    compile fileTree(dir: '../lib', include: '*.jar')
    generatedCompile project(':core')
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
    compile 'org.javatuples:javatuples:1.2'
}
Run Code Online (Sandbox Code Playgroud)

codegen/build.gradle

apply plugin: 'java'

dependencies {
    compile project(':core')
    compile group: 'com.squareup', name: 'javapoet', version: '1.8.0'
}
Run Code Online (Sandbox Code Playgroud)

android/build.gradle

apply plugin: 'com.android.library'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.3"

    defaultConfig {
        minSdkVersion 24
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        disable 'OldTargetApi'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile project(':core')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:24.2.1'
    testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)