良好做法Gradle Flavors With Class

时间:2015-07-17 14:52:15

标签: java android gradle android-gradle android-productflavors

我正在寻找有关如何使用gradle flavor来定义Android应用程序行为的信息。

  

假设我想在我的flavor1中开始一个新的活动,而不是   我的味道。

由于gradle不会互相覆盖类,

我必须在每种口味中定义活动

from Tkinter import *

root = Tk()
root.geometry("300x200+500+400")
lines = [StringVar(), StringVar()]
strings = [["Hello", "Stack", "Overflow"], ["Whats", "Going", "On"]]
buttons = [[],[]]

l1 = Label(root, text = "Selection: ", justify = LEFT)
l1.grid(column = 0, row = 0, sticky = NW, padx = (0, 250))
l1.grid_propagate(False)

l2 = Label(root, text = "Selection: ", justify = LEFT)
l2.grid(column = 0, row = 4, sticky = NW, padx = (0, 250))
l2.grid_propagate(False)

def process(line):
    global l1, l2, strings, lines

    if line == lines[0]:
        # Since lines[0] was passed in to the callback, we know to update line 0; 
        # take that line's label (or canvas in your case)
        updateLine = 0
        updateLabel = l1
    else:
        # Otherwise take the other line
        updateLine = 1
        updateLabel = l2

    # These operations are performed within if/elif/else to show how you coul
    # choose a different process for each Radiobutton: example, coloring a canvas differently
    if lines[updateLine].get() == strings[updateLine][0]:
        # This means the first button of whatever line was selected
        updateLabel.config(text = "Selection: %s" %strings[updateLine][0])
    elif lines[updateLine].get() == strings[updateLine][1]:
        # This means the second button of whatever line was selected
        updateLabel.config(text = "Selection: %s" %strings[updateLine][1])
    else:
        # You get the idea
        updateLabel.config(text = "Selection: Bet you thought I'd say %s" %strings[updateLine][2])

# Must have a seperate row number because with multiple lines, we can't simply use 'i' or 'j'
rowNum = 1
for i in range(len(lines)):
    for j in range(len(strings[i])):
        buttons[i].append(Radiobutton(root, text = strings[i][j], variable = lines[i], value = strings[i][j], command = lambda line = lines[i]: process(line)))
        buttons[i][j].grid(column = 0, row = rowNum, sticky = NW)
        rowNum +=1
    rowNum += 2

root.mainloop()

或者,在我的主文件夹中定义它并使用Constant类中的枚举,如果我在我的flavor1或我的flavor2中则返回

app/src/
     |-> flavor1/java/<package>/Activity.java
     |-> flavor2/java/<package>/Activity.java
     |-> main/java/<package>

我的猜测是我必须混合两种解决方案:

  • 如果我想在flavor1中创建一个独特的活动,我必须在flavor2中创建同一个类,可能只是app/src/ |-> flavor1/java/<package>/Constant.java |-> flavor2/java/<package>/Constant.java |-> main/java/<package>/Activity.java
  • 如果我想从throw RuntimeException("stub!")调用此活动,我必须使用main
  • 之类的内容

在我看来,这是使用gradle风味的最酷方式。但是我想确保在最终实施之前没有其他最好的方法来处理它。

有没有人在博客上写过某些内容或者想发表自己的意见?我很想读你!

提前致谢!

2 个答案:

答案 0 :(得分:0)

您可以在每种风格上放置不同的AndroidManifest文件。例如,你可以在flavor1中定义(让他们调用它)可选的Activity,在flavor1中扩展可选Activity的Class,你就完成了!请注意,AndroidManifest文件必须仅指定与主AndroidManifest不同的内容:在这种情况下,您应该只包含<activity/>(当然在空<application/>内)

答案 1 :(得分:0)

如果你想避免使用if (Constant.getFlavor() == flavor1),你可以创建一个工厂类,然后返回该工具类的正确意图。然后在每种风味中覆盖它。这样可以避免使用if语句污染代码。