使用 Build Flavors - 正确构建源文件夹和 build.gradle [英] Using Build Flavors - Structuring source folders and build.gradle correctly

查看:59
本文介绍了使用 Build Flavors - 正确构建源文件夹和 build.gradle的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请注意:答案在 Xavier 的答案之后编辑

我正在尝试使用不同的 在 Android Studio 中为同一个应用程序项目构建 Flavors.但是,我似乎很难将其配置为正常工作.

步骤:

  1. 创建一个名为Test"的新 Android Studio 项目.
  2. 打开 build.gradle* 并添加以下几行:

    productFlavors {风味 1 {包名'com.android.studio.test.flavor1'}风味2{包名'com.android.studio.test.flavor2'}}

  3. 重新启动 Android Studio 后,我现在在 Build Variants 部分下看到 4 个 build 变体.这意味着到目前为止我们已经成功地设置了产品口味.**
  4. flavor1创建了一个新的源文件夹;但是,我不确定我的做法是否正确.我是这样做的:

    • 请记住,我为此项目的包名称是:com.foo.test
    • 右键单击src文件夹,对于flavor1,我实际上在资源管理器中创建了各个文件夹,结构为src/flavor1/java/com/foo/测试/MainActivity.java.
    • 上述方法运行良好,因为java"文件夹位于蓝色中,这意味着 IDE 知道其活动源目录.此外,该包是自动创建的.尽管如此,我还是收到了发现重复类的警告.在此处查看屏幕截图.
    • 对于flavor2,我尝试手动创建包,但flavor2 的'src' 文件夹似乎不是蓝色的,因此右键单击时选项不同,并且我无法使用'New Package'.在这里查看图片.
    • 请注意,对于flavor1,我还创建了一个res"目录,它确实会变成蓝色,但尽管如此,它不提供创建Android资源文件或Andorid资源目录的能力,以防万一为不同的口味使用不同的资源.

我做错了吗?或者我错过了什么?如果您需要更多信息,请告诉我.

*我的项目似乎有两个 build.gradle 文件.一个位于项目文件夹 (\GradleTest) 的根目录下,这个是空的.第二个位于\GradleTest 子文件夹的根目录,也标记为'GradleTest' (GradleTest-GradleTest),这是打开时已经有代码的那个;因此,这是我编辑的那个.

** 我检查了 gradle 设置,显然使用自动导入已经启用. 尽管如此,对 build.gradle 文件进行更改不会自动更新构建变体.注意:我也尝试过使用 Build - Rebuild Project 和/或 Build - Make Project,不行.我仍然必须关闭该项目,然后重新打开以使更改生效.

解决方案

如果您进入 Studio 首选项,则在 Gradle 部分下,您可以为您的项目启用自动导入(稍后我们将默认启用此功能).这将使 Studio 在您编辑它时重新导入您的 build.gradle.

创建口味并不意味着您要为它们使用自定义代码,因此我们不会创建文件夹.您确实需要自己创建它们.

如果您查看我的 IO 演讲,您会看到我们是如何混合的将来自风味和构建类型的值结合在一起以创建变体.

对于 Java 源代码:

src/main/java源代码/风味1/java源代码/调试/Java

全部 3 个都用于创建单个输出.这意味着它们不能定义相同的类.

如果您想在两种风格中拥有同一类的不同版本,则需要在两种风格中创建它.

src/flavor1/java/com/foo/A.javasrc/flavor2/java/com/foo/A.java

然后你在 src/main/java 中的代码就可以了

import com.foo.A

根据选择的风格,使用正确版本的 com.foo.A.

这也意味着 A 的两个版本必须具有相同的 API(至少当涉及到 src/main/java/... 中的类使用的 API 时)

编辑以匹配修改后的问题

此外,将相同的 A 类仅放在互斥的源文件夹中很重要.在这种情况下,src/flavor1/java 和 src/flavor2/java 永远不会被一起选择,但 main 和 flavor1 是.

如果您想提供不同风格的活动的不同版本,请不要将其放在 src/main/java 中.

请注意,如果您有 3 种口味,并且只想要一个用于口味 1 的自定义,而口味 2 和口味 3 共享相同的活动,您可以为这两个其他活动创建一个公共源文件夹.您可以完全灵活地创建新的源文件夹并配置源集以使用它们.

关于你的其他观点:

第二个风味源文件夹不是蓝色是正常的.你需要切换到第二个风格来启用它,然后你就可以在里面创建包和类了.在此之前,Studio 不会将其视为源文件夹.我们希望在未来改进这一点,让 IDE 知道那些非活动源文件夹.

我觉得在res文件夹里不能创建资源文件也是正常的.菜单系统尚未更新以处理所有这些额外的资源文件夹.这将在以后出现.

Please note: Answer edited after Xavier's Answer

I am trying to use different Build Flavors for one same Application project in Android Studio. However, I seem to be having a terrible time configuring it to work appropriately.

Steps:

  1. Create a new Android Studio Project, named 'Test'.
  2. Open build.gradle* and added the following lines:

    productFlavors {
    flavor1 {
        packageName 'com.android.studio.test.flavor1'
        }
    flavor2 {
        packageName 'com.android.studio.test.flavor2'
        }
    }
    

  3. 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. **
  4. 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 is src/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.

Am I doing something wrong? Or am I missing something? Let me know if you need more info.

*My Project seems to have two build.gradle files. One located on the root of the project folder (\GradleTest), this one is empty. The second one located on the root of a subfolder of \GradleTest, also labeled 'GradleTest' (GradleTest-GradleTest), this is the one that already had code when opened; therefore, that is the one I edited.

** I checked gradle settings and apparently Use auto-import was already enabled. Despite of this, making changes to the build.gradle file doesn't automatically update the build variants. Note: I also tried using Build - Rebuild Project, and/or Build - Make Project, no-go. I still have to close the project, and re-open for changes to take effect.

解决方案

If you got in the Studio preferences, under the Gradle section, you can enable auto-import for your project (we'll enable this by default later). This will let Studio re-import your build.gradle whenever you edit it.

Creating flavors doesn't mean you're going to use custom code for them so we don't create the folders. You do need to create them yourself.

If you look at my IO talk you'll see how we mix in together values from the flavors and build type to create the variant.

For the Java source:

src/main/java
src/flavor1/java
src/debug/java

are all 3 used to create a single output. This means they can't define the same class.

If you want to have a different version of the same class in the two flavor you'll need to create it in both flavors.

src/flavor1/java/com/foo/A.java
src/flavor2/java/com/foo/A.java

And then your code in src/main/java can do

import com.foo.A

depending on the flavor selected, the right version of com.foo.A is used.

This also means both version of A must have the same API (at least when it comes to the API used by classes in src/main/java/...

Edit to match revised question

Additionally, it's important to put the same A class only in source folders that are mutually exclusive. In this case src/flavor1/java and src/flavor2/java are never selected together, but main and flavor1 are.

If you want to provide a different version of an activity in different flavor do not put it in src/main/java.

Do note that if you had 3 flavors and only wanted a custom one for flavor1, while flavor2 and flavor3 shared the same activity you could create a common source folders for those two other activities. You have total flexibility in creating new source folders and configuring the source set to use them.

On to your other points:

It's normal that the 2nd flavor source folder is not blue. You need to switch to the 2nd flavor to enable it, and then you'll be able to create packages and classes inside. Until then, Studio doesn't consider it to be a source folder. We'll hopefully improve this in the future to make the IDE aware of those unactive source folders.

I think it's also normal that you can't create resource files in the res folder. The menu system hasn't been updated to deal with all these extra resource folders. This will come later.

这篇关于使用 Build Flavors - 正确构建源文件夹和 build.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆