首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Build.Gradle模板

Build.Gradle模板
EN

Stack Overflow用户
提问于 2022-08-31 19:47:27
回答 1查看 36关注 0票数 0

有人知道如何在创建模块时模板build.gradle文件吗?现在,我遇到的问题是,我希望模块的build.gradle只包含少量信息,因为我将在顶级build.gradle文件中设置共享配置。我还有一个buildSrc文件夹设置来集中版本控制。

顶级build.gradle文件:

代码语言:javascript
复制
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath Deps.tools_gradleandroid
        classpath Deps.tools_kotlin
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.7.10'
    }
}

subprojects {
    afterEvaluate { project ->
        if (project.hasProperty('android')) {
            android {
                buildToolsVersion Config.buildTools
                compileSdkVersion Config.compileSdk

                defaultConfig {
                    minSdkVersion Config.minSdk
                    targetSdkVersion Config.targetSdk
                    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
                }

                buildTypes {
                    release {
                        minifyEnabled false
                        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                    }
                }

                compileOptions {
                    sourceCompatibility Config.javaVersion
                    targetCompatibility Config.javaVersion
                }

                kotlinOptions {
                    jvmTarget = Config.javaVersion.toString()
                }

            }
        }
    }
}

模块的预期build.gradle文件:

代码语言:javascript
复制
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    defaultConfig {
        applicationId "com.company.expectedmodule"
    }
}

dependencies {
    implementation Deps.androidx_core
    implementation Deps.androidx_appcompat
    implementation Deps.androidx_material
    testImplementation Deps.testlib_junit
    androidTestImplementation Deps.testandroidx_junit
    androidTestImplementation Deps.testandroidx_espressocore
}

当我创建“新模块”时,build.gradle文件是什么样子的:

代码语言:javascript
复制
plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.company.newmodule"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.5.0'
    implementation 'com.google.android.material:material:1.6.1'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

编辑:为了简单起见,我将依赖项括号移到顶级build.gradle文件中,并将模块改为库,并将库模块的代码简化如下:

代码语言:javascript
复制
plugins {
    id 'com.android.library'
    id 'org.jetbrains.kotlin.android'
}

android {

}
EN

回答 1

Stack Overflow用户

发布于 2022-09-07 08:02:27

在Gradle中共享构建逻辑的推荐方法是使用约定插件。就像在这个示例里。

此外,预编译脚本插件可以用类似于常规构建脚本的语法编写插件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73561151

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档