Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to avoid Gradle duplication in multi-module Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to avoid Gradle repetition in multi-module Android. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

When you have an Android project that contains many modules, you may find that many gradle configurations are copied (usually by copy and paste) in the build.gradle file in each module.

I read all kinds of ways to avoid this situation on the Internet until I saw this blog post. This article uses the name of the module to determine which gradle plug-ins to apply and the configuration to use for that module.

I have used something similar in one of my many module projects. The difference, however, is that I apply the required plug-ins in each separate module build.gradle, and in the root build.config, I use both the module name and its attributes to add the appropriate shared configuration.

For example, here is the common shared configuration of all modules in the root build.gradle:

Subprojects {afterEvaluate {project-> if (project.hasProperty ("android")) {android {compileSdkVersion 30 buildToolsVersion '30.0.2' defaultConfig {minSdkVersion 23 targetSdkVersion 30} compileOptions { SourceCompatibility JavaVersion.VERSION_11 targetCompatibility JavaVersion.VERSION_11}}

This application is a hybrid Java and Kotlin project, so only a few modules use Kotlin. In the Kotlin module, I applied the kotlin plug-in.

Apply plugin: 'com.android.library'apply plugin:' kotlin-android'

Then in the root build.gradle, I can add the Kotlin configuration to these modules:

If (project.hasProperty ('kotlin')) {android {kotlinOptions {jvmTarget = JavaVersion.VERSION_11.toString ()}} dependencies {implementation' org.jetbrains.kotlin:kotlin-stdlib-jdk8' implementation 'androidx.core:core-ktx'}}

If there is a generic configuration that applies only to certain modules, I can also use the module name as a filter to add that configuration only for those modules. For example, for all modules except the app module:

If (project.hasProperty ('android') & &! name.equalsIgnoreCase (' app')) {.}

Also for modules with specific names:

If (project.hasProperty ('android') & & name.equalsIgnoreCase (' feature-1')) {.}

Note that I use the root build.gradle to include the generic configuration to keep it simple, but there are more complex ways to use the buildSrc directory instead.

This is the end of this article on "how to avoid Gradle repetition in multi-module Android". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report