Kotlin and Android

I'm an Android developer, so the first thing I wondered, after getting "Hello World" in Kotlin running in IntelliJ is:  I wonder if I can already use Kotlin for an Android app?!

So I decided to do a simple test, I created a simple android app, and made sure it was working.  Then I created a kotlin file with a class and a simple method (which just returned “Hello from Kotlin”) and I called it from an Android activity.  But, after I added the kotlin jar to my project, the app no longer worked, I kept getting the error INSTALL_FAILED_DEXOPT when I tried to run it.   I could not find on google what this error message meant.  So I’m wondering if anyone knows the reason for this.  Should Kotlin theoritically work with android at this point?  Maybe it’s something simple that needs to be fixed?

Great work on Kotlin, it’s impressive to see it working in IntelliJ!

AFAIU, Kotlin should theoretically work with Android at this point, but you were the first to actually try it there. Did you try what these guys suggest? http://stackoverflow.com/questions/5153515/how-to-solve-install-failed-dexopt-this-error

Thanks!

Yes, I did try the things suggested in this post (uninstalling app/wiping app data).  That didn't help.

Also, unrelated to this issue, but something that caught me by surprise is that when I downloaded IntelliJ 11.1 and installed Kotlin, and then when I quit that and went back to IntelliJ 11, I wasn’t expecting Kotlin to be installed. I didn’t realize that plugins would be shared between two IntelliJ installations.  Is this the expected behavior?  I’m asking because the Kotlin plugin was preventing me from compiling my non-Kotlin project in IntelliJ 11!

thanks!

Unfortunately, I don't know what to say about the Android issue, we didn't do anything about Android so far.

On the sharing plugins: it’s a matter of how you installed IDEA: if two installations share the same configuration directory, you get plugins shared, otherwise you have two separate plugin repo’s.
A workaround here would be to have the plugin unzipped into the plugins folder of the EAP installation.

First of all: Sorry for posting this as a response! For some reason this forum does not allow me to create new discussions. I click on "New" -> "Discussion", then I search for Kotlin and get three greyed out restults.. Going directly to the two subforums tells me that I can't create a new discussion there. Please split this post into a seperate thread if possible.

I find Kotlin very impressive so far and would like to fiddle with the source code of the compiler myself. I have been able to build the plugin for IntelliJ with the build.xml from the repository. On Github you are offering a standalone version of the compiler which I would like to use and build myself. Is there a predefined ant task, that builds the standalone compiler?

Best wishes,
Fabian

Hi,

The forum settings should be fixed now. Please try if you can create discussions and tell me if you can’t.

Actually the “zip” target of build.xml is what we use to build the standalone compiler.
On the contratry, the plugin can’t be easily assembled by the Ant targets so you have to build it with IntelliJ IDEA artifacts.

I may be totally wrong, but it seems that Dalvik VM does not like flags in the class file.

Here is what Android’s log looks like when trying to install application compiled with kotlin runtime:

I/PackageManager(  162): Running dexopt on: org.test.kotlin

W/dalvikvm( 1807): Invalid file flags in class Lkotlin/concurrent/FunctionalList$ClassObject$;: 0009

F/libc   ( 1807): Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1)

I/DEBUG   (   77): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

kotlin/concurrent/FunctionalList$ClassObject$.class indeed has access_flags set to value 0x09, which is of course 0x01 + 0x08, but, according to JVM specs, there is no flag with value 0x08 defined for classes.

ACC_STATC = 8

Isn't it for fields and methods only? ACC_STATIC isn't mentioned in ClassFile description in specs, and while it may be perfectly valid to have it set for classes, Dalvik could be more picky than JVM, when it comes to flags.

I had exactly the same issue, regardless of the way I built an .apk. I tried to build it with IDEA and with ant. Both ways allow to make .apk successfully, and even I use aapt to remove *.kt files mistakenly included in .apk by IDEA or ant from .apk, the installation fails. Is there a way to change the flags? Is is because the flags are related to the class written in Kotlin? Can we ask Kotlin compiler to use just ACC_PUBLIC? How can we check the flags for other classes included in .apk?

See this request http://youtrack.jetbrains.com/issue/KT-1554

Feel free to vote for it.

It seems that fixing this request didn't help with the segfault from Android. Any ideas what the cause may be?

I am using Kotlin M1 and Android. It works (at least from emulator)

HelloWorld.kt

package com.silverkeytech

fun sayMessage():String{
  return “hello world”
}

WelcomePage.java

public class WelcomePage extends Activity {   /** Called when the activity is first created. */   @Override   public void onCreate(Bundle savedInstanceState)   {   super.onCreate(savedInstanceState);   setContentView(R.layout.main);   TextView text = (TextView) findViewById(R.id.lblMain);   text.append(com.silverkeytech.namespace.sayMessage());   } }

Yes, I tried and it works!  thanks for letting us know!  

Very excited to be able to use Kotlin for my Android projects!

Hello,

Can you post a full example on how you got the Kotlin source to compile to .dex  via blog or here?
I would purchase IntelliJ and switch 100% to Kotlin for Android as the language just doesn’t get in my way and allows me to focus on what’s really important, my code!

Thanks,

Josh

I am a .Net developer so I know very little about Android development but this is the step I took.

  1. Download JetBrains IntelliJ 11.1 community edition version.
  2. Install Kotlin Milestone Candidate http://www.jetbrains.com/kotlin/eap-plugin-repository/kotlin-plugin-0.1.1966.zip
  3. Verify that your kotlin plugin works (so just create a hello world module, etc)
  4. Install Android SDK.
  5. Verify that it works with your Jetbrain (create hello world app, run it in emulator).
  6. Now simply create a Kotlin file in your Android project and set up the package name.
  7. Write a simple Kotlin function and use it it in the main Activity.
  8. Compile and run.

That’s it. Sorry I couldn’t help much more. The sample code above is pretty much my first Android code.

I have a very similar problem, but only on devices with API 17 and older ones. None of my Kotlin apps work on those devices, and all of them work perfectly on API 19 and newer ones. So is it that just the older APIs aren't (yet) supported? I use Kotlin beta 1038.

logcat:

D/installd: DexInv: --- BEGIN '/data/app/com.radoid.wallbard.apk' ---
W/dalvikvm: Invalid file flags in class Lkotlin/FloatingPointConstants$DefaultImpls;: 0019
W/installd: DexInv: --- END '/data/app/com.radoid.wallbard.apk' --- status=0x000b, process failed
E/installd: dexopt failed on '/data/dalvik-cache/data@app@com.radoid.wallbard.apk@classes.dex' res = 11

Fixed in 1.0.0-beta-1103

I'm sorry for bringing only bad reports. :| The update to Beta 1103 made things a bit worse (on API 17 and older). The "invalid file flags" are still there, and dexopt now makes a segmentation fault at that point, every time:

I/PackageManager: Running dexopt on: com.radoid.wallbard
W/dalvikvm: Invalid file flags in class Lkotlin/FloatingPointConstants$DefaultImpls;: 0019
A/libc: Fatal signal 11 (SIGSEGV) at 0x00000004 (code=1), thread 3582 (dexopt)
W/installd: DexInv: --- END '/data/app/com.radoid.wallbard-1.apk' --- status=0x000b, process failed
E/installd: dexopt failed on '/data/dalvik-cache/data@app@com.radoid.wallbard-1.apk@classes.dex' res = 11

Have you updated the kotlin-runtime dependency version as well? Also, if you have Kotlin libraries in dependencies built with the previous version of Kotlin compiler, such libraries can cause the dexopt segfault as well.