سؤال

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.

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top