鑑於以下配置:搖籃buildType/productFlavor使用意想不到buildConfigField
productFlavors {
normal {
applicationId "com.app"
}
mock {
applicationId "com.app.mock"
}
}
buildTypes {
debug {
productFlavors.normal.buildConfigField "boolean", "mockMode", "false"
productFlavors.mock.buildConfigField "boolean", "mockMode", "true"
}
release {
productFlavors.normal.buildConfigField "boolean", "mockMode", "false"
// Release should never point to mocks. Ever.
productFlavors.mock.buildConfigField "boolean", "mockMode", "false"
}
}
我本來期望BuildConfig.mockMode = true;
,然而,這是最終構建配置:
public final class BuildConfig {
public static final boolean DEBUG = Boolean.parseBoolean("true");
public static final String APPLICATION_ID = "*****";
public static final String BUILD_TYPE = "debug";
public static final String FLAVOR = "mock";
public static final int VERSION_CODE = 1;
public static final String VERSION_NAME = "1.0";
// Fields from product flavor: mock
public static final boolean mockMode = false;
}
從一些調查/調試,我意識到,如果我實際上在發佈buildType中更改產品風格的值儘管mockDebug
被選爲我的構建變體,但更新了BuildConfig.mockMode
的值。
我已經實現了我想做一個更好的解決方案,所以我只是在尋找一個答案,可以幫助我瞭解爲什麼搖籃在這樣基礎上的配置作用,幫助我瞭解更多它在做什麼。
感謝您的答案,我想它是執行兩個路徑。你知道爲什麼兩個路徑都被執行了嗎?我認爲把它們分開的關鍵在於你可以爲不同版本配置不同的值。 –