简体   繁体   English

在应用程序模块和库模块之间共享逻辑

[英]Share logic between application module and library module

I have module.gradle.kts :我有module.gradle.kts

plugins {
    id("com.android.library")
    id("org.jetbrains.kotlin.android")
}

android {
    defaultConfig {
        ...
    }
}

And app.gradle.kts :app.gradle.kts

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
}

android {
    defaultConfig {
        ...
    }
}

I would like to define a common configuration of android.defaultConfig for both app.gradle.kts and module.gradle.kts in a dedicated gradle file, such as root.gradle.kts :我想在专用的 gradle 文件中为app.gradle.ktsmodule.gradle.kts定义android.defaultConfig的通用配置,例如root.gradle.kts

plugins {
    // What plugin to use here since this is neither an application module nor a library module ?
    id("org.jetbrains.kotlin.android")
}

android {
    defaultConfig {
        // Common configuration to be used by app.gradle.kts and module.gradle.kts
    }
}

Is there a common ancestor to "com.android.application" and "com.android.library" that can be used in that case?在这种情况下是否可以使用“com.android.application”和“com.android.library”的共同祖先?

I've defined my own extension function in buildSrc/src/main/Extensions.kt :我在buildSrc/src/main/Extensions.kt中定义了我自己的扩展 function :

import com.android.build.api.dsl.CommonExtension

fun CommonExtension<*, *, *, *>.applyRootConfiguration() {
    defaultConfig {
        // ...
    }
}

Then I can apply it in an application or a library by doing:然后我可以通过执行以下操作将它应用到应用程序或库中:

android {
    applyRootConfiguration()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在应用程序模块中使用带有 JNI 的库模块 Android 库模块 - Use library module with JNI in application module Android library module 是否可以 Proguard 一个 Android 库模块,而不是应用程序模块? - Is it possible to Proguard an Android library module, but not the application module? 应用程序模块也可以用作库模块吗? - Can an application module also be used as a library module? Android Studio应用程序模块到库模块 - Android Studio application module to a library module 在Android中的模块之间共享ViewModel实例 - Share ViewModel instance between module in android 在应用程序模块和库模块中包含相同的jar库 - Including same jar library in application module and library module Android Studio 3 库模块和功能模块的区别 - Android Studio 3 difference between library module and feature module 如何将库模块中的可绘制资源用于我们的应用程序模块? - How to use drawable resource from library module in to our application module? Android:库模块如何使用应用程序模块中定义的类? - Android: How a library module use the class defined in application module? 如何在测试模块和应用程序模块之间共享代码? - 假货取决于来自应用程序模块的代码 - How can I share code between test module and app module? - Fakes depend on code from app module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM