我正在尝试在Android Studio中为同一个应用程序项目使用不同的Build Flavors。然而,我似乎无法很好地配置它以使其正常工作。
步骤:
- Create a new Android Studio Project, named 'Test'.
Open build.gradle* and added the following lines:
productFlavors { flavor1 { packageName 'com.android.studio.test.flavor1' } flavor2 { packageName 'com.android.studio.test.flavor2' } }
- After restarting Android Studio, I now see 4 build variants under the Build Variants section. Meaning we were succesful on seting up the product flavors so far. **
Created a new Source folder for flavor1; however, I am not sure if I'm doing it the right way. Here's how I did it:
- Keep in mind that my Package name for this project is:
com.foo.test
- Right click on the
src
folder, for flavor1, I actually created the individual folders in the explorer, in a way that the structure issrc/flavor1/java/com/foo/test/MainActivity.java
. - The above worked well, since the 'java' folder is in blue, meaning the IDE knows its an active source directory. Also, the package was automatically created. Despite of this, I am getting a warning for duplicate class found. See screenshot here.
- For flavor2, I tried creating the package manually, but 'src' folder for flavor2 seems not be in blue, and therefore the options are different when right clicked, and 'New Package' is not available for me to use. See image here.
- Note that for flavor1, I also created a 'res' directory, which does turn blue, but despite of that, doesn't offer the ability to create either an Android Resource file, or Andorid resource directory, in case I wanted to use different resoruces for different flavors.
- Keep in mind that my Package name for this project is:
我是否做错了什么?或者我漏掉了什么?如果需要更多信息,请告诉我。
*我的项目似乎有两个build.gradle文件。一个位于项目文件夹根目录(\GradleTest),这个是空的。第二个位于\GradleTest子文件夹的根目录下,也标记为“GradleTest”(GradleTest-GradleTest),这个在打开时已经有代码了;因此,我编辑的就是这个。
**我检查了gradle设置,显然使用自动导入已经启用。尽管如此,对build.gradle文件进行更改不会自动更新构建变体。注意:我也尝试使用Build - Rebuild Project和/或Build - Make Project,但都无法实现。我仍然必须关闭项目,并重新打开才能使更改生效。
applicationId
替代packageName
。 - Hamzeh Soboh