仅使用 Maven 使用 Kotlin 的 Android 资源扩展
Using Kotlin's Android resource extensions using Maven only
我想在我的基于 Maven 的项目中使用 Kotlin Android extensions,但是尽管将 kotlin-android-extensions
添加到我的 Maven 插件集,它还是不行。
我的设置如下:
.
├── AndroidManifest.xml
├── pom.xml
├── res
│ └── layout
│ └── main_activity.xml
└── src
└── activity.kt
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.KotlinAndroid"
android:versionCode="1"
android:versionName="0.1-SNAPSHOT">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application android:label="Hello Kotlin">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.KotlinAndroid</groupId>
<artifactId>hello-kotlin</artifactId>
<packaging>apk</packaging>
<name>hello-kotlin</name>
<version>0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.2</version>
<configuration>
<sdk>
<platform>19</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.0.0</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-android</artifactId>
<version>1.0.0</version>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-android-extensions</artifactId>
<version>1.0.0</version>
</plugin>
<!-- I needed to add this plugin otherwise my Kotlin source
files are not found -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<version>1.10</version>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
res/layout/main_activity.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Hello World!"
android:id="@+id/textView" />
</LinearLayout>
src/activity.kt
:
package com.example.KotlinAndroid.activity
import android.os.Bundle
import android.app.Activity
import com.example.KotlinAndroid.R
class MainActivity() : Activity() {
protected override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}
}
所以这一切都有效,直到我将 activity.kt
更改为导入 kotlinx.android.synthetic.main.main_activity.*
而不是 import com.example.KotlinAndroid.R
,然后失败
[ERROR] hello-kotlin/src/activity.kt
: (6, 8)
Unresolved reference: kotlinx
[ERROR] hello-kotlin/src/activity.kt
: (12, 24)
Unresolved reference: R
我发现这个问题的所有解决方案仅适用于 Gradle 构建,在这种情况下显然添加 kotlin-android-extensions
插件就足够了。但是,在我的纯 Maven 构建中,我的插件列表中已经有 kotlin-android-extensions
(请参阅 pom.xml
文件);然而 Kotlin 没有生成所需的 kotlinx.*
类.
kotlin-android-extensions 现在是 kotlin-gradle 插件的一部分。 kotlin-maven 插件现在根本不支持 android。您的智能设备中有一个 issue 您可以投票给它。
我想在我的基于 Maven 的项目中使用 Kotlin Android extensions,但是尽管将 kotlin-android-extensions
添加到我的 Maven 插件集,它还是不行。
我的设置如下:
.
├── AndroidManifest.xml
├── pom.xml
├── res
│ └── layout
│ └── main_activity.xml
└── src
└── activity.kt
AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.KotlinAndroid"
android:versionCode="1"
android:versionName="0.1-SNAPSHOT">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application android:label="Hello Kotlin">
<activity android:name=".activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
pom.xml
:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.KotlinAndroid</groupId>
<artifactId>hello-kotlin</artifactId>
<packaging>apk</packaging>
<name>hello-kotlin</name>
<version>0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.8.2</version>
<configuration>
<sdk>
<platform>19</platform>
</sdk>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>1.0.0</version>
<executions>
<execution>
<id>compile</id>
<phase>process-sources</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>process-test-sources</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-android</artifactId>
<version>1.0.0</version>
</plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-android-extensions</artifactId>
<version>1.0.0</version>
</plugin>
<!-- I needed to add this plugin otherwise my Kotlin source
files are not found -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<version>1.10</version>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>src</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
res/layout/main_activity.xml
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Hello World!"
android:id="@+id/textView" />
</LinearLayout>
src/activity.kt
:
package com.example.KotlinAndroid.activity
import android.os.Bundle
import android.app.Activity
import com.example.KotlinAndroid.R
class MainActivity() : Activity() {
protected override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
}
}
所以这一切都有效,直到我将 activity.kt
更改为导入 kotlinx.android.synthetic.main.main_activity.*
而不是 import com.example.KotlinAndroid.R
,然后失败
[ERROR]
hello-kotlin/src/activity.kt
: (6, 8) Unresolved reference:kotlinx
[ERROR]
hello-kotlin/src/activity.kt
: (12, 24) Unresolved reference:R
我发现这个问题的所有解决方案仅适用于 Gradle 构建,在这种情况下显然添加 kotlin-android-extensions
插件就足够了。但是,在我的纯 Maven 构建中,我的插件列表中已经有 kotlin-android-extensions
(请参阅 pom.xml
文件);然而 Kotlin 没有生成所需的 kotlinx.*
类.
kotlin-android-extensions 现在是 kotlin-gradle 插件的一部分。 kotlin-maven 插件现在根本不支持 android。您的智能设备中有一个 issue 您可以投票给它。