web-dev-qa-db-ja.com

Kotlin Androidアプリのコンパイルがメッセージで失敗しました:未解決の参照:kotlinx

「Kotlinfor Android Developers」の説明に基づいて、基本的なKotlinベースのAndroidアプリをテストしています。 Android Studio2.1.1を使用しています。

次のbuild.grade(プロジェクト:WeatherApp)セットアップがあります。

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

次のbuild.grade(Module:App)セットアップがあります。

apply plugin: 'com.Android.application'
apply plugin: 'kotlin-Android'
apply plugin: 'kotlin-Android-extensions'

buildscript {
    ext.support_version = '23.1.1'
    ext.kotlin_version = '1.0.2'
    ext.anko_version = '0.8.2'
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.Android.tools.build:gradle:2.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-Android-extensions:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

Android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "com.qtimemedia.weatherapp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets {
        main.Java.srcDirs += 'src/main/kotlin'
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.Android.support:appcompat-v7:23.4.0'
    compile 'org.jetbrains.anko:anko-sdk23:0.8.3'
    compile 'org.jetbrains.anko:anko-appcompat-v7:0.8.3'
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
repositories {
    mavenCentral()
}

私は次のMainActivity.ktコードを持っています:

package com.qtimemedia.weatherapp

import Android.support.v7.app.AppCompatActivity
import Android.os.Bundle
import kotlinx.Android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        message.text = "Hello Kotlin!"
    }
}

これが私のactivity_main.xmlコードです:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:Android="http://schemas.Android.com/apk/res/Android"
    xmlns:tools="http://schemas.Android.com/tools"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:paddingBottom="@dimen/activity_vertical_margin"
    Android:paddingLeft="@dimen/activity_horizontal_margin"
    Android:paddingRight="@dimen/activity_horizontal_margin"
    Android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.qtimemedia.weatherapp.MainActivity">

    <TextView
        Android:id="@+id/message"
        Android:layout_width="wrap_content"
        Android:layout_height="wrap_content"
        Android:text="@string/hello_world" />
</RelativeLayout>

これが私のstrings.xmlコードです:

<resources>
    <string name="app_name">WeatherApp</string>
    <string name="hello_world">"Hello World!"</string>
</resources>

利用可能なNexus6 API23エミュレーターを使用してMainActivityを実行しようとしています。 [ツール] >> Android >> [ADB統合を有効にする]オプションをオンにしたままにしました(これもオフにして試しました)。エミュレーターは機能しているように見えますが、ビルドが失敗したため、アプリが読み込まれていないようです。エミュレータを実行すると、「4:実行」ウィンドウに次のメッセージが表示されます。

C:\Users\Owner\AppData\Local\Android\sdk\tools\emulator.exe -netdelay none -netspeed full -avd Nexus_6_API_23
Warning: requested ram_size 1536M too big, reduced to 1024M
emulator: WARNING: Crash service did not start
emulator: WARNING: VM heap size set below hardware specified minimum of 384MB
emulator: WARNING: Setting VM heap size to 384MB
Hax is enabled
Hax ram_size 0x40000000
HAX is working and emulator runs in fast virt mode.
console on port 5554, ADB on port 5555

[イベントログ]ウィンドウに次のように表示されます。

11:26:05 PM Gradle sync started
11:26:15 PM Gradle sync completed
11:26:16 PM Executing tasks: [:app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies]
11:26:34 PM Gradle build finished in 18s 266ms
11:35:55 PM Executing tasks: [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug]
11:39:04 PM Gradle build finished with 3 error(s) in 3m 8s 551ms

Gradleコンソールウィンドウに次のように表示されます。

e: C:\Users\Owner\AndroidStudioProjects\WeatherApp\app\src\main\Java\com\qtimemedia\weatherapp\MainActivity.kt: (5, 8): Unresolved reference: kotlinx
e: C:\Users\Owner\AndroidStudioProjects\WeatherApp\app\src\main\Java\com\qtimemedia\weatherapp\MainActivity.kt: (12, 9): Unresolved reference: message

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3 mins 5.237 secs

[メッセージ]セクションに次のように表示されます。

C:\Users\Owner\AndroidStudioProjects\WeatherApp\app\src\main\Java\com\qtimemedia\weatherapp\MainActivity.kt
Error:(5, 8) Unresolved reference: kotlinx
Error:(12, 9) Unresolved reference: message
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
Information:BUILD FAILED
Information:Total time: 3 mins 5.237 secs
Information:3 errors
Information:0 warnings
Information:See complete output in console

アプリを正しくコンパイルするにはどうすればよいですか?

5
MLev

次の応答を参照してください: https://stackoverflow.com/a/36526153/25651

Kotlin buildscriptセクションをプロジェクトから移動する必要がありますbuild.gradle to yourmodulebuild.gradleファイル。

9
hleb