Kotlin files not able to use R.java - android

I created a new layout file in the appropriate resource folder. After syncing, when I try to reference the layout file, i.e. R.layout.activity_test, R is an "unresolved symbol". When I try to build in AS it fails.
The interesting thing is that if I manually import the R file and use it in code, when I try to build on command line, it works. The other thing is when I try to access the layout file using R in a Java file, that also works. So I know R.java is being generated.
I tried creating a totally new project, and R is accessible in Kotlin files.
What is the problem here?
Things I have tried:
Cleaned then built
Invalidated Caches / Restart
Blow away .gradle folders and recreate indexes
Other info:
AS v3.0.1
Tried both Kotlin v1.1.2-4 and v1.1.60
Gradle v4.2.1
Gradle Plugin v3.0.0

Invalidate Caches/Restart worked for me.
Runtime JAR files in the classpath should have the same version. These files were found in the classpath:
C:/Users/R3ZA/.gradle/caches/modules-2/files-2.1/me.eugeniomarletti.kotlin.metadata/kotlin-compiler-lite/1.0.3-k-1.2.40/a16e967c276379eaf6e1fa03af77664f003db947/kotlin-compiler-lite-1.0.3-k-1.2.40.jar (version 1.2)
C:/Users/R3ZA/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib/1.3.20/eb2a232734e09fcd1b958a5c7520a93c6de38b32/kotlin-stdlib-1.3.20.jar (version 1.3)
C:/Users/R3ZA/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.3.20/7d7934e26ce34da1a0a8d00e38038d7cf3375e89/kotlin-stdlib-common-1.3.20.jar (version 1.3)

Add the following under android section in your application module:
android {
...
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}
This lets Android Studio know that the kotlin directory is a source
root, so when the project model is loaded into the IDE it will be
properly recognized. Alternatively, you can put Kotlin classes in the
Java source directory, typically located in src/main/java.
https://kotlinlang.org/docs/reference/using-gradle.html#android-studio

If you have a warning in your project please fix it.
in my case my Gradle version had a version Compile warning.
the App runs fine but the R class was not recognized, so I change the gradle version and fix the warnings and all works well.

Related

Android Studio generates R file only for modules with no module dependencies in multi-module project

After updating to AS 3.3.1 from 3.2.something I've got a strange bug: R file is generated only for modules that do not implement any other modules. I still can compile project or go to declaration via cmd+B, but every resource file like R.layout.activity_main is highlited with an "unresolved reference" message, until I remove every module implementation from module build.gradle and resync the project.
As far as I've noticed, the problem does not related with android gradle 3.3.0 or kotlin 1.3, since my old projects are affected as well.
What I've already tried:
Clean/Rebuild project
Invalidate cache
Resync Gradle
Switch to Android Gradle 3.2.1
delete .idea directory
Here's my project, module build.gradle example and used libraries.
For example, if I comment implementation(project(":presentation")) line in mainscreen/build.gradle.kts, the error disappears. Obviously, it isn't the solution.
UPD: SOLVED
Solution: you have to specify different package names in each AndroidManifest.xml files, i.e. com.mycompany.example.launcher for launcher module, com.mycompany.example.presentation for presentation module et cetera.
[solution1]
You can delete the old version of gradle from your system and start installing its new version.
[solution2]
Or remove Android Studio and reinstall it

Cannot create new activity in android studio

I keep getting an IllegalStateException error in the event log while creating new activity in android studio:
11:27:15 InvalidReferenceException: Error executing FreeMarker template: The following has evaluated to null or missing:
==> srcDir [in template "root://activities/common/common_globals.xml.ftl" at line 41, column 34]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
I've encountered the same problem and trying the following steps were working for me:
close android studio
delete folders:
.idea and
.gradle
re-open project using android studio
Method 1:Just Remove the following code from your app level build.gradle
debug {
storeFile file('/Users/Razz/Documents/keys/keystore.jks')
storePassword 'ABC#123'
keyAlias 'key0'
keyPassword 'ABC#123'
}
Method 2: In case, if there are two packages together (kotlin and java)
I was facing the same problem as whenever I create new activity form the activity templates provided by the android studio. Nothing was happening even after Gradle sync completes successfull.
After a long item, I figure out that, java directory name inside my project changed into kotlin somehow. And I supposed Android Studio looking for the java package to create new Activity.
Then, to solve this issue, I follow the below steps:
Closed Android Studio
Went to the project location up to java directory as ../ProjectName/app/src/main/
In this main directory, I had the kotlin directory instead of java.
So, I just renamed it to java.
Then opened the project with Android Studio, and that's all.
I created new activity it easily created one as it works before.
I have been faced that, after I setup my android environment.I already set up and configured everything, but this still occur.After searching a few mins, I found the problem in Logcat,
Then, I know project is missing Build Tool version. You have to give which build tool version you will use to run for project. Go to >> Project Structure , ( Cmd + ; ) in mac. Choose Compile Sdk Version and Build Tools Version,
After the gradle build, creating new activity from menu error has been fixed. I hope this will help for you.
Notes : Some of the projects from Github or bitbucket also missing Build Tools version when you cloned and run the project. These are the same errors and this approach can help to solve for them.
Comment the code in build.gradle which creates folders in tmp directory and then create the Activity.
After successful creation of activity ,revert back the commented code in build.gradle (Co)
allprojects {
/*String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
}*/
repositories {
jcenter()
}
}
In the file build.gradle, add
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
sychronize gradle then remove it and synchronize again. This worked for me.
allprojects {
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
repositories {
jcenter()
}
}
I experienced to same issue 2 day ago and i solve it by update the below text. Check to gradle.build app and module to newer versions.
classpath 'com.google.gms:google-services:4.3.4'
I had the same issue.
Visit this Link.
https://code.google.com/p/android/issues/detail?id=232076
this happens if you have your project in a different drive other then C:
android studio will automatically create a copy of your project (i hope they are cache files) in the C: drive's tmp folder. this folder is the cause for this issue.
try File-> Invalidate Caches/Restart. or
just deleted the tmp folder in the c: drive and restart everything and it will work fine.
removing the build folder redirect code in the gradle file is a workaround.
allprojects {
//String osName = System.getProperty("os.name").toLowerCase();
//if (osName.contains("windows")) {
// buildDir = "C:/tmp/${rootProject.name}/${project.name}"
//}
repositories {
jcenter()
}
}
Found it here https://github.com/udacity/ud851-Exercises/issues/67
I have the same exact problem when trying to create a new activity.
IDE Fatal Errors
I was running Android Studio 3.2.1 on Windows 7 Professional. I did not have this problem until I started to switch loading project from different drives - I used to run my projects from F: drive and when I downloaded sample code to C: drive, creating new activities started to give this IDE fatal error.
Got a clue from online sources of a potential issue caused by buildDir path in build.gradle(Project:) shown below;
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
Since my project working directory is in F: drive, I changed the above C: drive to F:
buildDir = "F:/tmp/${rootProject.name}/${project.name}"
With this simple change, I can now create new Activity.
If nothing else works for you I suggest you try this solution.
I had tried most of the above suggestions without any success; the directory was fine, invalidation of caches, etc. But if you right-click on the project folder, New > Activity > Gallery (all others are greyed-out), clicking on any activity template will show you the exact problem. In my case, for some reason, all of my Activities (including Basic and Empty) required minSdkVersion 16; not the original 15. I hope this works for you if others fail.

Unable to find module with Gradle path. Linking to library -unspecified instead

Update 2 This has been fixed in Android Studio 1.2 Beta 3
Update: This has reportedly been fixed in an upcoming release of Android Studio.
In Android Studio 1.2 Beta 2 I started getting the following warnings when syncing Gradle.
Failed to set up dependencies
Warning:Unable to find module with Gradle path ':Library1'. Linking to library 'Library1-unspecified' instead.
Warning:Unable to find module with Gradle path ':Library2'. Linking to library 'Library2-unspecified' instead.
I've noticed that jumping to a declaration declared in one of the library modules pulls up the Decompiler instead of just going to the actual source in the library module.
In my Android Studio Project I have the following modules:
- App
- Library1
- Library2
Module: App build.gradle snippet:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':Library1')
compile project(':Library2')
}
settings.gradle file:
include ':App'
include ':Library1'
include ':Library2'
project(':Library1').projectDir = new File(settingsDir, '../library1/lib')
project(':Library2').projectDir = new File(settingsDir, '../Library2/lib')
I faced this with Android Studio 2.2.3. The problem was with the .iml file that was causing the problem.
After importing the module folder into the project directory and adding the changes to include module in settings.gradle, delete the .iml file from the module folder and do a Gradle Sync. Then right click on any module in the Project Window and click on "Configure Project Subset...", if the imported module is not checked, check it and Android Studio should do a Gradle Sync. By then the imported module will appear in the Project Window and will have a .iml file regenerated that is does not cause any issues.
delete the .iml file under the error module.
restart Android studio
Right click project, Select "Configure Project Subset ..." and select your module, rebuild your project.
I met the same problem on AS 2.0 preview,
and I fixed it by following steps:
1.Rename the module.
2.Change the names in app/build.gradle and setting.gradle.
3.Start gradle sync.
I don't know why but it works on my issue.
Check in settings. of your app.
Down there is the field "Load/Unload Modules"
Your module might be unloaded, that's why Android Studio ignores it even if you specify it in Gradle.
At least that was the problem for me.
If you are using React-native>60, then you should run npx jetify on root of your project before doing Gradle sync.
For me the following worked in Android Studio 3.5.3
First of all, check if the path is correct in your Gradle settings.
If the path is correct then invalidate caches & restart by navigating to the top 'File' menu
It is a known bug introduced in 1.2 Beta 2.
See This issue on the Android issue tracker. Fix should be out within hours.
I had this problem and solved it by clicking on
File> Ivalidate cache / Restart...
as shown below:
Problem:
This problem can also occur when you change the path to the module.
settings.gradle
include ':app', ':filechooserlibrary'
project(':filechooserlibrary').projectDir = new File('../../repos-github/file-chooser/file-chooser-library/filechooserlibrary')
I changed the path to '../../github/file-chooser/file-chooser-library/filechooserlibrary'
The old path did not exist any more. But then, instead of accepting the new path, Android Studio recreated the old path structure thus referring to an empty directory.
Solution:
So, I deleted this obsolete path structure and restarted Android Studio, after deleting the .iml file (I don't know if this is necessary, but it cannot do harm). Then I reimported the Gradle project and it worked.
Try using an updated version of NDK.
I faced the same issue.
I was using NDK version r17c. Issue solve after using r21e.
Android Studio Version: 4.1.3
OS: Ubuntu 18.04.5

New to intellij android studio, can't open sample projects without Gradle error

i just switched to intellij and pretty new to android development.. I've been working on one app for 5 moth and now that i moved it to intellij android studio my options menus became invisible. I've been reading a lot and trying to catch up with newest features that are available now today.
By biggest pain is that im hitting this error ->
Failed to refresh Gradle project 'ActionBarCompat-ListPopupMenu' You are using Gradle version 1.8, which is not supported. Please use version 1.9. Please point to a supported Gradle version in the project's Gradle settings or in the project's Gradle wrapper (
I did search on this problem and some pages suggesting changing classpath to soething like
build:gradle:0.7.+ but that doest help..
What am i doing wrong? All i need is just to make those examples from android to work..
Thanks
The Gradle wrapper file is at the path (project-root)/gradle/wrapper/gradle-wrapper.properties. The distributionUrl property is where you set the Gradle version; it's embedded in the URL:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.9-all.zip
First make sure you've updated to the newest Android SDK Build-tools version, the most current one is 19.0.3; if you haven't, then open the Android SDK manager and update.
Then look in the build.gradle file inside your project folder (not the one in the root folder). This first couple of lines should resemble something like this:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
Set the class path to com.android.tools.build:gradle:0.9.+.
Then further along in the same file:
android {
compileSdkVersion 18
buildToolsVersion "19.0.3"
...
}
Set buildToolsVersion to 19.0.3 (the newest version).
Make sure Android Studio syncs the Gradle file changes. If it doesn't, restart Android Studio and/or rebuild the project. Then you should be good to go.
I think i figured this out.. Big thanks to Scott and Jaap.
I did updateof my intellij to 0.5.2, also i installed 19.0.3 ( newest build tools).
On few of the existing projects i had to change distributionUrl to
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
After rebuilding the project it worked like a charm.
Just a note to say : Scorr Berba's reply was probably the correct one. Also I'm not sure 1.11 can be used - I used the wrapper (there is a lot of confusion here : the wrapper is the gradlew ["gradle*w*"rapper] to set the gradle used for my samples to 1.10 and you must set up your project to use the wrapper OR you can build from the command line e.g "./gradlew build".

Cannot resolve R.java, duplicate class

I just start using Android Studio for a week and it works great for me, but when I started Android Studio today I get the error: 'error: duplicate class: mypackage.R'. I saw this error before when I used Eclipse so I tried to rebuild the project a few times and restarting Android Studio, this didn't help.
After reading some Stackoverflow questions I tried to deleted R.java and rebuild again, now I don't get any error while rebuilding. The only problem is that Android Studio cannot resolve R, so every line which uses R gets an error (project builds and run).
I also tried to delete all the R.class files but this doesn't help either. I checked if some class imports android.R, but they all don't, they just import 'mypackage.R'. I even deleted all my files and checked out a older version of my project but I still have the same problem.
Edit:
If I don't delete R.java the compiler doesn't give any error. Just when I try to build I get a lot of errors like: 'error: cannot find symbol variable button_login'. If I search the R.java file for 'button_login' I just find it. I did this with multiple errors.
Edit2:
When I deleted all the libaries the project now builds. But the problem is that I need those libaries (HTTPComponents). I added them from maven (File --> Project Structure --> Libaries --> +-sign') and added them to my settings.graddle:
buildscript {
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile files('libs/android-support-v4.jar')
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.2.5'
compile group: 'org.apache.httpcomponents', name: 'httpmime', version: '4.2.5'
//compile 'org.apache.httpcomponents-httpclient:4.2.5' doesn't work either
//compile 'org.apache.httpcomponents-httpmime:4.2.5' doesn't work either
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 15
targetSdkVersion 16
}
}
When I run gradlew.bat --stacktrace --info assemble I get errors that Gradle is unable to find the libaries from the maven repository. I checked the repository but they are available. http://search.maven.org/#browse%7C1628757896
Unfortunately I run into this as well, sometimes frequently, mostly happens when I put my computer to sleep and back on while Eclipse is on. Sometimes recompiling and validating the project works.
I did find this solution online somewhere, lost the link and do not claim credits for this:
Disable auto build, and clean the project then
Right click your project -> properties -> Java Build Path -> Source -> Add Folder -> add gen and src
Then compile
If that doesn't work, try to delete the gen folder and do that process
Let me know.
Delete the Build folder generated by Android Studio automatically!
Problem is simple usually. You have fresh instal of studio? So you just fogot add SDK support for those version Android in wich try to compile project. Android Studio can`t generate R file. Start SDK manager, install all missing SDK version and rebuld project.
Another problem can be if you use 64bit OS. You ned to install 32 bit lib, because aapt can`t work with 64bit libs.
in Ubuntu install ia32-libs
Fedora described here
in other distr find libs in google (just google:ia32-libs for "mydistr"), i cant describe all of them ;)
P.S. sorry for my English ;)
Delete all class file in your_package_name(androisTest) folder only keep ApplicationTest file.
If you are using some library from which you are getting this error, just delete the build folder under your app source and rebuild the project. I was using OpenCV library for which I w as getting this error and that got resolved by deleting the build folder and rebuilding the project.
Delete all generated code inside "gen" folder.
Uncheck Project->Build Automatically in eclipse.
compile android source once again.
Error will be removed, worked for me.
This still happens sometimes in Android Studio, but a simple Build / Clean Project followed by a Build / Rebuild Project takes care of it.
I have not had this problem for a long time until today.
Error:(10, 14) java: duplicate class: com.domain.name.R
After 2 hours of wasting time on the project and getting this error,
Finally I removed the .idea, gen and out folders and also iml file.
Then I closed the project and then re-import it using import project of main menu again.
Its working well right now.

Categories

Resources