Androidスタジオプロジェクト内のKotlinモジュールのテストパッケージ内のメインクラスにアクセスできないようです。以下に示すコードはすべて、AndroidアプリにインポートされるKotlin JVMモジュール内にあることに注意してください。テストパッケージがメインパッケージに定義されたKotlinクラスを読み取らない
ここに私のsrc/main/java
コードです:src/test/java
で
import com.google.gson.annotations.SerializedName
data class Customer(val password1: String,
val password2: String,
@SerializedName("last_name") val lastName: String,
@SerializedName("first_name") val firstName: String,
val email: String)
私のテストコード:
class CreateUser {
@Test
fun createRandomUser() {
val random = Random()
val randomNumber = random.nextInt(10000000)
val customer = Customer("password", "password", "lastName", "firstName", "[email protected]")
}
}
マイbuild.gradle
コードは次のようになります。
buildscript {
ext.kotlin_version = '1.1.4-3'
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'maven'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'kotlin'
repositories {
mavenCentral()
jcenter()
}
dependencies {
// some other compile dependencies
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
testCompile "org.hamcrest:hamcrest-all:1.3"
testCompile 'junit:junit:4.11'
testCompile 'org.mockito:mockito-all:1.9.5'
testCompile "org.jetbrains.kotlin:kotlin-test"
testCompile "org.jetbrains.kotlin:kotlin-test-junit"
}
task javadocJar(type: Jar) {
classifier = 'javadoc'
from javadoc
}
task sourcesJar(type: Jar) {
classifier = 'sources'
from sourceSets.main.allSource
}
artifacts {
archives sourcesJar
archives javadocJar
}
compileKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.6"
}
}
ルートbuild.gradle
ファイルとして見えますが、次のとおりです:
// Top-level build file where you can add configuration options
common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
maven {
url "https://jitpack.io"
credentials { username authToken }
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
ext {
versionName = "0.1.1"
rxJavaVersion = "2.1.3"
okHttpVersion = "3.9.0"
retrofitVersion = "2.3.0"
rxJava2AdapterVersion = "1.0.0"
googleGsonVersion = "2.8.0"
}
私が手にエラーがGradleのは、テストクラスでCustomer
(Unresolved reference: Customer
)を解決することができないということです。主なクラスはテストソースディレクトリには含まれていないようです。それでも、IDEで解決されます。
'src/main/kotlin'と' src/test/kotlin'にそれぞれ定義してみませんか? – Opal
それは違いますか? @Opal – blackpanther
グラデルの視点から見るとよいかもしれません。 GradleはJavaコンパイラを使用してJavaファイルをコンパイルし、kotlinコンパイラを使用してkotlinファイルをコンパイルします。 kotlinファイルが誤って配置されていると、それらはコンパイルのために選択されない可能性があります。 – Opal