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

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

问题描述

请注意:答Xavier的答案后,编辑

Please note: Answer edited after Xavier's Answer

我试图用不同的构建香料在安卓的Studio One相同的应用程序项目。但是,我似乎有一个可怕的时间来配置它的工作适当。

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.

的步骤:

  1. 创建一个新的Andr​​oid Studio项目,命名为测试。
  2. 打开build.gradle *,并添加以下行:

  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'
    }
}

  • 重新启动Android的工作室后,我现在看到的 4构建生成下变变异单元。这意味着我们是成功的设定好上了产品口味为止。 **
  • 创建一个新的源文件夹中的 flavor1 ;但是,我不知道如果我做了正确的方式。以下是我做的:

  • 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:

    • 请记住,这个项目我的包名称: com.foo.test
    • 右键点击的src 文件夹,flavor1,我居然创造了资源管理器中的各个文件夹,在某种程度上,该结构 SRC /flavor1/java/com/foo/test/MainActivity.java
    • 在上面的效果很好,因为Java的文件夹中的蓝色,这意味着IDE知道它的积极的源目录。另外,自动创建的包。尽管这样,我得到了重复类的警告中。 见截图在这里。
    • 对于flavor2,我试图手动创建包,但是SRC文件夹flavor2似乎不是蓝色,因此选项是不同的,当正确的点击,以及新包装不适用于我使用。在这里见图片。
    • 请注意,对于flavor1,我还创建了一个资源目录,这并变成蓝色,但尽管这一点,不提供或者创建一个Android资源文件,或者Andorid的资源目录的能力,在情况下,我想使用不同resoruces为不同的口味。
    • 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.

    *我的项目似乎有两个 build.gradle文件。其中位于项目文件夹(\ GradleTest)的根,这个人是空的。第二个位于\ GradleTest的子文件夹,还标有根'GradleTest(GradleTest-GradleTest),这是一个已经有code打开时;因此,这是一个我编辑。

    *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.

    **我检查摇篮的设置,显然使用自动导入已经启用。尽管这样,在更改build.gradle文件不会自动构建变种更新。 注意:我也尝试过使用建设 - 重建项目,和/或建设 - 做项目,不走了。我还是要关闭该项目,并重新打开使更改生效。

    ** 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.

    推荐答案

    如果您在工作室preferences了,在摇篮部分,您可以启用自动导入你的项目(我们会在默认情况下启用此后来)。这将让Studio中重新导入你的build.gradle每当编辑它。

    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.

    创建味道并不意味着你要使用自定义$ C $下他们,所以我们没有创建文件夹。你需要自己创建。

    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.

    如果你看一下 我的IO谈 你会看到我们如何搭配从口味共同价值观,构建类型来创建变体。

    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.

    有关Java源代码:

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

    都3用于创建一个单一的输出。这意味着他们不能定义相同的类。

    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
    

    和那么你的code在src / main / java下可以做

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

    import com.foo.A
    

    取决于所选择的风味,正确版本com.foo.A的被使用。

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

    这也意味着这两个版本A的必须具有相同的API(至少当它涉及到在src / main / java的/...

    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/...

    修改以符合经修订的课题

    此外,它把同一个类只在互相排斥的源文件夹很重要。在这种情况下的src / flavor1 / java和SRC / flavor2 / Java是从来没有一起选择,但主flavor1的。

    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.

    如果你希望提供一个不同的版本,不同风味的活动不把它在src / main / java中。

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

    请注意,如果你有3个味道,只想要一个自定义的flavor1,而flavor2和flavor3共享同一个活动,你可以创建这两个其他活动共同的源文件夹。你必须创建新的源文件夹和配置源设置为使用它们总的灵活性。

    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.

    在你的其他问题:

    这是正常的,第二味源文件夹不是蓝色。你需要切换到第二个味来启用它,然后你就可以到里面创建包和类。在此之前,Studio不认为这是一个源文件夹。我们将有望改善这一在未来让IDE知道那些的 unactive 的源文件夹。

    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.gradle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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