2015-12-07 33 views
11

JavaコードHomePage.classをKotlinに変換しようとしています。私はKotlin.org上の指示に従っています:Kotlin Foo :: class.java "Unresolved Reference:Java"エラー

getClass()

To retrieve the type information from an object, we use the javaClass extension property.

val fooClass = foo.javaClass

Instead of Java’s Foo.class use Foo::class.java .

val fooClass = Foo::class.java

私は(Androidの中で)AppCompatActivityを拡張するクラスと呼ばれるホームページを持っています。私はAndroid Studioを使用しています。私はHomePage::class.javaをやってみました、それはエラーがあります:Unresolved reference: java

enter image description here

どのように私はこの作業を取得しますか?

答えて

10

問題は、あなたがKotlinの反射機能のために必要であったreflection librariesに依存するのを忘れた可能性が最も高いです。

On the Java platform, the runtime component required for using the reflection features is distributed as a separate JAR file (kotlin-reflect.jar). This is done to reduce the required size of the runtime library for applications that do not use reflection features. If you do use reflection, please make sure that the .jar file is added to the classpath of your project.

Source

8

古いバージョンのKotlinを使用していましたが、正しく設定されていませんでした。私は最新のベータ版を含むようにgradleファイルを編集し、Kotlinを設定するオプションを選択しました。 Gradleので

buildscript { 
    ext.kotlin_version = '1.0.0-beta-3594' 
    repositories { 
     mavenCentral() 
    } 
    dependencies { 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 
     classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version" 
    } 
} 
... 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 
} 
+0

私にとってはベータ版ではなく、依存関係をすべて追加しました – Kostya

0

私は他のプロジェクトからクラスをコピーして、クラスのパッケージ名を変更するのを忘れていました。 私が変更されたとき、それはbuild.gradleに依存関係セクション

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

implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 

私はのGradleの始まり(モジュールアプリ)に入れ

1

を固定(プロジェクト)

buildscript { 
    ext.kotlin_version = '1.2.0' 
    repositories { 
     jcenter() 
     google() 
    } 
    dependencies { 
     classpath 'com.android.tools.build:gradle:3.0.1' 
     classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 

     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
}