Android Studio can't resolve R in imported project?

I am trying to create a new android studio. I exported the project from eclipse using the build gradle option. Then I imported it into Android Studio. The R.java file under gen has j in a small red circle on it. And in my source files, I get "I can not resolve the R character" wherever I have a link to a resource, for example. "R.layout.account_list" etc.

I have not used Intellij before. Thank you for any help, since there are obviously not many answers about Android Studio. Thank!

+43
android intellij-idea android-studio android-resources
May 16 '13 at 12:02
source share
31 answers
  • one
  • 2
  • Press F4 in the Project Structure , check the SDK on the left
  • Click the Modules tab ---> Source , select gen and src as sources

PS: The answer for the year and the menu have changed.

+31
May 16 '13 at 13:32
source share
— -

File -> Invalid Caches

then

Restart application

+27
Jan 31 '15 at 17:16
source share

None of the answers on the Internet have yet helped. Build> Make Project does not work for me. For me, it was as simple as choosing this other option (on Android Studio 1.3.1):

Create> Create module 'module name'

+11
Aug 28 '15 at 20:53 on
source share

In my case, I had an Activity file imported from Eclipse that had the line:

import android.R; 

So, all of my R classes solved the SDK, as soon as I commented on this line, everything was compiled correctly for my package. I only noticed this problem when I moved a project from my Mac to my Windows machine.

+7
Jun 17 '13 at 3:30
source share

This problem starts with me when I started working with build.gradle to include the necessary data (signatureConfigs) to create a signed .apk. After what turned out to be a successful build from the command line, I found that I switched to any class using R. *, that all R. * links were unresolved.

The answers here did not help me. I decided to re-import the project, and it magically solved the problem. Project File / Import ... and select the build.gradle file in the application root folder. I would like to know why re-import fixed this :)

+4
Nov 21 '13 at 1:34
source share

This is probably due to failed resource builds.

Once the problem is fixed, just Build > Rebuild Project will do the trick

+3
Mar 12 '15 at 7:41
source share

Go to File -> Preferences -> The compiler now checks the use of an external assembly

then rebuild the project

+2
Jun 26 '13 at 8:03
source share

I often came across this problem, especially when I contacted the project library as part of another project. In the latter case of this problem, WebView was used in my Android lib project. It turns out that Gradle creates an R.class file in a subdirectory called "web" whose path has nothing to do with my path to the source code. When I imported this, my problem disappeared. What did I find out? In the future, just search for the R.class file and pay attention to the path in which it is located. Then import the package into your code using this path. It really sucks that you must manually perform this. Gradle should automatically import the packages it creates.

+2
Jun 22 '15 at 7:10
source share

Regarding the following, from the answer of the song Crossle

  • Press F4 in the project structure, check the SDK on the left.
  • Select modules ---> Source tab, check gen and src as sources

Despite the message “We will provide a user interface for setting project parameters later ... etc.), this worked for me.

+1
Jun 10 '13 at 14:05
source share

First check if there is an import android.R; at the top of your class this happens when auto import is enabled in android studio.

after that, comment out all the lines of your code in which you use the R variable (you can comment on the whole class or ...) then run the application, it will regenerate the R variable and you can uncomment your code.

+1
Nov 14 '13 at 18:59
source share

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

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

+1
Mar 10 '14 at 20:37
source share

The solution is to open the project structure window. [press team +; on mac ox s. I do not know what the key is for other platforms. you should find it in the "File" menu.] and click on "Modules" in the "Project Settings" section, then your project will open, finally mark the created R.java as Sources.

I am using Intellij idea 14.0 CE. The generated R.java is in the build / generated / source / r / debug / com / example / xxx file

Really hard to find for the first time.

+1
Dec 11 '14 at 12:59 on
source share

Follow the five steps below:

  • Step 1 ===> Remove (CTRL X) the optional tag in the XML manifest file.
  • Step 2 ===> Open Assembly → Clear Project.
  • Step 3 ===> Open XML manifest file and latest recent tags (s): Example manifest manifest file: ( <uses-permission android:name="android.permission.VIBRATE"></uses-permission> )
  • Step 4 ===> Step 2 again.
  • Step 5 ===> Open tool -> Android -> Sync project with Gradle.

I had the same problem, but after all the steps, it works for me.

+1
Feb 23 '15 at 8:49
source share

IntelliJ 14

Check if the generated sources are excluded. Usually in the project structure → [module] → Paths → Compiler output: "Inherit the compilation path"

+1
Feb 27 '15 at 2:31 on
source share

My problem was that I had the file 'default.jpg' in the folder with the ability to transfer, and for some reason every resource was not allowed. Fixed after deleting this file!

+1
Aug 26 '15 at 10:30
source share

I also ran into the same problem and I went through many answers on stackoverflow. Then I found one solution that will help me solve this problem.

Check the package name in the AndroidManifest.xml file. I forgot to change it by copying another project into a new project.

+1
Nov 20 '15 at 10:06
source share

You should check all your files if it has no errors and rebuild your project again in:

  • Assembly> Reconstruction of the project.

Or in:

  • File> Settings> Build, Run, Deploy> Compiler

Tick use external build and rebuild the project.

+1
May 18 '16 at 1:59
source share

I solve this problem with setting JAVA_HOME, CLASSPATH.

People sometimes skip JAVA_HOME and CLASSPATH when setting up Java_path.

So, try checking the environment variable.

CLASSPATH →.;

JAVA_HOME -> C: \ Program Files \ Java \ jdk (version) or where Java is installed

and then check the xml file which may have an error (do not name known things like button, text, etc.)

0
Nov 19 '13 at 10:59
source share

None of these answers helped me!

My problem was not a problem! The program can fully compile and run on the device, but the IDE gave me an annoying syntax error. He emphasized the line of codes that included the "R".

The way to solve this problem:

I just added these three classes to the folder "myapp / gen /com.example.app/":

Buildconfig

 package com.example.app; public final class BuildConfig { public final static boolean DEBUG = true; } 

Manifesto

 package com.example.app; public final class Manifest { } 

R

 package com.example.app; public final class R { } 
0
Dec 01 '13 at 22:02
source share

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

File> Project Structure> Modules> select the main module> select its face> Generated Sources

change the value "Directory for generated files:"

from

 MY_PATH/.idea/gen 

to

 MY_PATH/gen 

Without compiling this code, apk runs and succeeds

but the Android Studio editor highlights mypackage.R.any everything as "cannot resolve" in all classes of subpackages

0
Sep 10 '14 at 5:31 on
source share

I had the same problem and fixed it by deleting the extra library.

To try this solution, go to File> Project Structure (on Mac, you can use the Apple command;)

Then select the application on the left tab. Go to the dependencies tab and remove the additional library.

0
Oct 22 '14 at 17:01
source share

check the build tool version in build.gradle (for the module application). Then go to the android sdk manager, see if the version is installed if it is not installed, or change the version of the build tool to the one installed as shown below.

  android { buildToolsVersion "22.0.1" .. .. .. } 
0
Jun 05 '15 at 2:56
source share

The solution posted by https://stackoverflow.com/users/1373278/hoss above worked for me. I reproduce it here because I still can not comment on the message "Hoss":

Remove import android.R; from your activity file in question.

My setup:

Android Studio 1.2.2

The project is exported from one Mac to Git (everything works). Then I imported the project to another Mac from Git. That's when he stopped resolving resource files.

I tried everything in this thread:

  • invalid cache and restart
  • Delete .iml files and the .idea folder and reimport project
  • Cleaning and Remodeling Project

Nothing worked except deleting import android.R; from my java activity file.

To avoid this problem in the future, add the .idea folder to your .gitignore file. There is a good plugin from Git for gitignore in Android Studio. Install this plugin and right click in .idea > Add to .gitignore

0
Jun 13 '15 at 22:24
source share

just clean the project and then sync the project with the gradle file.

0
Jun 22 '15 at 15:35
source share

In my case, I found that instead of “R.layout.account_list”, “android.R.layout.account_list” was specified. Just remove the "android". can solve the problem. Hope this help because sometimes you may lose sight of it when you focus too much.

0
Aug 17 '15 at 12:51 on
source share

In my case:

I had to copy the link to the R file; those. right-click gen / <package> / R and Copy Reference. Then paste this over R in your code where it cannot be resolved.

This solved it for me (having tried everything here). Still not sure why this works, to be honest.

0
Sep 12 '15 at
source share

I had the same problem. The package name was incorrect in two of the following three files. I manually updated the package name and it worked again.

GEN / com / example / application / BuildConfig.java

GEN / com / example / application / Manifest.java

GEN / com / example / application / R.java

I am not sure what changed them since I have never changed these files before.

0
Sep 21 '15 at 15:18
source share

If this error occurred while importing a project from Eclipse. The reason is that some of the xml files had errors. Check all XML files for errors. Including androidmanifest.xml, as well as values, themes, styles, layout folders, etc. The warnings in xml are fine, but errors will generate this error.

Then do a clean project and rebuild.

0
Jan 02 '15 at 21:56 on
source share

For me, with Android Studio 1.5.1, the solution was to recreate the whole project with a slightly different name.

I think this did not handle the application name "Kommentator_AS" because several places were named "Kommentator" instead.

0
Feb 05 '16 at 12:26
source share

I got into this problem with intellij 2016.1 because I installed the generated sources in a directory that was not in the base directory of the project.

For example, in the Android factor in the "Generated Sources" section, I had:

Directory for generated files: <project dir>/target/intellij-gen

When I changed this to <project dir>/gen , the problem disappeared.

Similarly, when I changed it to <project dir>/gen2 , the problem also disappeared, but I had to do a deep cleanup to get it to compile again:

rm -rf ~/.IdeaIC2016.1/system/compile-server/<project>_<hash>

and deleting all generated directories and target dir.

0
May 11 '16 at 15:01
source share
  • one
  • 2



All Articles