Local library module dependency kotlin multiplatform - android

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
}
}
}
}

Related

Gradle - Unable to load class LibraryExtension

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.

Intellisense not working in Kotlin Multiplatform Library

I have a kotlin multiplatofrm library that is included into an Android and iOS app.
In my android project include it as a composite build (MyLib). But Intellisense is not working at all for all code from in MyLib, though the whole thing compiles fine. I am using Android Studio. What could be wrong and how can I debug it?
rootProject.name='xxx'
includeBuild 'MyLib'
include ':common'
include ':app'
MyLib's build.gradle.kts looks as follows:
plugins {
kotlin("multiplatform") version "1.5.31"
kotlin("native.cocoapods") version "1.5.31"
}
repositories {
mavenCentral()
maven { setUrl("https://dl.bintray.com/kotlin/kotlinx.html/") }
}
group = "com.xxx.MyLib"
// CocoaPods requires the podspec to have a version.
version = "1.0"
kotlin {
ios()
jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}
cocoapods {
ios.deploymentTarget = "11.4"
frameworkName = "MyLib"
summary = "xxx"
homepage = "xxx"
podfile = project.file("../../iOS-App/Podfile")
}
sourceSets {
commonMain {
dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.5.31")
implementation("com.badoo.reaktive:reaktive:1.2.0")
implementation("com.badoo.reaktive:reaktive-annotations:1.2.0")
implementation("org.jetbrains.kotlinx:kotlinx-datetime:0.3.1")
implementation("com.russhwolf:multiplatform-settings-no-arg:0.8.1")
implementation("net.swiftzer.semver:semver:1.1.1")
}
}
}
}
tasks.withType<GenerateModuleMetadata> {
enabled = true
}
I think this is likely related to https://youtrack.jetbrains.com/issue/KTIJ-18903
I had the same issue and it drove me crazy. How can one write code nowadays without Intellisense (answer: you can't).
I tried a ton of things (all the usual and unusual stuff you do when Android Studio / IntelliJ act up). Ultimately I upgraded to Kotlin 1.6.0-RC2 (from 1.5.31) -> https://github.com/JetBrains/kotlin/releases/tag/v1.6.0-RC2.
Part of that is upgrading the Kotlin Plugin:
Another part is the Kotlin Gradle Plugin dependency:
org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0-RC2
And last but not least I had to downgrade the corouting dependency (from 1.5.2):
org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0-RC
After that everything was back to normal.

Android Gradle: Custom Plugin with id 'XXXX' not found - Kotlin DSL

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.

Compiler cannot resolve classes in io.ktor.client.features.logging

I'm trying to add logging for Ktor http requests in Android application. According to docs I have to add gradle dependency
implementation "io.ktor:ktor-client-logging:$ktor_version"
and just use this snippet
val client = HttpClient() {
install(Logging) {
logger = Logger.DEFAULT
level = LogLevel.HEADERS
}
}
Problem is that compiler "ignores" package 'io.ktor.client.features.logging' added as a dependency. What's strange is that JsonFeature (added as similar dependency) works just fine.
install(JsonFeature) { // perfectly works
...
}
install(Logging) { // unresolved reference
...
}
I already checked .jar file that gradle added to the project, it contains all expected classes, I can open them and see the source code, but magically just can't use in my app. After hours of research I guess it may be somehow related to gradle metadata or that logging feature is multiplatform and some additional gradle configuration is required, but unfortunately I'm not a gradle expert.
I tried adding enableFeaturePreview("GRADLE_METADATA") to settings.gradle, but no effect. Even tried to add "-jvm" to dependency.
implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"
With this dependency Android Studio finding package successfully, but fails to compile with following error
More than one file was found with OS independent path 'META-INF/ktor-http.kotlin_module'
Can anyone please clarify how to properly configure dependency for Ktor logger?
For the ktor-client-logging you have to have the dependency set for each platform:
commonMain {
dependencies {
implementation "ch.qos.logback:logback-classic:1.2.3"
implementation "io.ktor:ktor-client-logging:$ktor_version"
}
}
androidMain {
dependencies {
implementation "io.ktor:ktor-client-logging-jvm:$ktor_version"
}
}
iosMain {
dependencies {
implementation "io.ktor:ktor-client-logging-native:$ktor_version"
}
}
as for the meta META-INF/ktor-http.kotlin_module add to the app/build.gradle inside the android {} block:
android {
packagingOptions {
exclude 'META-INF/common.kotlin_module'
exclude 'META-INF/*.kotlin_module'
}
}

Unresolved references of buildSrc kotlin constants after migration to gradle kotlin dsl

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

Categories

Resources