简体   繁体   中英

How to have Android Studio generate R.java?

Today I've been struggling to get my Android Application to work in Android Studio. I've imported the project from an Eclipse project. However, the wrong R.java file is created.

Everywhere in the code the R is red: Cannot resolve symbol "R" . And when hovering the import statement ( import com.example.myproject.R; ): Unused import statement . I do not know where the R.java is created or should be located, but here's my guess: $MODULE$/build/generated/source/r/debug/com/example/myproject/R.java . That is, because the $MODULE/build/generated/source/r/release/com/example/myproject/R.java is being generated, but not the /debug/ version. If I copy the /release/ R.java file to the /debug/com/example/myproject/ directory, all errors solve themselves. But how can I make Android Studio to always generated the /debug/ R.java file?

The myproject.iml file shows:

<facet type="android" name="Android">
  <configuration>
    <option name="SELECTED_BUILD_VARIANT" value="debug" />
.... etc..

So why is the R.java in the release folder generated?

In my case, the

<facet type="java-gradle" name="Java-Gradle"> 

part is strangely missing in my .iml file

...
<facet type="android-gradle" name="Android-Gradle">
  <configuration>
    <option name="GRADLE_PROJECT_PATH" value=":listpopupmenu" />
  </configuration>
</facet>
<facet type="java-gradle" name="Java-Gradle">
  <configuration>
    <option name="BUILD_FOLDER_PATH" value="$MODULE_DIR$/build" />
    <option name="BUILDABLE" value="false" />
  </configuration>
</facet>
<facet type="android" name="Android">
...

after adding that part, it works as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM