Adding Kotlin plugins using plugins DSL - android

It looks like there are two ways of declaring some Kotlin plugins using plugins DSL: Using the id() method and the kotlin() method. For example, the android plugin can be added using either id("kotlin-android") or kotlin("android"). This is also the case for kapt but not for parcelize. Why can't this be kotlin("parcelize")? Is there a reason for this discrepancy? I tried to look up relevant documentation but that didn't get me very far.

TL;DR: Take the Gradle plugin ID for Parcelize and use everything after org.jetbrains.kotlin.
plugins {
kotlin("plugin.parcelize") version "1.6.10"
}
The kotlin(...) function is part of the Gradle Kotlin DSL. It is an extension function that extends
PluginDependenciesSpec, the plugins {} block
DependencyHandler, the dependencies {} block
I'm going to focus on the Plugin extension function. Some of this answer is applicable to the Dependency extension.
kotlin(...) source code
It's generated so it's difficult to see the source code. I dug through GitHub and found it in GenerateKotlinDependencyExtensions.kt
fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec =
id("org.jetbrains.kotlin.$module")
(edited, to show the end result)
kotlin(...) = id("org.jetbrains.kotlin.$module")
So it's nothing special. It's a Kotlin-specific shortcut for id(...). So if you
take the plugin ID for Parcelize, org.jetbrains.kotlin.plugin.parcelize,
and remove the bits that the kotlin(...) function adds (org.jetbrains.kotlin.),
you're left with plugin.parcelize.
NOTE Because this is in the plugins {} block, it's working on the Gradle plugin ID (org.jetbrains.kotlin.plugin.parcelize), not the Maven coordinates (org.jetbrains.kotlin:kotlin-gradle-plugin).
plugins {
// these two are equivalent
// id("org.jetbrains.kotlin.plugin.parcelize")
kotlin("plugin.parcelize")
}
Oh wait... it doesn't work??
FAILURE: Build failed with an exception.
* Where:
Build file '/.../build.gradle.kts' line: 3
* What went wrong:
Plugin [id: 'org.jetbrains.kotlin.plugin.parcelize'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
"Build failed with an exception" - Plugin version
That's because unlike the DependencyHandler.kotlin(...) extension, the PluginDependenciesSpec.kotlin(...) doesn't include the version. It says that in the error message: "plugin dependency must include a version number"
So to resolve it, add a version number.
plugins {
kotlin("plugin.parcelize") version "1.6.10"
}
Other Kotlin plugins
The same goes for the other Kotlin plugins. For example...
plugins {
// https://kotlinlang.org/docs/all-open-plugin.html
kotlin("plugin.allopen") version "1.6.10"
// https://kotlinlang.org/docs/all-open-plugin.html#spring-support
kotlin("plugin.spring") version "1.6.10"
// https://kotlinlang.org/docs/no-arg-plugin.html
kotlin("plugin.noarg") version "1.6.10"
// I can't find a Gradle Plugin ID for
// https://kotlinlang.org/docs/sam-with-receiver-plugin.html
// so this won't work!
// kotlin("kotlin-sam-with-receiver") version "1.6.10"
}

Related

gradle using plugin id(kotlin-kapt) vs kotlin(kapt) [duplicate]

It looks like there are two ways of declaring some Kotlin plugins using plugins DSL: Using the id() method and the kotlin() method. For example, the android plugin can be added using either id("kotlin-android") or kotlin("android"). This is also the case for kapt but not for parcelize. Why can't this be kotlin("parcelize")? Is there a reason for this discrepancy? I tried to look up relevant documentation but that didn't get me very far.
TL;DR: Take the Gradle plugin ID for Parcelize and use everything after org.jetbrains.kotlin.
plugins {
kotlin("plugin.parcelize") version "1.6.10"
}
The kotlin(...) function is part of the Gradle Kotlin DSL. It is an extension function that extends
PluginDependenciesSpec, the plugins {} block
DependencyHandler, the dependencies {} block
I'm going to focus on the Plugin extension function. Some of this answer is applicable to the Dependency extension.
kotlin(...) source code
It's generated so it's difficult to see the source code. I dug through GitHub and found it in GenerateKotlinDependencyExtensions.kt
fun PluginDependenciesSpec.kotlin(module: String): PluginDependencySpec =
id("org.jetbrains.kotlin.$module")
(edited, to show the end result)
kotlin(...) = id("org.jetbrains.kotlin.$module")
So it's nothing special. It's a Kotlin-specific shortcut for id(...). So if you
take the plugin ID for Parcelize, org.jetbrains.kotlin.plugin.parcelize,
and remove the bits that the kotlin(...) function adds (org.jetbrains.kotlin.),
you're left with plugin.parcelize.
NOTE Because this is in the plugins {} block, it's working on the Gradle plugin ID (org.jetbrains.kotlin.plugin.parcelize), not the Maven coordinates (org.jetbrains.kotlin:kotlin-gradle-plugin).
plugins {
// these two are equivalent
// id("org.jetbrains.kotlin.plugin.parcelize")
kotlin("plugin.parcelize")
}
Oh wait... it doesn't work??
FAILURE: Build failed with an exception.
* Where:
Build file '/.../build.gradle.kts' line: 3
* What went wrong:
Plugin [id: 'org.jetbrains.kotlin.plugin.parcelize'] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (plugin dependency must include a version number for this source)
"Build failed with an exception" - Plugin version
That's because unlike the DependencyHandler.kotlin(...) extension, the PluginDependenciesSpec.kotlin(...) doesn't include the version. It says that in the error message: "plugin dependency must include a version number"
So to resolve it, add a version number.
plugins {
kotlin("plugin.parcelize") version "1.6.10"
}
Other Kotlin plugins
The same goes for the other Kotlin plugins. For example...
plugins {
// https://kotlinlang.org/docs/all-open-plugin.html
kotlin("plugin.allopen") version "1.6.10"
// https://kotlinlang.org/docs/all-open-plugin.html#spring-support
kotlin("plugin.spring") version "1.6.10"
// https://kotlinlang.org/docs/no-arg-plugin.html
kotlin("plugin.noarg") version "1.6.10"
// I can't find a Gradle Plugin ID for
// https://kotlinlang.org/docs/sam-with-receiver-plugin.html
// so this won't work!
// kotlin("kotlin-sam-with-receiver") version "1.6.10"
}

How to configure Firebase Performance Monitoring plugin extension in Gradle Kotlin DSL

I have an Android app using Gradle with Kotlin DSL. I'm adding Firebase Performance Monitoring, but I would like for it to be enabled only for a specific build type.
I've been following the instructions provided at Firebase - Disable Firebase Performance Monitoring. Unfortunately the provided snippets are in Groovy.
I've tried to get a reference to the Firebase Performance Monitoring extension in my app level Gradle script by doing the following:
plugins {
...
id("com.google.firebase.firebase-perf")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
}
buildTypes {
getByName(BuildTypes.DEBUG) {
configure<com.google.firebase.perf.plugin.FirebasePerfExtension> {
setInstrumentationEnabled(false)
}
}
...
}
...
dependencies {
val firebaseVersion = "17.2.1"
implementation("com.google.firebase:firebase-core:$firebaseVersion")
implementation("com.google.firebase:firebase-analytics:$firebaseVersion")
implementation("com.google.firebase:firebase-perf:19.0.5")
}
Android Studio doesn't see any problem in this and auto-completes FirebasePerfExtension.
Unfortunately upon running a Gradle sync I get the following:
Extension of type 'FirebasePerfExtension' does not exist.
Currently registered extension types: [ExtraPropertiesExtension, DefaultArtifactPublicationSet, ReportingExtension, SourceSetContainer, JavaPluginExtension, NamedDomainObjectContainer<BaseVariantOutput>, BaseAppModuleExtension, CrashlyticsExtension, KotlinAndroidProjectExtension, KotlinTestsRegistry, AndroidExtensionsExtension, KaptExtension]
There's no plugin extension related to Firebase Performance Monitoring.
This is in my project level build.gradle file dependencies block:
classpath("com.google.firebase:perf-plugin:1.3.1")
Any help is appreciated!
Update 1
As recommended on the Gradle - Migrating build logic from Groovy to Kotlin guide at "Knowing what plugin-provided extensions are available" I've ran the kotlinDslAccessorsReport task. None of the resulting extensions seems to be related to Firebase.
Had the same issue and was going to apply from groovy file, but seems i found the solution in here: https://docs.gradle.org/5.0/userguide/kotlin_dsl.html#sec:interoperability
withGroovyBuilder {
"FirebasePerformance" {
invokeMethod("setInstrumentationEnabled", false)
}
}
We used this answer, util we discovered a better working way in the team
check(this is ExtensionAware)
configure<com.google.firebase.perf.plugin.FirebasePerfExtension> { setInstrumentationEnabled(false) }

How to generate and publish Code Coverage in gradle kotlin using Azure DevOps?

I am trying to integrate code coverage in gradle Kotlin as suggested in https://techblog.tbauctions.com/kotlin-azure-and-code-coverage/ but it's not working, below are the project files.
build.gradle.kts
plugins {
jacoco
}
val test by tasks.getting(Test::class) {
useJUnitPlatform { }
}
tasks.withType(JacocoReport::class.java).all {
reports {
xml.isEnabled = true
xml.destination = File("$buildDir/reports/jacoco/report.xml")
}
}
tasks.withType<Test> {
jacoco {
toolVersion = "0.8.3"
reportsDir = file("$buildDir/reports/jacoco")
}
finalizedBy("jacocoTestReport")
}
Error - Task with name 'test' not found in project ':app'
Also tried https://docs.gradle.org/current/userguide/jacoco_plugin.html
How to generate and publish Code Coverage in gradle kotlin using Azure DevOps?
According to the error message:
Error - Task with name 'test' not found in project ':app'
It seems the task called test is not provided in the project. When using a JVM language plugin like Java in Gradle, a task called test is automatically provided. This task runs all tests under src/test by default.
The Java plugin adds a number of tasks to your project, including the test task:
The Java Plugin:
So, make sure you have add plugin Java in Gradle.
You could check this document for some more details.
Hope this helps.

how to get Dagger2 Compiler Options working?

I'm trying to use 3 of the dagger2 compiler options in my android project.
but it seems none of them actually work.
I have pasted the code from here to my gradle.properties and even compiler options of AS settings.
the 3 that I'm interested in are:
-Adagger.fastInit=enabled
-Adagger.formatGeneratedSource=disabled
-Adagger.gradle.incremental
the fastinit and codeformatting just don't work (judging by the code that is generated) but the incremental cause a compile error saying:
no compiler option found.
the versions that I'm using are:
dagger : 2.18
gradle : 5.2.1
kotlin : 1.3.21
androidPlugin : 3.3.1
For projects with multiple modules, the top build.gradle can be updated with this
allprojects {
repositories {
...
}
afterEvaluate {
extensions.findByName('kapt')?.arguments {
arg( "dagger.formatGeneratedSource", "disabled" )
}
}
}
Perhaps you should try without "A"
dagger.fastInit=enabled
dagger.formatGeneratedSource=disabled
dagger.gradle.incremental=enabled
Also can try directly in build.gradle, but this should be done for each project.
kapt {
arguments {
arg('dagger.fastInit', 'enabled')
arg('dagger.formatGeneratedSource', 'disabled')
arg('dagger.gradle.incremental', 'enabled')
}
}

Ionic Could not find support-vector-drawable.aar

Having a weird issue on my Ionic app, I was able to build just fine yesterday but on one build it downloaded a bunch of files like it does when building android and then I got the following error:
Could not find support-vector-drawable.aar (com.android.support:support-vector-drawable:27.1.1).
Searched in the following locations:
https://jcenter.bintray.com/com/android/support/support-vector-drawable/27.1.1/support-vector-drawable-27.1.1.aar
When following the link https://jcenter.bintray.com/com/android/support/support-vector-drawable/27.1.1/support-vector-drawable-27.1.1.aar the page has the following JSON:
{
"errors": [
{
"status": 404,
"message": "Could not find resource"
}
]
}
Glad to know I'm not the only one. This happened to me too.
I've had to use the cordova-android-support-gradle-release plugin in the past to handle conflicts with different plugins leveraging different versions of the android support libraries. I had been using this cordova plugin with version 27.+. Changing that to force version 27.1.0 got things building again for me. (A command to add that plugin is below).
cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=27.1.0
Obviously it would be nice to know why this 27.1.1 file went missing today, which would allow continuing to use 27.+. However, hopefully this gets you running again.
----2/6/2019 Update:----
This issue was resolved in my project for the past 4 months. Then today it came back. For some reason the cordova-android-support-gradle-release .gradle file wasn't getting added to the build (even though others were). I followed the answer from #Moofish, and removed/re-installed the plugin (at 27.1.0 again). Then builds started working again. For me this did upgrade the cordova-android-support-gradle-release plugin from #1.4.4 to #2.0.1. Not sure if that was a fluke or a predictable thing.
I will leave a different solution from BRass' in case you don't want to play around with your plugins or Android support versions.
We had the exact same errors when trying to build our app and solved it by adding a script hook on after_platform_add to re-order the repository list in build.gradle file so the project looks for the .aar in a different place.
// Add <hook src="path/to/after_platform_add.js" type="after_platform_add" /> to your config.xml
var fs = require('fs');
module.exports = function(ctx) {
var gradlePath = './platforms/android/build.gradle';
var gradleFile = fs.readFileSync(gradlePath, 'ascii');
if (ctx.opts.platforms[0].indexOf('android') !== -1) {
gradleArray = gradleFile.split('\n');
for (var i = 0; i < gradleArray.length; i++) {
if (gradleArray[i].includes('jcenter()') && gradleArray[i + 1].includes('maven')) {
var jcenter = gradleArray.splice(i, 1)[0];
gradleArray.splice(i + 3, 0, jcenter);
}
}
gradleFile = gradleArray.join('\n');
fs.writeFileSync(gradlePath, gradleFile);
console.log('Reordered repositories');
}
}
I had the same problem and I already installed the plugin of cordova-android-support-gradle-release, so I removed the plugin (ionic cordova plugin rmcordova-android-support-gradle-release) and installed again the plugin (cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION=27.1.0), emmm...and it worked!
Try changing the build.gradle in platforms and in app/build.gradle to:
{
mavenCentral()
google() // Add this
jcenter()
maven {
url "https://maven.google.com"
}
}

Categories

Resources