i migrated my gradle file to kotlin dsl from groovy. Everything works fine but there is a problem with improting values to gradle from another kotlin file. I can import variables and autocomplete just works fine but when i build project, it gives me Unresolved reference error.
Appreciate any help.
Gradle version : 7.1.1
Android studio : Arctic Fox
build.gradle.kts that i created under buildSrc :
plugins {
`java-gradle-plugin`
`kotlin-dsl`
`kotlin-dsl-precompiled-script-plugins`
}
repositories {
mavenCentral()
}
File that i import from in buildSrc/src/main/java/Dependencies :
object Versions {
const val compose_version = "1.0.0"
}
object Dependencies {
const val compose_ui = "androidx.compose.ui:ui:${Versions.compose_version}"
const val compose_material = "androidx.compose.material:material:${Versions.compose_version}"
const val compose_ui_tooling_preview = "androidx.compose.ui:ui-tooling-preview:${Versions.compose_version}"
const val compose_ui_tooling = "androidx.compose.ui:ui-tooling:${Versions.compose_version}"
const val compose_ui_test = "androidx.compose.ui:ui-test-junit4:${Versions.compose_version}"
}
app build.gradle.kts file :
plugins {
id("com.android.application")
kotlin("android")
}
android {
compileSdk = 30
defaultConfig {
applicationId = "com.example.compose"
minSdk = 21
targetSdk = 30
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
debug {
isMinifyEnabled = false
isDebuggable = true
}
release {
isMinifyEnabled = true
isDebuggable = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}
}
compileOptions {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = "1.0.0"
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.6.0")
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("com.google.android.material:material:1.4.0")
// Jetpack Compose
implementation(Dependencies.compose_ui)
implementation(Dependencies.compose_material)
implementation(Dependencies.compose_ui_tooling_preview)
debugImplementation(Dependencies.compose_ui_tooling)
androidTestImplementation(Dependencies.compose_ui_test)
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
implementation("androidx.activity:activity-compose:1.3.0")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
}
You said that your other file is under buildSrc/src/main/java/Dependencies. Since this is a Kotlin file, it should be buildSrc/src/main/kotlin/Dependencies instead.
Related
When I upgrade gradle version from 7.33 to 7.41 or higher. Every line in every gradle.kts become unresolved. I tried to replace all lines(except namespace) in corresponding files of a new project created from Android Studio template and there is no problem. I can't figure out what's wrong in my existing project.
project build.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
//id("dagger.hilt.android.plugin")
id("com.google.devtools.ksp") version "1.6.21-1.0.5"
}
val composeVersion: String = rootProject.extra["composeVersion"] as String
val coroutinesVersion: String = rootProject.extra["coroutinesVersion"] as String
android {
compileSdk = 32
defaultConfig {
applicationId = "com.app.myapp"
minSdk = 24
targetSdk = 32
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
)
}
getByName("debug") {
applicationIdSuffix = ".debug"
versionNameSuffix = "-debug"
}
}
compileOptions {
isCoreLibraryDesugaringEnabled = true
sourceCompatibility(JavaVersion.VERSION_11)
targetCompatibility(JavaVersion.VERSION_11)
}
tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.freeCompilerArgs += "-opt-in=org.mylibrary.OptInAnnotation"
kotlinOptions.jvmTarget = JavaVersion.VERSION_11.toString()
}
buildFeatures {
compose = true
}
composeOptions {
kotlinCompilerExtensionVersion = composeVersion
}
packagingOptions {
resources {
excludes += "/META-INF/{AL2.0,LGPL2.1}"
}
}
namespace = "com.app.myapp"
applicationVariants.all {
kotlin.sourceSets {
getByName(name) {
kotlin.srcDir("build/generated/ksp/$name/kotlin")
}
}
}
}
dependencies {
implementation("androidx.core:core-ktx:1.7.0")
// Jetpack Compose
implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.compose.material:material:$composeVersion")
implementation("androidx.compose.ui:ui-tooling-preview:$composeVersion")
debugImplementation("androidx.compose.ui:ui-tooling:$composeVersion")
implementation("androidx.compose.foundation:foundation:$composeVersion")
implementation("androidx.compose.material:material-icons-core:$composeVersion")
implementation("androidx.activity:activity-compose:1.4.0")
implementation("androidx.lifecycle:lifecycle-viewmodel-compose:2.4.1")
implementation("com.google.accompanist:accompanist-systemuicontroller:0.24.9-beta")
implementation("com.google.accompanist:accompanist-permissions:0.24.9-beta")
// Material design 3
implementation("androidx.compose.material3:material3:1.0.0-alpha12")
implementation("androidx.compose.material3:material3-window-size-class:1.0.0-alpha12")
// Navigation
implementation("androidx.navigation:navigation-compose:2.4.2")
implementation("io.github.raamcosta.compose-destinations:animations-core:1.5.7-beta")
ksp("io.github.raamcosta.compose-destinations:ksp:1.5.7-beta")
// Hilt
/*implementation("com.google.dagger:hilt-android:2.42")
kapt("com.google.dagger:hilt-compiler:2.42")*/
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion")
// Nordic ble library
implementation("no.nordicsemi.android.support.v18:scanner:1.6.0")
implementation("no.nordicsemi.android:ble-ktx:2.4.1")
implementation("no.nordicsemi.android:ble-common:2.4.1")
// Java 8+ API desugaring
coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.4.1")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.3")
androidTestImplementation("androidx.test.espresso:espresso-core:3.4.0")
androidTestImplementation("androidx.compose.ui:ui-test-junit4:$composeVersion")
}
kapt {
correctErrorTypes = true
}
app build.gradle.kts
buildscript {
val composeVersion by extra("1.2.0-beta02")
val coroutinesVersion by extra("1.6.1")
dependencies {
classpath("com.google.dagger:hilt-android-gradle-plugin:2.42")
}
}// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id ("com.android.application") version "7.2.0" apply false
id ("com.android.library") version "7.2.0" apply false
id ("org.jetbrains.kotlin.android") version "1.6.21" apply false
}
tasks.register("clean",Delete::class){
delete(rootProject.buildDir)
}
settings.gradle.kts
pluginManagement {
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My App"
include(":app")
I have a KMM application in which the android target uses Jetpack Compose. I am getting the following error when I try to use a drawable resource:
e: [...]/OnboardingScreen.kt: (33, 46): Unresolved reference: drawable
This is the result of trying to access a drawable via painterResource(id = R.drawable.ic_icon).
I have tried the following things to fix the issue:
Clean and build the project
Invalidate cache and restart
Fix all warnings when executing ./gradlew assembleDebug
The static R class is correctly imported
Nothing of the above solves the problem.
This is my build.gradle.kts of the android module:
plugins {
id("com.android.application")
kotlin("android")
}
val composeVersion = findProperty("version.compose") as String
val composeNavigationVersion = findProperty("version.composeNavigation") as String
val koinVersion = findProperty("version.koin") as String
android {
compileSdk = (findProperty("android.compileSdk") as String).toInt()
defaultConfig {
applicationId = "com.app.app.android"
minSdk = (findProperty("android.minSdk") as String).toInt()
targetSdk = (findProperty("android.targetSdk") as String).toInt()
versionCode = 1
versionName = "1.0"
}
buildFeatures {
compose = true
}
// Set both the Java and Kotlin compilers to target Java 8.
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
composeOptions {
kotlinCompilerExtensionVersion = composeVersion
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
}
}
}
dependencies {
implementation(project(":shared"))
// Android
implementation("com.google.android.material:material:1.4.0")
// Jetpack Compose
implementation("androidx.compose.ui:ui:$composeVersion")
implementation("androidx.compose.ui:ui-tooling:$composeVersion")
implementation("androidx.compose.foundation:foundation:$composeVersion")
implementation("androidx.compose.material:material:$composeVersion")
implementation("androidx.compose.material:material-icons-core:$composeVersion")
implementation("androidx.compose.material:material-icons-extended:$composeVersion")
implementation("androidx.activity:activity-compose:$composeVersion")
implementation("androidx.navigation:navigation-compose:$composeNavigationVersion")
// Koin
implementation("io.insert-koin:koin-android:$koinVersion")
}
configurations.all {
resolutionStrategy {
force("org.jetbrains.kotlin:kotlin-stdlib:1.5.31")
}
}
My build.gradle.kts of the shared module:
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
}
version = "1.0"
kotlin {
android()
iosX64()
iosArm64()
iosSimulatorArm64() // sure all ios dependencies support this target
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
ios.deploymentTarget = "14.1"
podfile = project.file("../ios/Podfile")
framework {
baseName = "shared"
}
}
val multiplatformVersion = findProperty("version.multiplatformSettings") as String
val koinVersion = findProperty("version.koin") as String
val coroutinesVersion = findProperty("version.coroutines") as String
sourceSets {
val commonMain by getting {
dependencies {
implementation("com.russhwolf:multiplatform-settings-no-arg:$multiplatformVersion")
implementation("io.insert-koin:koin-core:$koinVersion")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion")
}
}
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13.2")
}
}
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 = 31
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
defaultConfig {
minSdk = 22
targetSdk = 31
}
}
And finally, my gradle.properties:
# Gradle
org.gradle.jvmargs=-Xmx2048M -Dkotlin.daemon.jvm.options\="-Xmx2048M"
# Kotlin
kotlin.code.style=official
# Android
android.useAndroidX=true
android.compileSdk=31
android.targetSdk=31
android.minSdk=22
# MPP
kotlin.mpp.enableGranularSourceSetsMetadata=true
kotlin.mpp.enableCInteropCommonization=true
kotlin.native.enableDependencyPropagation=false
kotlin.native.ignoreDisabledTargets=true
# Common versions
version.multiplatformSettings=0.8.1
version.koin=3.1.4
version.coroutines=1.5.2-native-mt
# Android versions
version.compose=1.0.5
version.composeNavigation=2.4.0-rc01
Thanks in advance.
That's how you reference image resources:
import com.your.package.R
val img = R.drawable.img_name
or
val img = com.your.package.R.drawable.img_name
Denny Kurniawan's comment above pointed me in the right direction. I had a similar problem after changing the name of my project. At the top of your 'OnboardingScreen.kt' file, check to see if the full 'package' name matches your actual project's name, e.g. "package com.android.application".
If the package name listed in 'OnboardingScreen.kt' is not the same as your actual project's name, then the static R class is probably not being imported correctly and, hence, this error.
In other words, the full package name in 'OnboardingScreen.kt' should match the name of the folder shown under 'app->src->main->java'. Hope this helps someone as it did me.
for me, this error happen because I drag my image to the resource manager, I solve it by importing the images manually with the "Import Drawables" button
I have a new Android project and I want to make use of Jetpack Compose, but following the steps outlined here gives me the following build error:
Unable to load class 'org.jetbrains.kotlin.gradle.tasks.KotlinCompile'.
This is an unexpected error. Please file a bug containing the idea.log file.
Perhaps I'm compounding the issue by using Kotlin DSL build.gradle.kts, but here's my gradle setup:
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("kotlin-kapt")
id("dagger.hilt.android.plugin")
}
android {
compileSdkVersion(30)
defaultConfig {
applicationId = "..."
minSdkVersion(24)
targetSdkVersion(30)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility(JavaVersion.VERSION_1_8)
targetCompatibility(JavaVersion.VERSION_1_8)
}
buildFeatures {
dataBinding = true
compose = true
}
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
composeOptions {
kotlinCompilerVersion = "1.4.20"
kotlinCompilerExtensionVersion = "1.0.0-alpha08"
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
//...
}
Try '1.0.0-alpha09'and Kotlin version "1.4.21" and you should be good to go.
Have you got this in your buildscript?
buildscript {
ext {
compose_version = '1.0.0-alpha09'
}
ext.kotlin_version = "1.4.21"
I wanted to configure Jetpack Compose. I did it but during running the app I get that error everytime. Jetpack is in the project and I can use it but I can't run the app.
Execution failed for task ':myModule:prepareDebugKotlinCompileTask'.
Could not resolve all files for configuration ':myModule:kotlin-extension'.
Could not find androidx.compose:compose-compiler:1.0.0-alpha05.
Searched in the following locations:
- https://dl.google.com/dl/android/maven2/androidx/compose/compose-compiler/1.0.0-alpha05/compose-compiler-1.0.0-alpha05.pom
My module's Gradle file:
plugins {
id("po-android-library")
id("kotlin-kapt")
id("de.mannodermaus.android-junit5")
}
android {
defaultConfig {
renderscriptTargetApi = AndroidSdkVersions.target
renderscriptSupportModeEnabled = true
vectorDrawables.useSupportLibrary = true
}
buildFeatures {
compose = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
composeOptions {
kotlinCompilerVersion = "1.4.0"
kotlinCompilerExtensionVersion = "1.0.0-alpha05"
}
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
testOptions.unitTests.isIncludeAndroidResources = true
sourceSets {
val sharedTestDir = "src/sharedTest/java"
getByName("test") {
java.srcDir(sharedTestDir)
}
}
packagingOptions {
pickFirst("mockito-extensions/org.mockito.plugins.MockMaker")
exclude("META-INF/rxjava.properties")
exclude("META-INF/rxkotlin.properties")
}
}
dependencies {
implementation(project(":common"))
implementation(project(":android-common"))
implementation(Deps.kotlin_stdlib)
implementation("com.android.tools.build:gradle:4.1.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10")
implementation(Deps.androidx_appcompat)
implementation(Deps.koin_android_viewmodel)
implementation(Deps.koin_androidx_ext)
implementation(Deps.koin_android)
implementation(Deps.rxkotlin)
implementation("androidx.compose.runtime:runtime:1.0.0-alpha05")
implementation("androidx.compose.ui:ui:1.0.0-alpha05")
implementation("androidx.compose.foundation:foundation-layout:1.0.0-alpha05")
implementation("androidx.compose.material:material:1.0.0-alpha05")
From Compose compiler version 1.0.0-alpha04, we should use Android Studio 4.2 Canary 13 or later and com.android.tools.build:gradle version should be 4.2.0-alpha13
From the Official site reference
I'm using new beta version of Android Studio 3.4 and kotlin dsl as bulding gradle scripts. I have a lot of modules in the project and that's why I write KotlinBuildScript extension for building projects, but I have error message for all external dependencies like "ERROR: Failed to resolve: com.google.dagger:dagger:2.18", "ERROR: Failed to resolve: androidx.recyclerview:recyclerview:1.0.0". How can I fix it?
Here is my root build.gradle.kts:
buildscript {
repositories {
mavenCentral()
jcenter()
google()
maven(url = uri("https://dl.bintray.com/kotlin/kotlin-eap"))
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.10")
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
google()
maven(url = uri("https://dl.bintray.com/kotlin/kotlin-eap"))
}
}
And my KotlinBuildScript:
object Version {
val kotlin = "1.3.10"
val androidx = "1.0.0"
val constraintlayout = "1.1.3"
val lifecycle = "2.0.0"
val paging = "2.1.0-alpha01"
}
fun KotlinBuildScript.setupApplication(applicationId:String, versionCode:Int, versionName:String) =
setup(applicationId, versionCode, versionName, false)
fun KotlinBuildScript.setupLibrary(applicationId:String) = setup(applicationId, null, null, true)
private fun KotlinBuildScript.setup(
applicationId:String, versionCode:Int?, versionName:String?, library:Boolean
) {
setupPlugins(library)
setupConfiguration(library, applicationId, (versionCode ?: 1) * 2, versionName ?: "1.0")
setupDependencies(library)
}
private fun KotlinBuildScript.setupConfiguration(
library:Boolean, applicationId:String, versionCode:Int, versionName:String
) {
configure<BaseExtension> {
compileSdkVersion(28)
buildToolsVersion("28.0.3")
defaultConfig {
if (!library) {
this.applicationId = applicationId
this.versionCode = versionCode
this.versionName = versionName
}
minSdkVersion(16)
targetSdkVersion(28)
multiDexEnabled = true
vectorDrawables.useSupportLibrary = true
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
packagingOptions {
exclude("META-INF/LICENSE.txt")
}
lintOptions {
isCheckReleaseBuilds = false
}
if (library) {
defaultConfig.ndk {
abiFilters("armeabi-v7a", "arm64-v8a", "x86", "x86_64")
}
externalNativeBuild {
cmake {
setPath("CMakeLists.txt")
}
}
} else {
flavorDimensions("architecture")
productFlavors {
create("arm") {
dimension = "architecture"
this.versionCode = versionCode - 1
versionNameSuffix = "-arm"
ndk {
abiFilters("armeabi")
}
}
create("x86") {
dimension = "architecture"
this.versionCode = versionCode
versionNameSuffix = "-x86"
ndk {
abiFilters("x86")
}
}
}
}
}
}
private fun KotlinBuildScript.setupPlugins(library:Boolean) {
apply {
plugin("com.android." + (if (library) "library" else "application"))
plugin("kotlin-android")
plugin("kotlin-android-extensions")
plugin("kotlin-kapt")
}
}
private fun KotlinBuildScript.setupDependencies(library:Boolean) {
dependencies {
add("testImplementation", "junit:junit:4.12")
add("implementation", fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
if (!library) {
add("implementation", project(":Library"))
}
add("kapt", "com.google.dagger:dagger-compiler:2.18")
add("implementation", "com.google.dagger:dagger:2.18")
add("kapt", "androidx.lifecycle:lifecycle-compiler:${Version.lifecycle}")
add("implementation", "androidx.lifecycle:lifecycle-extensions:${Version.lifecycle}")
add("implementation", "androidx.lifecycle:lifecycle-common-java8:${Version.lifecycle}")
add("implementation", "javax.inject:javax.inject:1")
add("implementation", "javax.annotation:javax.annotation-api:1.2")
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${Version.kotlin}")
add("implementation", "org.jetbrains.kotlin:kotlin-reflect:${Version.kotlin}")
add("implementation", "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1")
}
}
App module gradle:
import Setup.Version
import Setup.setupLibrary
setupLibrary("com.esminis.server.library")
I've found how to solve the problem. Using kotlin dsl you should always set buildFileName property in the settings.gradle of the project.
rootProject.buildFileName = 'build.gradle.kts'
Also, try to delete .idea and .gradle folder and restart Android Studio.