Android R.java will not generate in Eclipse - android

I am running Eclipse 4.2.2 Juno on Windows 64-bit for development on Android SDK 17 with ADT. Just today, I cleaned a working project, only to find that the R.java file would no longer generate.
This problem has a very divergent list of possible causes. User Gray, in response to the thread located here, listed a set of articles, all addressing different possible causes.
He says:
Dont worry. First you may clean the project, then run the project. If
this does not work then follow the following links:
And then proceeds to list articles pertaining to different causes of this problem, the links to which I am unable to include, but can be found in the question linked to above.
Gray's comment is a good summary of the most common causes to this problem, including resource file naming convention, the erroneous "import Android.R" statement, XML errors, corruption requiring cleaning and rebuilding, and checking the Android SDK in Project -> Properties -> Java Build Path / Libraries.
The problem is, my R.java still doesn't generate! The only remaining possibility within Gray's list seems to be that either my main.xml or AndroidManifest.xml is broken, so I have included them to make sure I didn't miss any errors.
My main layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.bostonwalker.sseng.SSSurfaceView
android:id="#+id/ssview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
And my manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bostonwalker.enginedev"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >"
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
As a beginner Android programmer, this is beyond my capability to debug. Can someone please find an explanation?

First check if there are any errors in your resource file's. R.java will not be generated if that's the case. Check if you have updated your adt to rev 22. If so follow the below
Right click on your project goto properties. Java Build Path. Choose Order export tab. Make sure that Android Private Libraries is selected. If you have referenced library project. do the same for the library project also. Clean and Build.
Also goto android sdk manager and check that you have the android sdk build tools installed. This many not be necessary but make sure you have android build tools installed.
Check the link below
Eclipse error: R cannot be resolved to a variable

Whenever your generated R class isn't generated, it indicates that there's a problem with generating it due to some parsing issue from the XML resources. Check the error console in your IDE to figure out what's specifically wrong.
Common problems are:
An unescaped character in your strings.xml, for instance you're instead of you\'re
Missing layout_width or layout_height tags in layout resources
Missing namespace declarations
Variable names that aren't supported by Java, for instance due to capitalization or use of spaces, hyphens or other unsupported characters
Any other kind of syntax error in XML

In addition to what Reghunandan said, I just had the same issue after updating to SDK 22 with Eclipse Indigo Win x64. Turns out that when updating the SDK Manager uninstalled the old Build Tools and never installed the updated ones so I had to run it through again to get them reinstalled. Doing this fixed my issue and all the build errors disappeared.
Note: I had to restart Eclipse before the errors disappeared, even though I wasn't prompted to.

I faced a similar issue and found that I imported com.google.android.gms which had a reference to google-play-services.jar. So once I added the path of google-play-services.jar in my apps libraries, R.Java is generated.

Related

After adding google-play-service_lib my resourceIds cannot be recognized

I am using ADT 23.0.2.
I just imported the google-play library to my workspace and added the reference to it in my project.
Now nowhere in my project the resource ids can be recognized because there is no R file.
(Anywhere I have R.id.blah I get the error "R cannot be resolved to a variable);
It's just gone. What am I supposed to do?
I exactly followed the steps by developers.google.com. It's nothing complicated but I don't know why this happened.
(I have all layout files in place and no import for android.R are in my class files.)
Before adding the
Tools I use:
Here are screenshots from my app properties:
My manifest
<permission
android:name="com.appname.appname4.MAPS_RECEIVE"
android:protectionLevel="signature">
</permission>
<uses-permission android:name="com.appname.appname4.MAPS_RECEIVE"/>
<activity
android:name="com.appname.appname4.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.appname.appname4.SearchResultListActivity"
android:label="#string/title_activity_search_result_list"
android:parentActivityName="com.appname.appname4.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.appname.appname4.MainActivity" />
</activity>
<activity
android:name="com.appname.appname4.ContactInfoActivity"
android:label="#string/title_activity_property_detail"
android:parentActivityName="com.appname.appname4.SearchResultListActivity"
>
</manifest>
I had similar issues when I first started Android programming. Here are some fixes that worked for me in the past.
1. Fix/Check all xml(layout) files.
Many times a R file not being generated is due to an issue with your layout file or Android Manifest file. Follow this link and work your way down the page checking permissions and the general layout format.
2. Project/Clean.
go to Project->Clean.
Also make sure you have selected the option to build project automatically.(Without this option check marked R file is never generated automatically).
3. Fix Project Properties.
Right click on your project in Package explorer then choose fix project properties.
Repeat Step 1 after this.
4. Builders.
Go to Project->Properties then Builders. Select the appropriate boxes.
Repeat Step 1, and Clean and rebuild project.
5. Appcom7 and Google Play services error.
Sometimes to fix errors associated with installing these two reference libraries. I copy there folders into a generic library folder on my hard(where I keep most of my eclipse reference libraries), after this I delete them from the Package explorer and from the "default" directory where they were installed. I Fix project properties again and clean/rebuild. This should get you back to your non-reference library version of the package. Then I manually add them as Projects to my eclipse IDE and finally follow this to add them as references to my project. Then fix project properties,clean and rebuild.
6. Reference Question.
Finally, referring to this StackOverflow page has helped me countless times. And one way or another fixed my issue.
Good Luck.

Eclipses does not seem to generate the correct Blank Activity android project

Hey guys I am new to Android development with eclipse. I am following the walkthrough on the google website (here) and already hit an issue with it. I've installed the SDK and eclipse as instructed. Now I am trying to create and run a default Blank Activity android app. When I create the project, eclipse creates an appcompat_v7 project along side my MyFirstApp project. When I hit run, the app seems to be build without any issues and the log tells mw that it is installed on my app. But nothing happens, and I can't find it on the device.
Now the reason I think that it is not getting generated is because that Android Manifest is very barebones and missing lots of vital things by the looks of it:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
</application>
Secondly, the next step in the tutorial asks me to open up the fragment_main.xml file from the res/layout/ directory. This file does not exist. The directory is empty, there is nothing there.
I don't get any errors so I am not sure what has gone wrong.
So after a long discussion with #Prodigga, we came to the point where we think it is better to take a step back and wait for things to settle with the new SDK release (api level 21).
By the way, tutorial in the question is not outdated. There is this minor problem with the new sdk.
I don't know what is causing the problem and I don't know the solution yet but there is a workaround. If you only want to create a blank activity, given that you are experiencing the same issue, do as follows
Uncheck "create activity" check box when configuring your new project.
Create the package and MainActivity manually.
Add it to the manifest manually.
If you would like to benefit from code auto-generation, then I recommend downgrading the sdk. For example, you could use this ADT bundle (10/30/2013 - Windows 64-bit), but don't try updating the SDK to level 21, not yet.
In your android manifest file, add an activity element as the child of the application element. Add the name of the Activity class that you would want to show on the launch screen. See the below link on the syntax for this:
http://www.mkyong.com/android/how-to-set-default-activity-for-android-application/

IntelliJ IDEA - Can't build anything, always get "package R does not exist"

I'm trying to use IntelliJ IDEA to work on an Android app with a colleague that swears by it, but I'm unable to build any of the Android projects he sends me because whenever I try a build I get an error saying package R does not exist.
I have been trying out everything I can think of. Examples and other things to note are:
Making sure I have a version of the local.properties file pointing to the root of my Android SDKs folder.
Fiddling with various settings in the project structure dialog (I won't list them all).
The project I'm working on has two library projects with also use the R class and they work fine.
The intellisense within the IDE recognises the R class and comes up with the stuff I expect.
Any help would be greatly appreciated. I feel I'm missing something basic.
I am brand new to IntelliJ so I apologize if this doesn't work for you. I was experiencing the same problem and the solution was to add an Android Facet to your project.
(I am on a Mac, so directions here may be slightly off, and there are probably better ways to find this window, if so let me know!)
Right click your project and go down to Open Module Settings (seems F4 also works)
Select Facets in the far left column
Click the + button
Add an Android Facet to your project, and VOILA!
You may need to import your Rs now, which could be a huge pain... so hopefully someone can chime in with an easy way to auto-import
Hope this helps someone!
Another possible solution to those listed here is to check that the package name in your AndroidManifest.xml matches that of your actual package:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.yourpackagename" >
My package was building fine until I refactored the package name, everything refactored except for the package name in manifest file (see above) which caused the error "Package R does not exist".
If you run into this with a package that has been building before you need to "Rebuild".
Build->Rebuild Project . Deleting gen does not do it since IDEA saves the actual generated R somewhere else.
In my case, I added some classes from another project and I had to add the import to the R package from my actual project:
import com.youcompanyname.yourprojectname.R;
I've started playing around with IntelliJ since I've had issues with the new Android Studio and wanted to try something that was a little more stable (I've never really used either before). I ended up getting the dreaded “package R does not exist” error. In my experience, this is usually something messed up in an XML file. For me, the problem was actually with the AndroidManifest.xml file for an Android Library Module that I had created for ActionBarSherlock. For whatever reason, when it created the AndroidManifest.xml file it didn't bother to use the AndroidManifest.xml file that came with ABS and it put the following in:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ActionBarSherlockLibrary_4_3_1"
android:versionCode="1"
android:versionName="1.0">
<application
android:label="#string/app_name"
android:icon="#drawable/ic_launcher">
<activity
android:name="ACTIVITY_ENTRY_NAME"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
When I created this Module I chose File > New Module...
I then Selected Library Module under the Android section and for the Content Root I browsed to the location of the files for ABS (Other fields auto filled in correctly and I didn't change them).
I then clicked finish and it prompted me if I wanted to keep the local file or the memory file for project.properties.
I chose to keep the local file and it didn't prompt me for any others after that.
I have no idea why it messed up the AndroidManifest.xml file (maybe what I did above was wrong...???), but to fix it I just copied the original AndroidManifest.xml that I had downloaded with the library and replaced the messed up AndroidManifest.xml file. After that I did Build > Rebuild Project and it resolved the “package R does not exist” errors.
One other thought, #xbakesx mentions the Facets settings, and for any modules you are using as libraries, you should make sure that "Library module" is checked under Facets for that Module. I had some issues with that at first when I was trying to figure out how to configure libraries in IntelliJ/Android Studio (I've really only used Eclipse).
I just restart IntelliJ and it magically solved.
Try it first.
Had same issue - the issue was the Manifest file was did not have the default Activity setup properly.
To all the hopeless people who have reached this far down the answers: I feel your pain. I have spent an hour doing nothing but trying all those things above as well as proposals from other sources. I have checked all my xml resources twice, cleaned caches, rebuilt a dozen times, even restarted the entire machine. Here is what actually worked for me:
Under 'Build Variants' (lower left corner), I selected a different Build Variant (in my case release instead of debug) for the target that had the missing R problem. Solved it.
I've refactored the packagename and intellij didn't update it in the manifest.
So if you've refactored the packagename then go to the manifest and update the attribute "package" in the root of the xml file save and rebuild it. If any errors occur then it's probably a wrong packagename in the import (at least this was my case) so just fix them
hope this helps anyone
In my case I had to delete the intellj compiler cache. on my windows machine it was somelike this:
%USERPROFILE%\.IdeaIC12\system\compile-server\<my project>
Try create new project and select "Create project from existing sources"
Adding my $0.02 just in case someone else has the issue I did. In my case I had generated the "Hello world" application using IntelliJ and forgot to change the package name from "com.example." I then used IntelliJ to refactor the package name. This caused the error to start happening.
So I did a global search for "com.example" and it was found in the AndroidManifest file as mentioned by others. However, changing this did not fix the problem. "com.example" was also found in "workspace.xml" under the ".idea" directory. I changed all the occurrences their, did a rebuild and then it started working again!
This seems like a bug in IntelliJ.
In Android Studio, my problem was when i copy pasted a class to use as a template to make another similar class, it asks to automatically import a bunch of stuff, most of which was not going to be related to the new class, so i said cancel. with it it left out import com.your.packagename.R; so R wasnt actually being imported into the file i was trying to use it in.
can be fixed with an alt-enter when clicking on R (it should be highlighted in red)

R cannot be resolved to a variable on newly installed eclipse

I have been looking for this on stack overflow for the past two hours and have tried a lot of things but nothing worked.
I have installed eclipse juno just this morning and i am getting this error R cannot be resolved to a variable in any project that I create. Also, I cannot see any files under the gen folder, even after I build the project. I have tried clean project, installed ia32 libs for my 64 bit system running ubuntu, tried changing folder positions, rebooting system but nothing worked.
The generated code is:
package tanvi.alarm;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
/>
</RelativeLayout>
manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tanvi.alarm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Check if you have this line
import android.R;
and if, remove it and clean and build project again.
Same thing is solved here. Check my answer.
I had the same problem as you but in Windows, and I found a solution to it.
You have to check your SDK's folder permissions. I moved my SDK folder, which is located in my second HDD, to a new folder and it all worked perfectly.
After upgrading to Juno, I had this problem. It was fixed by using the Android SDK Manager to install Android SDK Build-tools.
I've just spent a day finding a fix for this problem.
In my case I started with Eclipse: Help->Check for Updates. These updated and I then opened the SDK manager and updated a few packages there too.
After that I had the 'R cannot be resolved error' for new projects and also the 'Could not find *.apk' in other projects
My solution was to Help->Check for Updates in Eclipse again and then open the SDK manager again and a small number of extra packages were available for an update. These updates were not available the first time I looked for updates.
I restarted Eclipse after the 2nd round of update and all was fine after that.
Hope this helps someone in the future.
If nothing above is resolved then check your /res folder where there might be an error in xml file. Due to this error R.Java file will not be updated due to which the above problem might occur. If have the error in xml file it must be probably in res-values-strings.xml. Try removing that error and also update your Androidmanifest.xml if any string values are used.
This will automatically resolve the issue.
I installed Juno myself yesterday and had to faff around in the beginning but everything seems to be working now. You didn;t say which SDK you've installed, but I'm assuming you are using the latest SDK (r20) as you seem to have dotted all the other Is. However, as r20 is the only version that currently works with Juno it would be best to check if you're unsure.
Unsurprisingly the code you supplied in the question works as is, so the problem isn't there. However, if there is an error in one of the XML files in that you have supplied, you'll need fix those before the R class can be generated.
Alternatively, if you previously had a 3.x version of Eclipse installed are you still using the same workspace for for running Juno? If so I suggest starting again in a fresh workspace and see if you have the same issues. Even using -clean on the old workspace won't cut it. If you want to keep using the old workspace directory, delete the .metadata directory within it and try again, though you will need to import any existing projects afterwards.
To be honest, I'd be surprised if it was a workspace clash as I'd expect you to have more problems that just an R cannot be resolved error, but I'm just trying to cover the bases.
I had to install this library on my ubuntu 12.04, sudo apt-get install ia32-libs (my system is 64 bit).
And on restart of eclipse, the files were generated.
If the problem persists, check if the Project -> Build Automatically is selected. Once selected, the project should run fine.

FragmentActivity causing ClassNotFoundException

I just used Android SDK Manager to update Android SDK Tools to revision 17, and Android Compatiblity to revision 7. Now, the program I've been running for ages crashes on startup.
Narrowing down the issue, I have created a new blank project, added android-support-v4.jar to the build path, and changed Activity to FragmentActivity and that's all. Now it crashes.
The error message is:
java.lang.ClassNotFoundException: com.example.test.TestActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.test-2.apk]
The code is:
package com.example.test;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class TestActivity extends FragmentActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Everything else, including the manifest, is unchanged from the defaults. Any help is much appreciated!
Edit: Manifest included below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="9" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Turns out it's a problem with Android SDK Tools r17. I had previously been using the method given in the tutorial at:
http://mobile.tutsplus.com/tutorials/android/android-compatibility-working-with-fragments/
However, this no longer works. Instead, all I needed to do was right-click on my project in Eclipse and choose Android Tools-->Add Support Library...
Doing this means it is no longer necessary to go to Java Build Path and click "Add External JARs..."
Many thanks to eMich for this solution from: Jar-file issue with ADT r17
I had the same issue and none of the answers I found in stackoverflow solved my problem. After hours of several (many) trial-and-error, I solved my problem by configuring build path of my project. In Eclipse, right click the project > Build Path > Configure Build Path..., in Order and Export tab, check Android Private Libraries, click OK. After that, clean up the project and try running again.
I solved this problem by comparing my project with other (newly created) project that could run as expected. I compared each configurations and AndroidManifest.xml of them.
Hope this helps you too :)
UPDATE Other solution: using ANT
I found another way to solve this problem: using ANT. My friend faced the same issue, but fixing the build path didn't solve his problem. I don't know whether it was because we use the different IDE version, different ADT version, or different operating system (I use GNU/Linux). And then, I suggested him to use ANT rather than the IDE's one.
First, setup the project (create build.xml) by executing android update project -p <project-dir> -n <project-name> for each project (including library projects). And then, from the main project's directory, execute ant debug to build, ant installd to install, and run the application.
The strange thing was, once he succeeded with this way, he even can compile by using IDE again, without ANT at all.
I assume that fragment activity is listed in the manifest properly? Here's my main FragmentActivity class in the Manifest:
<activity android:name=".Polling" android:label="#string/app_name"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
select your project in Package Explorer > Right Click it > Android Tools > Fix PRoject Properties
and also
Try Project > Clean
Then rerun.
I got the
java.lang.NoClassDefFoundError: com.android.example.SupportFragment
at com.android.example.SupportFragmentActivity.onCreate()
on PopupMenu
SupportFragment extends SherlockFragment implements PopupMenu.OnMenuItemClickListener
...
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
return onOptionsItemSelected(item);
}
when trying to make a api 17 app compatible with api 8, the only indication was the logcat error above, so check that all your imported classes are supported if you get this error.
Same issue got solved by Doing Build path->Configure Build path->order and export->Check Add private libraries->ok.
Then clean up the project.
Done a big problem solved.
I also got the same issue, and resolved it by the below way.
If because of some reason you have UNCHECKED to the private libraries from build path, so just add Androidv-4 jar in build path and enable(check) it in "Order and Export" tab
Remove anything lib about v7 or v4. Move your Android project, Alt+Enter, click android, look is lib: make sure there is nothing about v7 or v4 lib
add v4 lib
clean project
run again
if you remove v7, will be error with styles... you may look this

Categories

Resources