I´m trying to migrate my android project to using gradle kotlin dsl, replacing all build.gradle files with build.gradle.kts files and using kotlin there. Already before, I used to have a kotlin file containing object elements with library and version constants (in buildSrc -> src -> main -> kotlin), like:
object Versions {
const val anyLibVersion = "1.0.0"
}
object Lib {
const val anyLib = "x:y:${Versions.anyLibVersion}"
}
In build.gradle files, I can access these constants without problems, but as soon as I switch them to build.gradle.kts, it cannot resolve them anymore. Any explaination for that?
What you'd typically have is following in buildSrc/build.gradle.kts
import org.gradle.kotlin.dsl.`kotlin-dsl`
repositories {
jcenter()
}
plugins {
`kotlin-dsl`
}
and then have your versions/dependencies in say buildSrc/src/main/java/Dependencies.kt
You can fix it by completely removing the android {} block, doing a Gradle sync, and then adding the code block back.
Does NOT treat the buildSrc directory as an module in settings.gradle.kts (.)
Precompiled script plugins.
Then for apply from in Kotlin DSL, you should use plugins {}
build.gradle.kts (X:buildSrc)
plugins {
`java-gradle-plugin`
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}
repositories {
mavenCentral()
}
Precondition
buildSrc/src/resources/META-INF/gradlePlugins/some-package-id.properties file:
implementation-class=gradlePlugins.android.AndroidLibraryPlugin
buildSrc/src/main/kotlin/gradlePlugins/android/AndroidLibraryPlugin.kt
Pay attention to the package of implementation-class will be "some-package-id"
package gradlePlugins.android
import org.gradle.api.Plugin
import org.gradle.api.Project
class AndroidLibraryPlugin : Plugin<Project> {
override fun apply(project: Project) {
project.configurePlugins()
project.configureAndroid()
project.configureDependencies()
}
}
Consume the plugin:
Check that "some-package-id" will be the file name of some-package-id.properties
feature.gradle.kts
plugins {
id("some-package-id")
}
GL
Precompiled script plugins
Use buildSrc to abstract imperative logic
Kotlin Dsl Samples
Source
Commit
Related
I am trying to create a custom gradle plugin to make use of the gradle conventions for a multi-module project.
I have the buildSrc module where in the settings.gradle file I apply the "de.fayard.refreshVersions" plugin for managing the dependencies versions, while in the build.gradle file I registered the custom plugins and using kotlin-dsl.
I firstly tried to move the logic to the build-logic module following the nowinandroid project, but, because I am using the refreshVersions plugin, this could not be possible as the plugin does not support it.
The following code shows the build.gradle file in which I registered my custom plugin.
plugins {
val kotlinVersion = "1.7.10"
`kotlin-dsl`
kotlin("jvm") version kotlinVersion
kotlin("plugin.serialization") version kotlinVersion
}
dependencies {
compileOnly(Android.tools.build.gradlePlugin)
compileOnly("org.jetbrains.kotlin:kotlin-gradle-plugin:_")
implementation(KotlinX.serialization.json)
}
gradlePlugin {
plugins {
register("feature-plugin") {
id = "com.example.myapplication.feature-plugin"
implementationClass = "com.example.myapplication.featureplugin.FeaturePlugin"
}
register("hilt-plugin") {
id = "com.example.myapplication.hilt-plugin"
implementationClass = "com.example.myapplication.hiltplugin.HiltPlugin"
}
}
}
While the code below is the actual custom plugin in which I applied some plugins, dependencies and, using the LibraryExtension, I configured the app flavours for the modules and some gradle features.
package com.example.myapplication.featureplugin
import AndroidX
import Consts
import com.android.build.api.dsl.LibraryExtension
import com.example.myapplication.projectextensions.configureKotlinAndroid
import com.example.myapplication.projectextensions.configureFlavors
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.configure
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.project
class FeaturePlugin : Plugin<Project> {
override fun apply(project: Project) {
with(project) {
pluginManager.apply {
apply("com.android.library")
apply("kotlin-android")
apply("kotlin-kapt")
// Custom plugin for Hilt
apply("com.example.myapplication.hilt-plugin")
}
extensions.configure<LibraryExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = Consts.AndroidTargetSdk
configureFlavors(this)
}
dependencies {
add("implementation", AndroidX.core.ktx)
add("implementation", AndroidX.lifecycle.viewModelCompose)
add("implementation", AndroidX.hilt.navigationCompose)
}
}
}
}
In my project modules, where these configurations are needed, I added the custom plugin's id registered, also I deleted all the dependencies, plugins and features that those modules have in common.
plugins {
id("com.example.myapplication.feature-plugin")
}
android {
namespace = "com.example.myapplication.feature.login"
}
dependencies {
// Navigation
implementation(AndroidX.navigation.compose)
// region Compose
implementation(platform(AndroidX.compose.bom))
implementation(AndroidX.compose.ui.text)
implementation(AndroidX.compose.material.icons.core)
implementation(AndroidX.compose.material.icons.extended)
// Accompanist
implementation(Google.accompanist.pager)
implementation(Google.accompanist.pager.indicators)
// endregion
}
When I build the project I get this error:
Unable to load class 'com.android.build.api.dsl.LibraryExtension'.
This is an unexpected error. Please file a bug containing the idea.log file.
It all works fine if I don't include the following LibraryExtension's block in my custom plugin.
extensions.configure<LibraryExtension> {
configureKotlinAndroid(this)
defaultConfig.targetSdk = Consts.AndroidTargetSdk
configureFlavors(this)
}
Any idea of how to fix this problem and getting the plugin to be installed in my app modules recognising the LibraryExtension?
To note:
kotlin version = 1.7.10
android plugin = 7.3.1
gradle wrapper = 7.4.2
I also link the project's github repository here.
check this answer for your question, i believe it is gonna solve your problem.
I have a multi-module Android project. The Modules are: "app", "core", "auth", "coreAndroid", and various feature modules. I am trying to add Firebase Crashlytics to the "core" module that is kotlin-only, so that I can create a function like:
fun cLog(msg: String?){
FirebaseCrashlytics.getInstance().log(msg)
}
which will be accessed by all kotlin-only modules (like networking) to log error messages.
I have followed the official documentation but I can get the FirebaseCrashlytics Instance only in Android Modules. The google-services.json file is in the "app" module. I have been trying various solutions from the web for 3 days now but nothing seems to work.
The way that I have structured my gradle files is that I have an android-library-build.gradle and a library-build.gradle file, each of which act as a common ground for for their respective android and kotlin-only modules.
These are my gradle files:
(app) build.gradle.kts
plugins {
id("com.android.application")
kotlin("android")
kotlin("plugin.serialization") version Kotlin.version //"1.6.10"
kotlin("kapt")
id ("com.google.firebase.crashlytics")
id ("com.google.gms.google-services")
}
...
dependencies {
...
implementation (platform("com.google.firebase:firebase-bom:31.2.0"))
implementation ("com.google.firebase:firebase-analytics-ktx")
implementation ("com.google.firebase:firebase-crashlytics-ktx")
}
(project) build.gradle.kts
buildscript {
val kotlin_version by extra("1.8.0")
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
dependencies {
...
// Dependency for the Google services Gradle plugin
classpath("com.google.gms:google-services:4.3.15")
// Dependency for the Crashlytics Gradle plugin
classpath("com.google.firebase:firebase-crashlytics-gradle:2.9.4")
}
}
android-library-build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
apply plugin: 'androidx.navigation.safeargs.kotlin'
...
dependencies {
...
implementation (platform("com.google.firebase:firebase-bom:31.2.0"))
implementation ("com.google.firebase:firebase-analytics-ktx")
implementation ("com.google.firebase:firebase-crashlytics-ktx")
}
library-build.gradle
apply plugin: 'java-library'
apply plugin: 'org.jetbrains.kotlin.jvm'
dependencies{
...
implementation (platform("com.google.firebase:firebase-bom:31.2.0"))
implementation ("com.google.firebase:firebase-analytics-ktx")
implementation ("com.google.firebase:firebase-crashlytics-ktx")
}
apply {
plugin("com.google.firebase.crashlytics")
plugin("com.google.gms.google-services")
}
(core) build.gradle.kts
apply {
from("$rootDir/library-build.gradle")
}
plugins {
kotlin(KotlinPlugins.serialization) version Kotlin.version
}
dependencies {
...
}
I have also tried:
to implement directly the dependencies and the plugins in the core build.gradle.kts file without success (with the platform implementation, and with the separate dependencies implementation).
to copy/paste the google-services.gson file in the core module
to use Firebase on Android without the google-services plugin meaning that I copy/pasted the generated strings from app/build/generated/res/google-services/debug/main/values/values.xml to the strings.xml file of app module, and deleted the gson file with the relevant plugins, and still didn't work.
I really can't figure it out. Any help would be deeply appreciated.
After the comments of #CommonsWare, it seems that its not possible to use directly the crashlytics library in a kotlin-only module.
I have found another way using an Interface in the koltin-only "core" module, and its implementation in the "app" module that has access to the crashlytics libraries.
Here is a simple example:
"core" module:
interface CrashlyticsService {
fun cLog(message: String?)
}
"app" module:
class FirebaseCrashlyticsService : CrashlyticsService {
override fun cLog(message: String?){
message?.let {
FirebaseCrashlytics.getInstance().log(it)
}
}
}
And then, wherever I want to use the cLog function in other modules that depend on "core" i just inject it in the constructor of the Class, i.e. :
class NetworkingServiceImpl(
private val httpClient: HttpClient,
private val crashlyticsService: CrashlyticsService
) : NetworkingService {
override suspend fun downloadVideo(videoUrl: String): ByteArray {
crashlyticsService.cLog("This is a crash test message")
return httpClient.get(videoUrl).body()
}
}
Can I include local library module on the android sourceset in kotlin multiplatform?
If so, how do we do that?
I have tried adding
api(project(":local-library-one"))
api(project(":local-library-two"))
in android source-set of build.gradle.kts file.
It fails.
You have to make your "local-library" multiplatform too. It can be only targeted to android, so you don't need to modify anything but build.gradle file, something like this:
plugins {
kotlin("multiplatform")
id("com.android.library")
}
android {
// your setup
}
kotlin {
android()
sourceSets {
val androidMain by getting {
dependencies {
// your deps
}
}
}
}
I'm trying to develop a gradle plugin to use it for generating some objects and methods for our api using some scheme.
I have followed some tutorials but they all seem not to work, atleast for me.
Some of these tutorials were:
https://musings.animus.design/kotlin-poet-building-a-gradle-plugin/
https://medium.com/#magicbluepenguin/how-to-create-your-first-custom-gradle-plugin-efc1333d4419
I have not used the buildSrc module because I'm already using it for Kotlin DSL, so I decided to create a new module and create my plugin there.
My plugin's module build.gradle.kts looks like this:
plugins {
id("java-gradle-plugin")
id("kotlin")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath(config.ClassPaths.androidBuildTools)
classpath(config.ClassPaths.kotlinGradlePlugin)
}
}
repositories {
google()
jcenter()
}
dependencies {
implementation(config.ClassPaths.androidBuildTools)
implementation(config.ClassPaths.kotlinGradlePlugin)
}
gradlePlugin {
plugins {
create("Generator") {
id = "Generator"
implementationClass = "Generator"
}
}
}
My projects settings.gradle.kts looks like this:
include(":SampleProject", ":scheme-generator")
And in my application module's build.gradle.kts I'm applying this plugin like this:
apply(plugin = "Generator")
The build script stops here with an error: plugin 'Generator' not found
My Generator class looks like this:
class Generator : Plugin<Project> {
override fun apply(target: Project) {
target.android().variants().all { variant ->
// Make a task for each combination of build type and product flavor
val myTask = "myFirstTask${variant.name.capitalize()}"
// Register a simple task as a lambda. We can later move this to its own
// class to make our code cleaner and also add some niceties.
target.tasks.create(myTask){task ->
// Group all our plugin's tasks together
task.group = "MyPluginTasks"
task.doLast {
File("${target.projectDir.path}/myFirstGeneratedFile.txt").apply {
writeText("Hello Gradle!\nPrinted at: ${SimpleDateFormat("HH:mm:ss", Locale.getDefault()).format(Date())}")
}
}
}
}
}
}
The android and variants methods are declared in a utils file, they look like this:
object GeneratorUtils {
fun Project.android(): BaseExtension {
val android = project.extensions.findByType(BaseExtension::class.java)
if (android != null) {
return android
} else {
throw GradleException("Project $name is not an Android project")
}
}
fun BaseExtension.variants(): DomainObjectSet<out BaseVariant> {
return when (this) {
is AppExtension -> {
applicationVariants
}
is LibraryExtension -> {
libraryVariants
}
else -> throw GradleException("Unsupported BaseExtension type!")
}
}
}
I have tried many things, but I seem not to get this right.
EDIT:
Using the buildSrc module for my plugin works totally fine, the plugin is applied and the gradle tasks are visible. However, buildSrc is reserved for other purposes, and we would like our plugin to be in a separate module, so we will be able to use it in other projects.
EDIT 13/04/2021
I have managed to see the tasks that are added by my plugin in my app tasks list by including this module as a composite build.
My settings.gradle now looks like this:
pluginManagement {
includeBuild("generator")
}
include(":SampleProject")
build.gradle of my plugin looks like this:
apply plugin: 'java-gradle-plugin' // Allows us to create and configure custom plugins
apply plugin: 'kotlin' //Needed as we'll write our plugin in Kotlin
buildscript {
ext {
kotlin_version = '1.4.31'
gradle_version = '4.1.2'
}
repositories {
google()
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.android.tools.build:gradle:$gradle_version"
}
}
repositories {
google()
jcenter()
}
dependencies {
// Android gradle plugin will allow us to access Android specific features
}
gradlePlugin {
plugins {
create("Generator") {
id = "Generator"
implementationClass = "com.example.Generator"
}
}
}
And my generator class now looks like this:
class Generator : Plugin<Project> {
override fun apply(project: Project) {
project.tasks.register("generationTask") { task ->
task.apply {
group = "generation"
actions.add(Action {
print("Hello from generation task!")
})
}
}
}
}
I can see generationTask in my tasks list and I can execute it normally. It prints the text without any problems.
The problem now is to include com.android.tools.build:gradle:4.1.2 in my dependecies to use it to access build types and flavors and their paths to save my generated code there. When I add it to my dependencies block, gradle fails with this error: Could not find com.android.tools.build:gradle:4.1.2.
How can I solve this problem?
Gradle build goes through specific set of phases, and the Configuration phase comes before the Execution phase. So you cannot use a plugin, which is built in the same build process, because by the time gradle tries to use it on Configuration phase, the plugin has not been built yet.
buildSrc directory is a special one, it's built not as part of the same build process, but in a separate build, before the main build process starts. This feature is called included or composite build. buildSrc is just a pre-defined way to set up a composite build and you can define your own included builds. So to make your plugin visible to the main build, you need to put it into a separate build and include this build into a composite build as described in the doc above. Here is an article describing how to transform a plugin defined in buildSrc into a composite build.
I want to make a multi module app with separate Data module. Can I import firebase in a plain kotlin module, and if not, how should I modify my Data module to be able to import firebase into it?
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.google.gms:google-services:4.3.2'
}
}
plugins {
id "org.jetbrains.kotlin.jvm"
}
dependencies {
def moduleDependencies = rootProject.ext.remoteDependencies
def moduleTestDependencies = rootProject.ext.remoteTestDependencies
implementation moduleDependencies.firestore
implementation moduleDependencies.firestoreKtx
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
apply plugin: 'com.google.gms.google-services'
This is my current build file inside my data module. Currently I am able to sync, and build, but I can't import firebase libraries inside the module.
Edit: The problem is that I want to use firestore inside a module other than the app module, but when I add the dependencies for it inside the data module, I can't use Firestore (because of unresolved reference)
The problem was that I tried to use firestore inside a pure kotlin library, to solve the problem I had to add:
apply plugin: 'com.android.library', and apply plugin: 'kotlin-android', and an android block to the gradle file of the module. (Basically I had to convert my kotlin library to an android library)