2015-12-07 15 views
11

Próbuję przekonwertować kod Java HomePage.class na Kotlin. Ja postępując zgodnie z instrukcjami na Kotlin.org:Kotlin Foo :: class.java Błąd "Nierozwiązana referencja: 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

Mam klasy o nazwie HomePage który rozciąga AppCompatActivity (w Androidzie). Korzystam z Android Studio. Próbowałem robić HomePage::class.java i ma błąd: Unresolved reference: java

enter image description here

Jak mogę uzyskać to do pracy?

Odpowiedz

10

Problem jest bardzo prawdopodobne, że zapomniałeś zależą reflection libraries które były potrzebne dla refleksyjnych funkcji Kotlin.

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

Okazuje się, że korzystałem ze starszej wersji Kotlina i nie został on poprawnie skonfigurowany. Edytowałem plik gradle, aby dołączyć najnowszą wersję beta, i wybrałem opcję, która konfiguruje Kotlin, i teraz działa.

W 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

Dla mnie pomógł nie beta, ale dodanie wszystkich swoich zależności – Kostya

0

skopiowane klasy z innego projektu i zapomniałem zmienić nazwę pakietu klasy. kiedy zmieniło, to stałe

1

kładę na początku Gradle (ok Module)

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

i

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

w zależności sekcja

W build.gradle (Projekt)

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 
    } 
} 
Powiązane problemy