Trying to share com.apollographql.apollo3 between my kmm gradle files - android

Working on an Android project that has a lot of KMM modules, so I've tough I would extract a common gradle file and simply use it from the project specific gradle files.
My common gradle file is shared-library.gradle.kts
package commons
import dependencies.Dependencies
import dependencies.TestDependencies
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.apollographql.apollo3")
id("com.android.library")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val commonMain by getting {
dependencies {
implementation(Dependencies.Koin.CORE)
implementation(Dependencies.Result.KMM)
implementation(Dependencies.Coroutines.CORE)
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test"))
implementation(TestDependencies.KOIN)
}
}
val androidMain by getting
val androidTest by getting
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
}
val iosX64Test by getting
val iosArm64Test by getting
val iosSimulatorArm64Test by getting
val iosTest by creating {
dependsOn(commonTest)
iosX64Test.dependsOn(this)
iosArm64Test.dependsOn(this)
iosSimulatorArm64Test.dependsOn(this)
}
}
}
android {
compileSdk = BuildAndroidConfig.COMPILE_SDK_VERSION
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = BuildAndroidConfig.MIN_SDK_VERSION
targetSdk = BuildAndroidConfig.TARGET_SDK_VERSION
}
}
And then I can go use it like this from a build.gradle.kts
import dependencies.Dependencies
plugins {
id("commons.shared-library")
}
....
This all works great except the id("com.apollographql.apollo3") part, when added in the shared gradle file I get the following compilation error
org.gradle.internal.exceptions.LocationAwareException: Precompiled script plugin '/Users/calin/Playground/SharedAppSample/buildSrc/src/main/kotlin/commons/shared-library.gradle.kts' line: 1
Plugin [id: 'com.apollographql.apollo3'] 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)
I see that the plugin is available as a gradle plugin https://plugins.gradle.org/search?term=com.apollographql.apollo3
And I have setting.gradle.kts configured like this
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
But for some reason the KMM Gradle file will ignore this configuration (maybe?)

The error says that it can't use the plugin repositories because no version is provided.
- Plugin Repositories (plugin dependency must include a version number for this source)
There are a few different ways of providing a version. Using buildSrc is my preferred way.
If shared-library.gradle.kts is a buildSrc convention plugin, then add a dependency using the plugins Maven coordinates (not the plugin ID!) in ./buildSrc/build.gradle.kts.
The Maven coordinates are available from the Gradle plugin portal
// ./buildSrc/build.gradle.kts
...
dependencies {
implementation("com.apollographql.apollo3:apollo-gradle-plugin:3.5.0")
}

Try adding the version after the apollo plugin id
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.apollographql.apollo3").version("3.6.2")
id("com.android.library")
}
Make sure the version matches the apollo-runtime version in dependencies, if you have it.

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.

How to update latest gradle version of kotlin library

I created a library in multiplatform in latest intellij. My intellj added the outdated gradle version
settings.gradle.kts
pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == "com.android") {
useModule("com.android.tools.build:gradle:4.1.2"). // How to update to latest version and what is the use of 4.1.2?
}
}
}
}
rootProject.name = "xyz"
I commented above in my code. Can someone guide how can I update my gradle version to latest and what is the use of 4.1.2 that piece of code?
I tried to remove 4.1.2 below piece of code then I am getting issue
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == "com.android") {
useModule("com.android.tools.build:gradle:4.1.2")
}
}
}
Error
Build file '/Users/vmodi/IdeaProjects/abc/build.gradle.kts' line: 1
Plugin [id: 'com.android.application'] was not found in any of the following sources:
gradle-wrapper.properties
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
build.gradle.kts
plugins {
kotlin("multiplatform") version "1.6.21"
id("com.android.application")
}
group = "com.abc"
version = "0.0.1"
repositories {
google()
mavenCentral()
}
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64()
sourceSets {
val ktorVersion = "2.0.0"
val commonMain by getting {
dependencies {
implementation("io.ktor:ktor-client-core:$ktorVersion")
implementation("io.ktor:ktor-client-logging:$ktorVersion")
implementation("io.ktor:ktor-client-content-negotiation:$ktorVersion")
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktorVersion")
implementation("io.ktor:ktor-client-auth:$ktorVersion")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-core:1.3.2")
implementation("io.insert-koin:koin-core:3.2.0-beta-1")
}
}
val androidMain by getting {
dependencies {
implementation("io.ktor:ktor-client-okhttp:$ktorVersion")
implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
}
val iosX64Main by getting
val iosArm64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosX64Main.dependsOn(this)
iosArm64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("io.ktor:ktor-client-darwin:$ktorVersion")
}
}
}
}
}
android {
compileSdk = 21
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
applicationId = "com.abc.kotlinmultiplatform"
minSdk = 21
targetSdk = 31
}
#Suppress("UnstableApiUsage")
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
}
IntelliJ version
IntelliJ IDEA 2022.1.1 (Ultimate Edition)
Build #IU-221.5591.52, built on May 10, 2022
Licensed to Vivek Modi
For educational use only.
Runtime version: 11.0.14.1+1-b2043.45 x86_64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 11.6.5
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 16
Non-Bundled Plugins:
com.intellij.nativeDebug (221.5591.54)
org.jetbrains.kotlin-js-inspection-pack-plugin (0.0.9)
Kotlin: 221-1.6.21-release-337-IJ5591.52
After #PylypDukhov suggestion, trying to update gradle 7.0.4 I am getting this weird error
error
Failed to query the value of property 'namespace'.
Package Name not found in /Users/vmodi/IdeaProjects/abc/src/androidMain/AndroidManifest.xml, and namespace not specified. Please specify a namespace for the generated R and BuildConfig classes via android.namespace in the module's build.gradle file like so:
android {
namespace 'com.example.namespace'
}
settings.gradle.kts
pluginManagement {
repositories {
google()
gradlePluginPortal()
mavenCentral()
}
resolutionStrategy {
eachPlugin {
if (requested.id.namespace == "com.android") {
useModule("com.android.tools.build:gradle:7.0.4")
}
}
}
}
rootProject.name = "LetsGetCheckedKotlinMultiplatform"
Added androidMain
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.abc"/>
4.1.2 is android gradle plugin version, it's not related to gradle version.
Android gradle plugin is shipped with Android Studio, if you wanna use latest version mentioned by #Egor, you have to use Android Studio.
IntelliJ IDEA supports them with a delay - the last supported version at the moment is 7.0.4, which works fine to me, the only problem you might have with it is the lack of support for the 32 target version of Android - but it's not required to upload the app and will work fine on 32 devices, also you can build release version in AS with newest plugin, and develop in IDEA, as to me it seems more performant in KMM projects.
4.1.2 is the version number. To find out what the latest version is, visit Google's Maven Repository. At the time of writing, the latest stable version is 7.2.0, so simply replace "4.1.2" with "7.2.0" and rebuild the project.

Android Grade could not resolve androidx.room:room-runtime:2.4.2

I am trying to upgrade this Modular project to latest dependencies but gradle build fails with could not resolve androidx.room:room-runtime:2.4.2 I have aleady included mavenCentral() to repositories but doesn't seem to help.
Note: same versions work on different app with monolithic architecture, not sure modular architecture has anything to do with it.
app/build.gradle
buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:${Versions.gradle}"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.kotlin}"
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:${Versions.nav}"
}
}
buildSrc/build.gradle
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
mavenCentral()
}
dependencies.kts
object Versions {
const val kotlin = "1.6.10"
const val gradle = "7.0.2"
const val room = "2.4.2"
...
}
object Libraries {
const val roomCompiler = "androidx.room:room-compiler:${Versions.room}"
const val roomRunTime = "androidx.room:room-runtime:${Versions.room}"
const val roomKtx = "androidx.room:room-ktx:${Versions.room}"
...
}
Here is a link to my Git Repo
Cheers!
I've clone your repository in upgrade branch. It seems the module doesn't get the repository.
It's because your :data:model module is used as plain kotlin project, and it can't implement room library. In fact, many of your module is not registered as Android Library.
If you intent to use the module to setup Android configuration or use android library like room, please implement library plugin in your build.gradle.
I suppose you can implement "com.android.library" to all your modules first, and please for the love of god... do a better gradle configuration for your modules.

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.

Local library module dependency kotlin multiplatform

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

Categories

Resources