How to generate R.java

99封情书 提交于 2019-12-17 19:18:55

问题


I downloaded few source codes from Google source codes.

I executed them on Eclipse but R.java file is not created and therefore I got many errors saying that R cannot be resolved. There is no main.xml in layout too. Some other XML files are there. How can I execute these programs?


回答1:


As long as you have a valid Android project, you can use aapt package to generate the R.java from resources. Here's example usage (all should be in one line):

aapt package --non-constant-id -f -m -M <abs_path_to_AndroidManifest.xml> 
  -S <abs_path_to_res_dir> -I <abs_path_to_platforms_android.jar> 
  -J <abs_path_to_dir_that_should_contain_gen_R_java) 
  --generate-dependencies

The above can be made scriptable using ANT by using the aapt target or by simply using an exec target.




回答2:


Presume you have Android sdk. If you have.. just clean the project. R.java will be autogenerated.




回答3:


Hi I just faced the same problem. The file R.java is deleted an regenerated each time you clean the project. In my case there were some errors in one of .xml files and no R.java was generated. If you did not have Main.xml that might be the reason. anyway if you fix .xml problems R.java should be generated.




回答4:


  • Open Android SDK Manager
  • Tick Tools that there are 3 packages of Android SDK Tools , delete and install them again.
  • When they are finished, please refresh your project in Package Explorer.

You will see BuildConfig.java and R.java in /gen

I think you wanna need that I recommend this comment

Enjoy with your building app.




回答5:


After few hours frustration finally I have decide to remove the red marks from the my file where it was not able to resolve the R.java as it was not actually available in my code. After removing the first red line from my activity file the project builds and generated the R.java automatically.

This is something really freaking thing for me as it goes without any reason and came back with the same thing.

Hope this will help you

Kind Regards, Summved




回答6:


If your build.xml includes the "-resource-src" target (which it will if you include $SDK_DIR/tools/main_rules.xml or lib_rules.xml) you can rebuild it with ant.




回答7:


For future readers, restart adb then clean.




回答8:


Correct all errors in the Xml layouts, if the errors is only in the id attribute, change to other name an save, then type the original values, this solution works for me.




回答9:


I encountered this issue after adding a jpg file with a numeric file name into my drawable folder. I was getting a compilation error, " expected on line y" when trying to rebuild the project after cleaning it. You may want to check that you do not have a similar issue. Although I encountered this while using Android Studio, I am sure it would affect Eclipse as well.




回答10:


Cleaning the project, delete the R.java and regenerate it. If you have an error anywhere in your /res file (the resources) , the R will not be generated usually. It could be an error in the name of a picture , or anything ... fix your errors before. good luck ;)




回答11:


I have encountered similar problem and i changed the workspace folder and all errors disappeared. It's sort of simple solution but it works.



来源:https://stackoverflow.com/questions/4619162/how-to-generate-r-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!