2

My project my directory looks like this:

-project
  -someModule
  -mainProjectModule
    -src
      -main
        -java
          (all of my code)

I am trying to add a src-gen folder so it will look like this:

-project
  -someModule
  -mainProjectModule
    -src
      -main
        -java
          (all of my code)
    -src-gen
      -main
        -java
          (all of my generated code)

My generated code is in the same package as my non generated code. How can I make the /src-gen/main/java folder turn blue in Android Studio, and what do I need to put in my gradle to get this to build properly? I currently have this in my gradle and it doesn't seem to work:

sourceSets {
    main {
        java.srcDirs = ['src/main/java', 'src-gen/main/java']
    }
}

I have also tried manually editing the mainProjectModule.iml file to add the /src-gen/main/java folder as source, and it will turn blue, but it will automatically change back after several seconds.

clocksmith
  • 6,226
  • 3
  • 32
  • 45
  • Is the source generated via the build script, or is it generated through some other method? – Scott Barta Mar 15 '14 at 21:15
  • I have 2 different generated sources. One is greendao (which comes from running separate module within the project. The other is google app engine endpoints which comes from a completely different project altogether. If I simply put these generated sources directly into src/main/java it works, but id rather separate them. – clocksmith Mar 15 '14 at 21:17
  • 1
    That syntax works for me. Is your `sourceSets` block inside your `android` block? If so, what version of Android Studio are you running? – Scott Barta Mar 15 '14 at 21:25
  • Wow. I had sourceSets outside the android block. I moved it and it worked. Thanks! – clocksmith Mar 15 '14 at 21:32
  • Great, added as an official answer with a little more explanation. – Scott Barta Mar 15 '14 at 21:35

1 Answers1

8

The sourceSets block needs to be inside the android block for Android build files. Note that this is different from plain Java builds, where that block goes at the top level:

android {
    ...
    sourceSets {
        main {
            java.srcDirs = ['src/main/java', 'src-gen/main/java']
        }
    }
}

As you've seen, editing the .iml files directly doesn't work for long -- those get overwritten every time Android Studio syncs the Gradle files with the project, which is somewhat often.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks for your answer, could you take a look at my question, please? http://stackoverflow.com/questions/24578939/android-studio-canonical-way-to-include-a-code-generation-task – motobói Jul 04 '14 at 18:25