android gradle 构建,Android使用Gradle配置构建变体

每个构建变体都代表您可以为应用构建的一个不同版本。例如,您可能希望构建应用的免费版本(只提供有限的内容)和付费版本(提供更多内容)。您还可以针对不同的设备、根据 API 级别或其他设备变体构建应用的不同版本。然而,如果您希望根据设备 ABI 或屏幕密度构建不同的版本,则请改用 [APK 拆分]

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

gradle.png

主要是在android {} 代码块内部创建和配置构建类型,其中defaultConfig是一些基本配置,buildTypes就是构建类型,其中默认情况下已经有了一个release版本,就是所谓的发布版本,release{}代码块里面就是属于release版本的一些配置,其实默认情况下应该还有一个debug版本,就是平常的调试版本,只是这里没有显示出来。

配置构建类型buildTypes

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

buildtype.png

这里我们来看一下buildTypes的基本构建,

applicationIdSuffix: 是在当前applicationId的基础上拼接剩下的字段形成一个新的applicationId,因为同一个applicationId是无法同时运行在同一个手机上的,这里也可以直接使用applicationId属性来命名新的一个包名。

initWith:复制其他buildTypes的内容,这里的jnidebug变体就复制了debug变体的内容。一般写法是可以命名一个默认的变体,将一些公共的变量在里面赋值从而给其他变体使用。

配置产品风味ProductFlavor

创建产品风味与创建构建类型类似:只需将它们添加到 productFlavors {} 代码块并配置您想要的设置。产品风味支持与 defaultConfig 相同的属性,这是因为 defaultConfig 实际上属于 ProductFlavor 类。这意味着,您可以在 defaultConfig {} 代码块中提供所有风味的基本配置,每种风味均可更改任何这些默认值,例如 applicationId。

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

ProductFlavor

.png

这里可以看到使用和buildTypes类似,具体需要哪些属性可以查看官网地址。这里有个地方应该注意的是,编译类型其实是buildTyps和productFlavor的组合,这里有三种buildTypes和两种productFlavor所以最终有6种编译类型。

在创建和配置您的产品风味之后,在通知栏中点击 Sync Now。在同步完成后,Gradle 会根据您的构建类型和产品风味自动创建构建变体,并按照 的格式命名这些变体。例如,如果您创建了“演示”和“完整”这两种产品风味并保留默认的“调试”和“发布”构建类型,Gradle 将创建以下构建变体:

演示调试

演示发布

完整调试

完整发布

您可以将构建变体更改为您要构建并运行的任何变体,只需转到 Build > Select Build Variant,然后从下拉菜单中选择一个变体。然而,要开始自定义每个构建变体及其功能和资源,您需要了解如何创建和管理源集。

组合多个商品风味

某些情况下,您可能希望组合多个产品风味中的配置。例如,您可能希望基于 API 级别为“完整”和“演示”产品风味创建不同的配置。

为此,您可以通过适用于 Gradle 的 Android 插件创建产品风味组,称为风味维度。

构建您的应用时,Gradle 会将您定义的每个风味维度中的产品风味配置与构建类型配置组合来创建最终构建变体。Gradle 不会组合属于相同风味维度的产品风味。

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

3.png

flavorDimensions属性:创建一个“模式”风味维度以组织“完整”和“演示”产品风味,以及一个“api”风味维度以基于API级别组织产品风味配置

风味维度

demo

full

minApi24

[minApi24][Demo]

[minApi24][Full]

minApi23

[minApi23][Demo]

[minApi23][Full]

minApi21

[minApi21][Demo]

[minApi21][Full]

在 Gradle 为每个构建变体或对应 APK 命名时,属于较高优先级风味维度的产品风味首先显示,之后是较低优先级维度的产品风味,再之后是构建类型。以上面的构建配置为例,Gradle 可以使用以下命名方案创建总共 12 个构建变体:

构建变体:[minApi24, minApi23, minApi21][Demo, Full][Debug, Release]

对应 APK:app-[minApi24, minApi23, minApi21]-[demo, full]-[debug, release].apk

例如,

构建变体:minApi24DemoDebug

对应 APK:app-minApi24-demo-debug.apk

过滤变体

您可以在模块级 build.gradle 文件中创建一个变体过滤器,以移除某些构建变体配置。

假设您计划为演示版本的应用仅支持 API 级别 23 和更高级别。您可以使用代码块过滤出组合了“minApi21”和“演示”产品风味的所有构建变体配置:

android {

...

buildTypes {...}

flavorDimensions "api", "mode"

productFlavors {

demo {...}

full {...}

minApi24 {...}

minApi23 {...}

minApi21 {...}

}

variantFilter { variant ->

def names = variant.flavors*.name

// To check for a certain build type, use variant.buildType.name == ""

if (names.contains("minApi21") && names.contains("demo")) {

// Gradle ignores any variants that satisfy the conditions above.

setIgnore(true)

}

}

}

...

创建源集

默认情况下,Android Studio 会创建 main/ 源集和目录,用于存储在所有构建变体之间共享的一切资源。然而,当然也可以创建新的源集来控制 Gradle 要为特定的构建类型、产品风味(以及使用风味维度时的产品风味组合)和构建变体编译和打包的确切文件。

例如,可以在 main/ 源集中定义基本的功能,使用产品风味源集针对不同的客户更改应用的品牌,或者仅针对使用调试构建类型的构建变体包含特殊的权限和日志记录功能。

使用步骤如下:

(1)打开 Project 窗格并从窗格顶端的下拉菜单中选择 Project 视图。

(2)导航至 /app/src/。

(3)右键点击 src 目录并选择 New > Folder > Java Folder。

(4)从 Target Source Set 旁边的下拉菜单中,选择编译的版本。

(5)点击 Finish。

同样可以用来创建XML 值文件:

(1)在相同的 Project 窗格中,右键点击 src 目录并选择 New > XML > Values XML File。

(2)为 XML 文件输入名称或保留默认名称。

(3)从 Target Source Set 旁边的下拉菜单中,选择 编译的版本。

(4)点击 Finish。

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

4.png

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

5.png

76d639bd860b?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

6.png

更改源集配置

如果您的源未组织到 Gradle 期望的默认源集文件结构中,可以

使用 sourceSet{} 代码块更改 Gradle 希望为源集的每个组件收集文件的位置。

下面的代码示例可以将 app/other/ 目录中的源映射到 main 源集的某些组件,并更改 androidTest 源集的根目录。

android {

...

sourceSets {

// Encapsulates configurations for the main source set.

main {

// Changes the directory for Java sources. The default directory is

// 'src/main/java'.

java.srcDirs = ['other/java']

// If you list multiple directories, Gradle uses all of them to collect

// sources. Because Gradle gives these directories equal priority, if

// you define the same resource in more than one directory, you get an

// error when merging resources. The default directory is 'src/main/res'.

res.srcDirs = ['other/res1', 'other/res2']

// Note: You should avoid specifying a directory which is a parent to one

// or more other directories you specify. For example, avoid the following:

// res.srcDirs = ['other/res1', 'other/res1/layouts', 'other/res1/strings']

// You should specify either only the root 'other/res1' directory, or only the

// nested 'other/res1/layouts' and 'other/res1/strings' directories.

// For each source set, you can specify only one Android manifest.

// By default, Android Studio creates a manifest for your main source

// set in the src/main/ directory.

manifest.srcFile 'other/AndroidManifest.xml'

...

}

// Create additional blocks to configure other source sets.

androidTest {

// If all the files for a source set are located under a single root

// directory, you can specify that directory using the setRoot property.

// When gathering sources for the source set, Gradle looks only in locations

// relative to the root directory you specify. For example, after applying the

// configuration below for the androidTest source set, Gradle looks for Java

// sources only in the src/tests/java/ directory.

setRoot 'src/tests'

...

}

}

}

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值