Android Studio issue with R file - android

I was using Android studio now for a long time and it was fine. Recently, not sure what happened, but when I click on debug and have it perform make it complains about the R file and it seems like it doesn't generate it.
The only way it works is if I click on Build > Rebuild Project...problem is that it takes a long time for that to go through.
Is there anything I am missing? I updated the Android SDK tools, etc. as per some of the other suggestions?

Yes this is a known issue in the newest version of android studio. Google has suggested a temporary workaround until they can track down the problem and fix in next release. Turn off external builds in preferences > compiler. It might help.
Source: cannot resolve R into variable after Android studio update and R.java not updated after update to 0.1.6 .
Hope this helps.

Solution for android studio Users :
Goto : Menu tab - > Build - > Rebuild Project
after rebuild Project.R.Java file will be solve automatically ..

I was having the same problem, but it looks like a new update came out today. It seems to have solved the problem.

Try to import android.R, for every class you need R (of the package)
and then compile, it makes different error from before.
And remove all ('import android.R') , and rebuild it.
This trick is not for solving problem in proper way,
but makes the tool refresh.

And remove all "import android.R" , and rebuild it and import them again its working for me

Related

Values in XML Attributes no longer accepted as valid after upgrading to Android Studio 3.0.1

After upgrading to Android Studio my styles.xml suddenly has lots of errors.
When trying to change the parent property Android Studio suggests 'TextAppearance.Widget.AppCompat.Toolbar' as a valid value when typing...
Also I had to prefix ?attr/... attributes with ?android:attr like below...
android:background="?android:attr/selectableItemBackground"
Note that my app will compile and run...
Are these bugs? (probably...)
If not; What is the reason for the change?
If so; What prefix is missing for my themes?
Thanks, Ove
It can be solved after Sync project with Gradle, Clean , Rebuild the Project. I think it is a bug of android studio 3.0.
I get that from time to time as well, I usually try the following:
Clean & Rebuild Project
Sync project with Gradle files
File -> Invalidate caches / Restart -> Invalidate and Restart
A combination of these in a random order usually solves it (I know, I know, really scientific method... but hey as long as it works :))

unable to instantiate application - ClassNotFoundException

EDIT: This problem has not already been resolved in the other suggested SO question
I had a fully working app on the market for over a year, with very few crash reports. Then recently I changed my app into a library, so that it could be included within multiple different "wrapper" projects. This was so that I could easily make different version - free, paid, non-google markets, with/without in-app purchasing etc etc.
The new "library+wrapper" app appeared to work fine. I could run it multiple times, without error. But then a day later (when presumably the OS had closed some or all of the app's activities) I tried to run it and it reported
Unable to instantiate application com.mycompany.mygamelibrary.MyGameApplicationClass: java.lang.ClassNotFoundException: com.mycompany.mygamelibrary.MyGameApplicationClass
The class it failed to find is the first class that runs when the program starts up, MyGameApplicationClass - which extends Application. This class is part of the library.
I suspect something goofy in one of the two manifest files.
The manifest of the wrapper project contains the lines...
<application android:icon="#drawable/mygame_icon"
android:screenOrientation="portrait" android:label="My Game Name"
android:name="com.mycompany.mygamelibrary.MyGameApplicationClass">
Any ideas what could have gone wrong?
EDIT: The library was referenced "the correct way" as defined by yorkw's answer to this SO question.
EDIT: I can not repeat the crash at the moment :-( I don't know what it is the OS does when the app is not used for a day or two.
There are two possibilities. Either you, like me, have a spelling error in your manifest file. Have a co-worker or friend read it to make sure the name is correct. Or you have not referenced the project correctly.
The official document describes how to properly link projects in its documentation.
Why it would first seem to work and later stop working is a bit of a mystery. However, I guess that the VM might still have had the necessary references ready to resolve the classes in the library just fine. A restart of the VM removed all those references and trying to resolve them was unsuccessful.
Update: Regarding the edits in the OP: As you confirm that you have correctly referenced the other project, you can check if the project is included in APK, just to be sure. You can rename and open an APK as any other archive (.rar works fine for me). Sometimes, it happened to me, the project is not correctly included in the APK. A cleaning of your workspace usually remedies the problem and so could a restart of your IDE depending on what you are using. To manually conduct a clean in Eclipse for example, use Project->Clean... or try Android Tools->Fix Project Properties by right-clicking on your project.
As you seem to also have fixed the problem by restarting your device, it could be that the libraries were linked incorrectly. A problem that I have never seen myself but as a very common quote says: "Have you tried turning it off and on again?".
For Android Studio:
Build --> Clean Project
Fixed issue.
Have you tried to make a new subclass of MyGameApplicationClass in your 'main' project and set it in the manifest as Application class?
I had a slew of bugs with Android Studio 3.0 Canary 4 and the way I fixed them was by editing the AndroidManifest.xml by adding in some jibberish to the application name. Then, I clicked build. Obviously, a whole mess of new error messages appeared. I changed the name back to what it should be, built the app, and it just ran.
Sometimes, I just don't know...
EDIT: Just ran into this issue on Android Studio 3.0 Canary 4 on my laptop when switching over. I again went through the same process of changing AndroidManifest.xml file to contain a typo, building, and changing back. That didn't work.
I then noticed that instant run was still enabled. Going into settings (by clicking command + , (comma key)) and typing "instant run", I was able to disable instant run, built the app, and the error of class not found went away.
Summary of Steps to Fix [FOR ME]
Invalidate cache / restart
Clean the project
Manually delete the build folder (need to be in project view for this one)
Make an intentionally errant edit to your AndroidManifest.xml file, build the app, observe the errors, remove the errant edit and build again
Disable instant run
Again, I don't mean to insinuate that this will fix everyone's error, but I have now used some combination of these steps on two different machines (MacOS Sierra) and it has been resolved for me. Hope it helps.
In my case, application id and package were mismatched. This should be same as presented in following images...
AndroidManifest.xml
app/build.gradle
In this case you can see, applicationId and package both are same that is com.mycompany.mygamelibrary
May be its a Build Path Configuration problem.I did the following to solve the issue.
1.Right click on your project and go to Java Build Path.
2.Click on Order and Export tab.
3.Check Android Private Libraries and other 3rd part libraries if you have added.
4.Press ok and clean the project.
I hope it will solve the issue.
Once I had the same error message, but maybe the cause isn't the same.
I did a code and worked for a while, then I wanted to improve it and got the same error and I couldn't run it.
I could fix the problem with
the correct Build Path order (as I can see you've already did this)
I check on the Order and Export tab the android-suppor-v4.jar
and the key was the Android SDK Managert->Upgrade everything and (next) Eclipse->Help->Check for updates.
After I upgraded to the latest android plugin and SDK my app compiled and ran again.
I hope this will help you!
I'm not very sure about this but it might be that your system's debug.keystore license validity has expired as it is valid for only 365 days. You just need to delete the debug.keystore from your computer. The debug.keystore will be generated automatically by Eclipse when you compile your Android App.
Same message seen ... this time it turned out to be different output folders for MyApp/gen and MyApp/src in the Build Path (caused by Maven integration).
Unchecking "Allow output folders for source folders" solved the problem.
I had this issue in an Android application that needed an Application class which was created in wrong path inside the Android Studio project. When I moved the class file to the correct package, it was fixed.
This all Process work for me to solve application class Exception.
Step 1: Open Run(window+R) Search -> Prefetch Remove all file (Some file not Delete)
Step 2: Open Run(window+R) Search -> %temp% Remove all file (Some File not Delete)
Step 3: Open Android Studio -> Build -> Clean Project
OR
Select File > Invalidate Caches / Restart > Invalidate and Restart from Android Studio toolbar.
OR
Close and reopen Android project.
OR
Restart System
I ran into this issue several times and both times it seemed to be caused by some instant run feature.
In my case, deleting the application from the device and then installing it from Android Studio again resolved the issue.
I ran into this problem today. The project runs well for over a year but today it reports this issue, and cannot debug on my testing device.
I fixed it by updating to latest gradle version. Hope this can solve your problem.

Android studio suddenly cannot resolve symbol R

I was having a project open in Android studio. It was generated by the wizard and working fine.
I did some small changes to activity_main.xml and when I changed back to MainActivity.java i get the error in several places that it cannot resolve R. I might have done something to cause this. But I´m not sure what since it appered when I edited the xml.
Does anyone know what might be the solution to this?
I can find the R.java in r/release/packege/com/ and it looks fine.
Most of the time it is due to a bad XML file. XML files can be layout files, value files, or the Manifest file. Please check your xml files and try to rebuild the project. Sometimes cleaning the project and rebuilding it also works.
In addition, make sure you do not have a drawable with an invalid name. I had a drawable with a numeric filename and that didn't sit well with Android so it failed to compile R.java.
Downgrade Your Gradle Plugin Version
No amount of cleaning, rebuilding and restarting would do the trick for me.
The only thing that did the trick was downgrade our Gradle version from 3.4.0-alpha02 to 3.2.1.
So, instead of:
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0-alpha02'
}
We used:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
After making that change and then doing a Gradle sync, everything worked.
Not sure if it's related to the alpha release or to that version or if changing your Gradle version at all just forces a refresh that otherwise cannot be done with the other commands, but there ya go.
Make sure in your AndroidManifest.xml the package name is correct. That fixed the problem for me when my R.whatever was marked red!
Try to clean the project by doing the following:
If you are using Windows, open CMD, cd to your project directory and run gradlew clean.
I just had this problem and the reason was that after editing the layout android studio automatically added an import to the source files:
import android.R
Of course the main R class has not all the symbols in your layouts, which made everything a red mess.
Removing that line solved for me.
EDIT:
Seems like eclipse had the same problem early on.
I sometimes get this after updating Android Studio. Not always straight away but after a while (perhaps after a restart of the program).
Here is what has worked for me: Tools - Android - Sync project with Gradle files.
For some reason just clicking the "Sync now" link that shows up after changing the build.gradle file has not done any change for me. Even though the messages says approximately the same thing.
I had the same problem because in my strings.xml I forgot to escape a apostrophe. Use " \' " never " ' " in xml file!
I had that same problem crop up, too (most recently while I was at lunch--came back and WTF?). And since this was a simple Hello, World program, there wasn't many places these errors could hide. The XML files looked fine--I hadn't even touched the manifest. A clean did nothing.
So I commented out a section of my layout.xml to try to narrow down the problem. Sure enough, the errors went away.
And then I simply UNcommented out exactly what I had commented...and everything was hunky-dory. Strange! Perhaps there was some lingering issues that needed to be touched in order to cause a rebuild of the layout file. You'd think basic stuff like that wouldn't happen these days, but they do.
One more thing! It's possible that something may happen to Java as well. It's very hard to know the status of the Java run-time environment; just easier to start again. So here's one more thing to try: restart your computer, restart Android Studio, AND THEN Invalidate Caches and Restart. Yep, the whole-hog.
When all else fails restarting android sometimes helps. During the start files are loaded afresh and this enables detection of the R
This problem occurs when you rename the package name.
After renaming the package name, you must change the older package
name to a newer one.
For me it solved the problem once I changed that.
After that, you might get errors in xml files, that you have to change
the package name there also.
That's it.
I just had the R problem and after a few hours found that the problem was my regional spelling as used in one of my xml files. The problem word was behaviour, which is spelled behavior by Americans.
I am Australian and we are taught ye olde English way of spelling certain words and so I looked past it a number of times before the penny dropped.
Its a small thing, but it can easily sneak up and bite you on the nether regions and is hard to spot and can waste a lot of time.
In my case I had just added some new image files to the drawable folder and accidentally put a GIF in there with my PNGs. I had not incorporated them into the project at all yet. I removed the GIF and then did a clean and rebuild. That fixed it for me.
I had this exact problem when importing a Udacity Example Program. The Message Pane in Android Studio said the error was failing to load a .PNG in a sub-directory deep in the Project Files.
Turns out the PNG with all of the sub-directory path names was greater than the name length limit defined by Windows 7 (260 characters). I gave the Project a shorter name - to free up some space - and then Android Studio happily compiled.
After you tried make project, clean project, rebuild project but still doesn't work.
Try restart Android studio

What to do when workspace build becomes stuck?

I am using Eclipse (Galileo) to build an android app. Stupidly, instead of letting Eclipse close normally I became impatient and forced it to close via the Windows Task Manaager.
Now, when I try to open Eclipse it builds as usual but then, at 2%, it completely stops. It always happens at one certain project file (a project I downloaded from a book), and the console says
> "Native folder: C:\Users\DV9000\Android Related\Sams Teach Yourself Android Code\Chapter 18\libs"
Maybe it has something to do with a library? I would be happy to delete the project but I must wait until the workspace has finished building. Argh!!!
Any help appreciated.
Edit: Fixed it
I ended up deleting the files in the folder.. workspace.metadata.plugins\org.eclipse.core.resources.projects
This was described in James Beninger's answer in this post
The post says "Use at own risk", so I was nervous as I had no idea what I was doing. Maybe someone else knows?
Have you tried going to Project->Clean and selecting the project?
i had the same issue. had to create a new workspace and import all my projects.

What can I do with R cannot be resolved problem?

If I make a new Android project in Ecplipse and I try, to open one of the samples from android SDK, I have got an error, look like this:
R cannot be resolved
What can I do, to be able, to run these files? I tried to delete import android.R; but nothing changed.
Thanks
First thing you might try is "clean" your project (option in the menu). If it persists:
There should be an error that stops R from being generated. Check the "problems" tab (?) in eclipse to see what that problem is, and solve that. It could be missing libs, or various other stuff.
You possibly got error in building resources.
Possible fixes:
-refresh your project (F5)
-clean you project and refresh
-see "Console" tab, sometimes the problem is shown there
-reload the Android SDK
-restart the Eclipse IDE
Check your XML-Layout files. The problem come here half a time.
If you are sure it is note your layout file, check in your java build path if the google librarys are well set. (Google APIs)
(sorry for my bad english)

Categories

Resources