Kotlin Android 应用程序编译失败并显示消息:未解析的引用:kotlinx
Kotlin Android App compilation failed with message: Unresolved reference: kotlinx
我正在根据 "Kotlin for Android Developers" 书中的说明测试基于 Kotlin 的基本 Android 应用程序。我正在使用 Android Studio 2.1.1。
我有以下 build.grade(项目:WeatherApp)设置:
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我有以下 build.grade(模块: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>
我正在尝试 运行 我的 MainActivity 使用可用的 Nexus 6 API 23 模拟器。我将 "Tools >> Android >> Enable ADB Integration" 选项选中(我也尝试过将其关闭)。模拟器似乎在工作,但由于构建失败,我的应用程序似乎没有加载。当 运行 连接模拟器时,我在“4: 运行” window:
中看到以下消息
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
我在事件日志中看到以下内容 window:
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 控制台 window 中看到以下内容:
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
如何让应用程序正确编译?
查看以下回复:
您需要将 kotlin buildscript 部分从您的 项目 build.gradle
移动到您的 module build.gradle
文件.
从 R 扩展导入布局文件。
像这样
import kotlindemo.app.com.myapplication.R.layout.activity_main;
import kotlinx.android.synthetic.main.activity_main.*
我正在根据 "Kotlin for Android Developers" 书中的说明测试基于 Kotlin 的基本 Android 应用程序。我正在使用 Android Studio 2.1.1。
我有以下 build.grade(项目:WeatherApp)设置:
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我有以下 build.grade(模块: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>
我正在尝试 运行 我的 MainActivity 使用可用的 Nexus 6 API 23 模拟器。我将 "Tools >> Android >> Enable ADB Integration" 选项选中(我也尝试过将其关闭)。模拟器似乎在工作,但由于构建失败,我的应用程序似乎没有加载。当 运行 连接模拟器时,我在“4: 运行” window:
中看到以下消息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
我在事件日志中看到以下内容 window:
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 控制台 window 中看到以下内容:
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
如何让应用程序正确编译?
查看以下回复:
您需要将 kotlin buildscript 部分从您的 项目 build.gradle
移动到您的 module build.gradle
文件.
从 R 扩展导入布局文件。 像这样
import kotlindemo.app.com.myapplication.R.layout.activity_main;
import kotlinx.android.synthetic.main.activity_main.*