Android new class problem


I was just trying to monkey with kotlin android example, so I created a class:



package com.example

import android.view.View
import android.content.Context
import android.widget.ImageView
import java.util.ArrayList

public  class ImageScroll(val context : Context) : ImageView(context) {

  val images = ArrayList<Drawable>()
  val current = 0

  fun addImage(drawable : Drawable ){

  fun addImage(resource : Int){
  val d = getResources()?.getDrawable(resource)
  if(d != null )

  protected override fun onDraw(canvas : Canvas?){
  val d = images.get(current)


And added these lines to the activity


  val strip = ImageScroll(this)

And I get  a class not found error


03-10 11:37:58.837: ERROR/dalvikvm(2049): Could not find class ‘com.example.ImageScroll’, referenced from method com.example.HelloKotlin.onCreate
03-10 11:37:59.057: ERROR/AndroidRuntime(2049): FATAL EXCEPTION: main
  java.lang.NoClassDefFoundError: com.example.ImageScroll
  at com.example.HelloKotlin.onCreate(KotlinActivity.kt:12)
  at android.os.Handler.dispatchMessage(
  at android.os.Looper.loop(
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(
  at dalvik.system.NativeStart.main(Native Method)


This is weird. Did you try to create this Android project from scratch? Here's a pretty large example of a Kotlin based Android app


Thanks for the reply. No I haven't created it from scratch. I checked out the sample code from github and added this class. This is the first time I am trying Kotlin, so there might be errors in my code even though IDEA compiles it nicely. BTW, I looked at your project, and couldn't find any Kotlin source. All the files in the src directory seems to be java. Where can I find the Kotlin files?   


The application source code is at The mix with Java and Kotlin is especially prevalent in Kotlin Android projects since there are java libraries that you have to import by source such as HoloEverywhere and ActionBarSherlock.

You can check out the whole source (everything is self contained) and run it. The resulting app itself is available at Google Play at

Try to create an Android module from scratch and see if it still gives you the same problem.

This project uses:

  • JSON Serialization
  • XML Parsing
  • SQLite ORM
  • Media Player
  • Services
  • Fragments
  • Sliding Menu
  • Holo Theme
  • ActionBarSherlock
  • Http calls

So you’ll have enough real world component usage to use in your own Kotlin Android projects.


Please send us your project. Thanks


Is your activity in the same package as ImageScroll?


Yes it is in the same package indeed. BTW, are you aware of any performance penalty for using Kotlin instead of Java?


I've never seen any benchmark anywhere but it's a statically typed JVM language that emits Java bytecode. So its performance profiler should be very similar to Java.


Here is the source code.   

kotlin-android.tar.gz (1.32 MB)


I understand that it should run as fast asa Java, but I guess its memory foot print and boot time might be different.


From my Android River experience, there's no boot time lag even on my lowly Galaxy S. I am actually writing a game with Kotlin using libgdx and artemis-entity and the game just boots right up.



public  class ImageScroll(val context : Context) : ImageView(context) {



Get rid of the val and then it works. You don't need to store it in ImageScroll, it's already stored in ImageView.

That being said, it looks like a bug.


Kotlin automatically generate a get method for val context in consructor called getContext(). Unfortunatly ImageView already has this method (and it is final). There is known bug about missing error/warning, when generated getter override another one from super class. Fill free to vote this issue As a workaround you can remove 'val' in constructor or you can rename a context property.