Android subprojects with Kotlin DSL - gradle

I'm working on migrating by Gradle build files to the Kotlin DSL and I encounter an issue.
On my parent build.gradle, I have the following piece of code
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Version.kotlin}"
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate { project ->
if (project.plugins.findPlugin('com.android.application') ?:
project.plugins.findPlugin('com.android.library')) {
android {
compileSdkVersion = Android.SDK_COMPILE
defaultConfig {
minSdkVersion Android.SDK_MIN
targetSdkVersion Android.SDK_TARGET
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
This allows me to configure in only one place all the modules that are android applications or libraries.
However this doesn't seem to work when I migrated to kotlin:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath(Dependency.androidGradle)
classpath(Dependency.kotlinGradle)
}
}
allprojects {
repositories {
google()
mavenCentral()
}
}
subprojects {
afterEvaluate {
if (project.plugins.findPlugin("com.android.application") != null ||
project.plugins.findPlugin("com.android.library") != null) {
android { <<<<------------ Unresolved reference: android
compileSdkVersion(Android.SDK_COMPILE)
defaultConfig {
minSdkVersion(Android.SDK_MIN)
targetSdkVersion(Android.SDK_TARGET)
versionCode = Android.VERSION_CODE
versionName = Android.VERSION_NAME
}
...
}
}
}
}
The error is Unresolved reference: android and it looks like the android{} block is not recognized by the script compiler.
My theory is that the if checking for the subproject type is not enough, and I might have to cast or get a reference to some object in which I can call the android{} block, but honestly I do not know enough.
Any clues?

Gradle Kotlin DSL determines the dependencies for each script from the gradle classpath plus the applied plugins. That's why its recommended to use the plugins { ... } block in the Kotlin DSL.
You need to add the android and kotlin plugins to your root without applying it.
plugins {
id("<android-plugin>") version "<plugin-version>" apply false
id("<kotlin-plugin>") version "<plugin-version>" apply false
}
Unfortunately, that will still not generate the static accessors for the root build script but it will give you access to the plugin classes within the script and you can reference them like:
subprojects {
// BasePlugin is the common superclass of the AppPlugin and LibraryPlugin which are the plugin classes that "com.android.application" and "com.android.library" apply
plugins.withType<BasePlugin> {
// BaseExtension is the common superclass of the AppExtension and LibraryExtension which are the extension classes registered by the two plugins to the name "android"
configure<BaseExtension> {
// This block is typed correctly
defaultConfig {
// ...
}
}
}
}

Related

Gradle subproject conditional plugin configuration with gradle kotlin dsl

This is how I've previously configured subprojects in gradle if a certain plugin was present:
subprojects {
repositories {
jcenter()
google()
}
plugins.withType(com.android.build.gradle.LibraryPlugin) {
android {
compileSdkVersion 29
...
}
}
}
Now I'm not working with groovy anymore. How can I achieve the same thing with gradle kotlin DSL?
Biggest problem here seems to be to find a way to access the android extension, I can't seem to access it using android nor extensions.findByType(com.android.build.gradle.LibraryExtension::class.java).
I solved this problem by creating a precompiled script plugin, which I applied to all subprojects.
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
dependencies {
implementation("com.android.tools.build:gradle:4.1.2")
}
buildSrc/src/main/kotlin/android-libary.gradle.kts
plugins {
id("com.android.library")
}
android {
compileSdkVersion(29)
...
}
subProject/build.gradle.kts
plugins {
`android-library`
}

Define the generator code location using gradle to build xtext and xtend

I'm trying to create a first project using xText and xTend building with gradle.
I created the grammar following the guidance in the xText documentation and also created the xtend generators.
In eclipse the code generates to src-gen folder as expected.
When I created the gradle script, also following the http://xtext.github.io/xtext-gradle-plugin/xtext-builder.html to build my code instead of generating the code in 'src-gen' folder it generates in 'build' folder.
Is there any way to change this folder from build to src-gen in the gradle? I tried a lot of things and I got always errors.
Complete code of grade script:
apply plugin: 'org.xtext.builder'
dependencies {
xtextLanguages 'com.example.mylang:mylang:1.0.0-SNAPSHOT'
}
xtext {
languages {
mylang{
setup = 'com.example.MyLangStandaloneSetup'
generator.outlet.producesJava = true
}
}
sourceSets {
main {
srcDir 'src/main/xtext'
xtendOutputDir 'src-gen'
}
}
}
you can configure that in the source set
sourceSets {
main.xtendOutputDir = 'xtend-gen'
}
e.g.
plugins {
id "org.xtext.xtend" version "1.0.21"
}
apply plugin: 'java'
apply plugin: 'org.xtext.xtend'
sourceSets {
main.java.srcDirs = ['src','xtend-gen']
main.xtendOutputDir = 'xtend-gen'
}
repositories {
jcenter()
}
dependencies {
// This dependency is exported to consumers, that is to say found on their compile classpath.
compile 'org.eclipse.xtext:org.eclipse.xtext.xbase.lib:2.13.0'
}
or for the xtxt builder plugin
buildscript {
repositories {
mavenLocal()
jcenter()
}
dependencies {
classpath 'org.xtext:xtext-gradle-plugin:1.0.21'
}
}
plugins {
id "org.xtext.builder" version "1.0.21"
}
repositories {
mavenLocal()
jcenter()
}
dependencies {
xtextLanguages 'org.xtext.example.mydslfoo:org.xtext.example.mydslfoo:1.0.0-SNAPSHOT'
}
xtext {
version '2.13.0'
languages {
mydslfoo {
setup = 'org.xtext.example.mydslfoo.MyDslFooStandaloneSetup'
generator {
outlets {
HEROES {
}
}
}
}
}
sourceSets {
main {
srcDir 'src'
output {
dir(xtext.languages.mydslfoo.generator.outlet, 'src-gen')
}
}
}
}

Gradle cannot find method android() for arguments (Gradle 2.2.3)

Gradle for my project isn't working. I've checked other questions that have had the same problem, but this one is in a newer version and I'm not sure how to solve it. I'm trying to get the Android SDK up and running for the first time, so I'm really new to this. Thorough answers are appreciated.
Here is my Gradle error:
Error:(22, 0) Could not find method android() for arguments [build_3bh8pl96cp64yo421v01fuk9s$_run_closure3#4b7fff2f] on root project 'Meet-upProject' of type org.gradle.api.Project.
And here is my build.gradle in root:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
dexOptions {
incremental true
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_6
targetCompatibility JavaVersion.VERSION_1_6
}
}
dependencies {
compile files('app/libs/junit-4.12-JavaDoc.jar')
}

Could not find property 'plugin' on project:

I have an SDK for which I've developed a number of small test applications. These are all stand alone android apps which have the following in common:
1) All dependencies are the same (i.e. same version of my sdk/android sdk/etc..)
2) They all share a common top level build.gradle file (which is currently empty):
3) They're build,test etc.. phases are all identical.
I want the projects to all exist as modules in Android studio, and I want them to share as much configuration as possible in the top level build.gradle so that I can update the one file and have everything working correctly.
When I add the following to my top level build.gradle:
subprojects {
apply plugin: "android"
repositories {
mavenCentral()
maven {
url "my.repo.com"
credentials {
username "user"
password "pass"
}
}
dependencies {
compile 'com.my.library:my-library:0.0.1'
// compile 'com.google.android.gms:play-services:+'
}
}
}
I get
'could not find property 'plugin' on ':App1' where ':App1' is the first app in settings.gradle, and the build fails.
My goals would be to get the above block and as many other applicable elements in this top level file.
Edit: I've fixed the syntax error (i was missing the :), but I've that leads to a "plugin with id 'android' not found. Generally this would be resolved by adding:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
But that doesn't resolve it in this case.
Edit 2:
final form:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.14.4'
}
}
allprojects {
repositories {
mavenCentral()
}
}
subprojects {
apply plugin: "android"
repositories {
mavenCentral()
maven {
url "my.repo.com"
credentials {
username "user"
password "pass"
}
}
dependencies {
compile 'com.my.library:my-library:0.0.1'
// compile 'com.google.android.gms:play-services:+'
}
}
android {
compileSdkVersion 19
buildToolsVersion '19.1.0'
lintOptions {
abortOnError false
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/NOTICE'
}
defaultConfig {
minSdkVersion 16
targetSdkVersion 19
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
productFlavors {
}
}
}
That seems to be working correctly.
This is a simple syntactical error. The apply() method takes a Map as argument. Don't forget the colon (:).
apply plugin: 'android'

where to put sourceSets in multiple projects gradle

I have a multiple projects gradle, in the top gradle is
subprojects {
apply plugin: "java"
sourceSets {
main {
scala {
srcDirs = ['src/main/scala', 'src/main/java']
}
java {
srcDirs = []
}
}
}
repositories {
mavenCentral()
maven {
url "http://repo.springsource.org/milestone"
}
}
}
But it complains
> Could not find method sourceSets() for arguments [build_vgdvugn6hqrvg7eo53afh1229$_run_closure1_closure2#19962194] on root project 'testCom'.
So where should I put sourceSets?
The error message is a bit misleading, but before you can configure sourceSets.main.scala, you'll have to apply the scala plugin.

Resources