3

I am trying to update a 3-year-old Android app and in the process convert from Eclipse to Android Studio. I am also new to android development. I am having troubles with the R files. I have tried the help tips here,here, and here (which aren't all that different I realize). I am referencing strings.

Here is the method:

private void setDefaultSettings() {
    SettingsFragment.SETTING_KEY = getResources().getString(R.string.default_key);
}

default_key is in my strings.xml file:

<resources>
    <string name="default_key">Key1</string>
</resources>

I have tried to:

  1. Clean and build the project
  2. Clean, sync gradle, build project
  3. Modify xml files, clean, sync gradle, build
  4. Including android.R.string, clean, sync gradle, build

What am I missing here?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
mrsB2013
  • 171
  • 2
  • 8
  • 2
    Please, can you put the log? – extmkv Mar 06 '18 at 15:22
  • maybe the R your using is linking to another project resource ? – Remario Mar 06 '18 at 15:22
  • @Remario how would I check that? – mrsB2013 Mar 06 '18 at 15:30
  • The `R` file should be built provided there aren't any other errors in your code. Have you tried manually including `import com.myproject.R` at the top of your file? (Where `com.myproject` should be replaced with your application package) – Michael Dodd Mar 06 '18 at 15:34
  • @extmkv do you want the log when I try to compile without an R.java or when I manually add the eclipse R.java to the app path? – mrsB2013 Mar 07 '18 at 15:18
  • @MichaelDodd that also did not work. The application had originally had that. I have tried with those lines and with removing it. – mrsB2013 Mar 07 '18 at 15:20
  • Try a manual build from the terminal with `gradlew clean build` - Do any errors or warnings appear in the output? – Michael Dodd Mar 07 '18 at 16:51
  • @MichaelDodd that was a huge help. I was able to find errors and fix them and now my R.java file is generated. If you convert that to an answer I would mark it as my accepted answer. Thanks!! – mrsB2013 Mar 07 '18 at 19:43

1 Answers1

3

The main reason for R.java not generating is a failed build, R.java is generated from all the IDs and other constants defined in the /res folder. If it's unclear as to why R.java is not generating, manually build your project by going to the terminal and typing

gradlew clean build

for Windows, or

./gradlew clean build

for Mac or Linux. Any build errors that may have been masked by the IDE's error reporting will be shown in full in the terminal. Once these errors have been fixed, then R.java will successfully generate.

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64