Android Studio cannot resolve R in imported project?

后端 未结 30 2030
名媛妹妹
名媛妹妹 2020-11-30 02:27

I\'m trying the new Android Studio. I\'ve exported a project from eclipse, using the build gradle option. I\'ve then imported it in Android Studio. The R.java file under gen

相关标签:
30条回答
  • 2020-11-30 02:39

    In my case:

    I had to Copy Reference the R file; i.e. right click gen/<package>/R and Copy Reference. Then paste that over the R in your code where it fails to resolve.

    That solved it for me (after trying everything else here). Still not sure why it worked to be honest.

    0 讨论(0)
  • 2020-11-30 02:42

    After you tried Clean and Rebuild without success, check your res folder for corrupted files.

    In my case a corrupted .png file caused all the trouble.

    0 讨论(0)
  • 2020-11-30 02:45

    Here's what worked for me in IntelliJ (Not Studio), in addition to the replies presented above:

    You need to attach an Android facet to an existing Android module. To do this, select the module from the module list in 'Module Settings', hit the '+' button on top and select 'Android'. See https://www.jetbrains.com/idea/webhelp/enabling-android-support.html.

    0 讨论(0)
  • 2020-11-30 02:46
    1. Press F4 into Project Structure, Check SDKs on left
    2. Click Modules ---> Source Tab, check gen and src as sources

    PS: The answer over a year old and the menus have changed.

    0 讨论(0)
  • 2020-11-30 02:46

    I've been frustrated on many occasions with this problem, especially when intergating a project library as part of another project. In the most recent occurrence of this plagued problem, my Android lib project was using a WebView. It turns out that Gradle builds the R.class file in a sub directory called "web" whose path does not have anything to do with my source code path. When I imported this, my problem was gone. What have I learned? In the future, simply do a file search for R.class and note the path where it is located. Then import the package into your code using that path. It really sucks that you have to manually perform this. Gradle should automatically import packages it generates.

    0 讨论(0)
  • 2020-11-30 02:46

    In my case (Linux, Android Studio 0.8.6 ) the following helps :

    File > Project Structure > Modules > select main module > select its facet > Generated Sources

    change value of "Directory for generated files:"

    from

    MY_PATH/.idea/gen
    

    to

    MY_PATH/gen
    

    Without that code is compiled, apk is build and run successfully

    but Android Studio editor highlights mypackage.R.anything as "cannot resolve" in all sub-packages classes

    0 讨论(0)
提交回复
热议问题