Android new class problem

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

<code>

``

package com.example

import android.view.View
import android.content.Context
import android.graphics.Canvas
import android.widget.ImageView
import android.graphics.drawable.Drawable
import java.util.ArrayList

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

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

  fun addImage(drawable : Drawable ){
  images.add(drawable)
  }

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

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

}

And added these lines to the activity

``

  val strip = ImageScroll(this)
  strip.addImage(R.drawable.theron)

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.app.Activity.performCreate(Activity.java:5008)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
  at android.app.ActivityThread.access$600(ActivityThread.java:130)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4745)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  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 https://github.com/dodyg/AndroidRivers

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 https://github.com/dodyg/AndroidRivers/tree/master/src/com/silverkeytech/android_rivers. 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 http://goo.gl/kShgp.

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|attachment](upload://sP2ndvKMMWR6OkLvFgqEXC9n5Gr.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 http://youtrack.jetbrains.com/issue/KT-2792. As a workaround you can remove 'val' in constructor or you can rename a context property.