Kotlin-dsl-samples: Create an Android sample project

Created on 18 May 2016  Â·  24Comments  Â·  Source: gradle/kotlin-dsl-samples

@anokmik originally wrote:

Hi!

Thanks for an opportunity to use Kotlin for writing Gradle build scripts.

As an Android developer I'm curious if it supports Android for now? If not, then could you, please, tell when this feature might be added?

This issue is now about creating a sample project that will demonstrate how to write an Android-based build using Gradle Script Kotlin.

chore kotlin-dsl-samples

Most helpful comment

I've just updated the title and description of this issue to make it about demonstrating support for Android builds via a sample project.

As mentioned in the comments above, the major limiting factor in being able to use Gradle Script Kotlin to write Android builds is being able to actually get the Android plugin on the build script classpath in the first place (#29). Once that's done, we'll start working on putting together an Android sample.

All 24 comments

I've tried to run generateKtsConfig task in my Android project, with the following result:

$ ./gradlew generateKtsConfig

FAILURE: Build failed with an exception.

* Where:
Build file '/path-to-my-project/app/build.gradle' line: 1

* What went wrong:
A problem occurred evaluating project ':app'.
> Failed to apply plugin [id 'com.android.application']
   > Gradle version 2.10 is required. Current version is 3.0-snapshot-1. If using the gradle wrapper, try editing the distributionUrl in /path-to-my-project/gradle/wrapper/gradle-wrapper.properties to gradle-2.10-all.zip

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 2.329 secs

Also, in the Release Notes, I've found this information:

There is not yet support for additions to the buildscript classpath. This means that there there is not yet support for resolving external plugins. Naturally, this is a high priority for the next milestone.

This probably makes it impossible to build any Android project.

Even “kotlin-plugin-1.1.0-dev-372.zip” is not compatible with Android Studio 2.1.1 (latest version, downloaded today).
According to README, IntelliJ IDEA 2016.1.2 is required. I don't know, how it translates to Android Studio version though.

There are a couple of reasons that writing Android builds in Kotlin won't work yet (as of 1.0 M1):

  1. As @sczerwinski points out—it's not yet possible to resolve external plugins, including the Android plugin itself.
  2. Even if you could resolve it, the Android plugin fails the build if you're working with any version of Gradle other than 2.10—this can also be seen in @sczerwinski's comment above

So at the moment, the best answer I can give you is: _stay tuned_. We're already in communication with the Android team about this work, and I'll update this issue as the next steps toward Android support become clearer.

Android +1

Even if you could resolve it, the Android plugin fails the build if you're working with any version of Gradle other than 2.10

It turns out that the Android plugin's limitation on Gradle 2.10 can be worked around by adding the following to your build:

buildscript {
  System.properties['com.android.build.gradle.overrideVersionCheck'] = 'true'
  ...
}

See this bug report for more information: https://code.google.com/p/android/issues/detail?id=211462&thanks=211462&ts=1464346630

I've just updated the title and description of this issue to make it about demonstrating support for Android builds via a sample project.

As mentioned in the comments above, the major limiting factor in being able to use Gradle Script Kotlin to write Android builds is being able to actually get the Android plugin on the build script classpath in the first place (#29). Once that's done, we'll start working on putting together an Android sample.

@cbeams Here is the issue I am seeing.

Error message:

org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection

Error:

otal time: 1.442 secs
$ ./gradlew
Gradle version 2.10 is required. Current version is 3.0-milestone-2. If using the gradle wrapper, try editing the distributionUrl in /Users/noname/repo/android-gradle-java-template/gradle/wrapper/gradle-wrapper.properties to gradle-2.10-all.zip
As com.android.build.gradle.overrideVersionCheck is set, continuing anyways.
The use of the @OutputDirectories annotation on non-Map properties has been deprecated and is scheduled to be removed in Gradle 3.0. Please use separate properties for each directory annotated with @OutputDirectory, or change the property type to Map.
The use of the @OutputFiles annotation on non-Map properties has been deprecated and is scheduled to be removed in Gradle 3.0. Please use separate properties for each file annotated with @OutputFile, reorganize output files under a single output directory annotated with @OutputDirectory, or change the property type to Map.
Incremental java compilation is an incubating feature.

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring root project 'android-gradle-java-app-template'.
> org.gradle.api.internal.tasks.DefaultTaskInputs$TaskInputUnionFileCollection cannot be cast to org.gradle.api.internal.file.collections.DefaultConfigurableFileCollection

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 0.755 secs

Superseded by #83.

I got the hello-android kotlin sample app to build with AndroidStudio 3.0 by adding the old style

    maven { url 'https://maven.google.com' }

into the settings.gradle pluginManagement/Repositories block. You can't add google() there but you can add this older style maven url declaration.

@MonteCreasor You should not have to. You can use google(), see here: https://github.com/jaredsburrows/android-gif-example/blob/master/build.gradle.kts#L10.

I wasn't talking about build.gradle, I was referring to settings.gradle. I have no problems with declaring google() in the build.gradle.kts, only in the settings.gradle. If you try and add google() to the repository block in settings.gradle, it won't get resolved. And, you can't import RepositoryHandler. I tried to add import org.gradle.api.artifacts.dsl.RepositoryHandler at the top of settings.gradle and that didn't help.

That is right. That is not supported yet: https://docs.gradle.org/current/userguide/plugins.html#sec:plugin_management.

I like your android-gif-example though. The kotlin gradle setup is great.

@MonteCreasor Thanks! Feel free to contribute. I want to keep it up to date with the latest Kotlin DSL.

@jaredsburrows How do you get your kotlin dsl scripts to not have all sorts of parsing conflicts between Java and Kotlin. The project compiles and runs fine using AndroidStudio 3.0, but if I open any of the .kts files, I get all sorts of errors all to do with the parser not being able to find java objects (String, Object, etc.). This also happened with the hello-android app in the Kotlin samples.

@MonteCreasor It happens in this file: https://github.com/jaredsburrows/android-gif-example/blob/master/gradle/project-schema.json.

When you add a plugin to your classpath and then apply the plugin, you need to update the project-schema.json by running gradlew kotlinDslAccessorsSnapshot.

@jaredsburrows Shouldn't that just work already in your project? I just ran kotlinDslAccessorsSnapshot in your project and it ran without any errors, but it doesn't resolve all the errors in the .kts files. I'm not adding any plugins because I'm just trying to get your project to run. Well, it does run, but if you saw the errors I have in the .kts files, you would be amazed that it builds and runs anyway.

@MonteCreasor Ideally it should. But when I apply a new plugin and add it's extension it always complains.

What errors are you seeing? If you are using Android Studio 3.0.0, there is a bug for this here: https://github.com/gradle/kotlin-dsl/issues/584.

@jaredsburrows Ya, I saw that exact bug earlier today when I was trying to get the hello-android app to compile. Those are the exact type of errors I'm getting. I'm running AndroidStudio 3.0.0 on Ubuntu 16.04 LTS. Why the heck is it working for you? Is this a Windows/Linux issue maybe?

It should work for you as it works for the CI. The IDE just "complains" because Android Studio is always being Jetbrain's normal Intellij IDEA IDE.

Same problem in IDEA 2017.2

You are right. I see the same thing.

Thanks for all the help @jaredsburrows. I think I'll wait until things are more stable and go back to boring Groovy for now.

If you apply all your plugins using the plugins {} block then you don't need the project-schema.json file anymore, see https://github.com/gradle/kotlin-dsl/blob/master/doc/getting-started/Configuring-Plugins.md for some guidance on how to achieve this. The hello-android sample does this, see https://github.com/gradle/kotlin-dsl/tree/master/samples/hello-android.

Named repositories (e.g. google(), jcenter() etc..) in settings.gradle(.kts) scripts will be supported starting with the Kotlin DSL 0.13 in Gradle 4.4, see #469.

@eskatos This is very exciting news! Thank you!

Was this page helpful?
0 / 5 - 0 ratings