问题
I have src/main/java
for the main source set. Also a src/heroes/java
source set for a product flavor named heroes
and a src/villains/java
source set for a product flavor named villains
.
Those have only a single build type, debug
. Meaning the build variants pane will contain heroesDebug
and villainsDebug
.
Now, in Android Studio, if I select build variant heroesDebug
, src/main
and src/heroes
become "active" while src/villains
"deactivates". Meaning I can use ALT + Insert, code complete, and other such features of the IDE on those "active" source sets. Things like syntax highlighting won't even show on files inside an "inactive" source set.
And that's mostly fine. But I'll have a huge problem if I rename or move a lot of classes in src/main
that are imported in many places within src/villains
. I've noticed that the refactored package or class names reflect in src/heroes
but when I switch to villainsDebug
, I'll see red lines and Cannot resolve symbol
errors in src/villains
where the IDE should've updated the new names or locations of packages or classes!
That also works in reverse — if villainsDebug
is active and I reshuffle some code in src/main
, the changes will propagate to src/villains
but if I again switch to heroesDebug
, those changes will be nowhere to be seen.
EDIT
So how do I get changes in src/main
to propagate to all the other source sets of "inactive" build variants?
回答1:
try to add this in your apps build.gradle :
android{
...
sourceSets{
main{
java.srcDirs = ['src', 'heroes', 'villains']
}
}
}
this will cause all directories to be visible and you should do this only while refactoring and then remove this afterwards, because you will see duplicate class exceptions. Also this will create a weird Android project view, with multiple java folders. Probably not the best solution, but it worked for me
回答2:
So how do I get all source sets to be "active" irregardless of what build variant I've selected?
From IDE point of view, this is impossible at the current Android Studio (3.1.4) release, and I think this is a per design feature which is reasonable. Because, your source sets are supposed to be isolated according to build variants and should not impact on each other.
If you just want to relocate those "shared" sources now and then, probably you can add additional source sets which is independent on build variants, it will be something like below.
sourceSets {
String sharedSources = "$projectDir/src/sharedSources/java"
main {
}
heroes {
java.srcDirs += sharedSources
}
villains {
java.srcDirs += sharedSources
}
}
So, your directory structure will be as below, but "sharedSources" is NOT a build variant here.
app
├── src
│ ├── heroes
│ ├── heroesDebug
│ ├── heroesRelease
│ ├── villains
│ ├── villainsDebug
│ ├── villainsRelease
│ ├── sharedSources // NOT indicating a build variant. Move those classes out to here.
│ └── main
来源:https://stackoverflow.com/questions/51884947/refactoring-inside-the-main-source-set-doesnt-propagate-to-inactive-build-varia