如何在 cordova 中添加到 settings.gradle

how to add to settings.gradle in cordova

This is the same question I am to new to comment on it to see if he found a answer

当您 运行 “cordova build android”时,Cordova 正在生成一个新的 settings.gradle 文件 我尝试使用脚本来修改此文件在 _prepare before_compile 之后使用挂钩。但无论我做什么,都会重新创建此文件。有人解决了这个问题吗?还有另一种方法可以将模块添加到 android 项目吗?除了使用 settings.gradle

我对 java 或 Gradle 几乎一无所知,所以任何现场都很棒。

您可以使用 build-extras.gradle 文件包含或排除依赖项。可以使用 before_build 挂钩操作将此文件与 build.gradle 文件一起添加到同一位置。

请您查看Official Cordova documentation for more info on the same. Also check out this example,其中解释了重复模块的排除。同样可以扩展到模块包含。

更新:我明白问题是关于 settings.gradle,我说的是 build.gradle。这是因为据我所知,除了示例 link 中解释的通过 build.gradle 之外,没有其他方法可以直接操纵 settings.gradle。另外,我建议你看看这个 gradle thread,它解释了通过 build.gradle 文件添加依赖项。

但如果您仍在寻找操纵 settings.gradle 的解决方案,则必须按照此 post 中的建议在 android 平台中编辑 build.js 文件快速修复或调整。

希望对您有所帮助。

您可以通过更改 project.properties

来做到这一点

步骤如下:

Step-1. Edit/Make project.properties 在根目录中并在 CordovaLib:[=18= 之后添加你的模块作为库引用]

target=android-25
android.library.reference.1=CordovaLib
android.library.reference.2=libraryModule1
android.library.reference.3=libraryModule2

步骤 2. 运行 cordova build android。这将在您的 setting.gradle 文件中创建一个条目。

//GENERATED FILE - DO NOT EDIT
 include ":"
 include ":CordovaLib"
 include ":libraryModule1"
 include ":libraryModule2"

您的应用 build.gradle 也将如下所示:

dependencies {
    ----
   // SUB-PROJECT DEPENDENCIES START
    debugCompile(project(path: "CordovaLib", configuration: "debug"))
    releaseCompile(project(path: "CordovaLib", configuration: "release"))
    debugCompile(project(path: "libraryModule1", configuration: "debug"))
    releaseCompile(project(path: "libraryModule1", configuration: "release"))
    debugCompile(project(path: "libraryModule2", configuration: "debug"))
    releaseCompile(project(path: "libraryModule2", configuration: "release"))
    ----
    // SUB-PROJECT DEPENDENCIES END
}