如何防止自动生成settings.gradle的Cordova构建命令

时间:2015-10-09 11:02:20

标签: android cordova android-gradle build.gradle cordova-plugins

我创建了一个带有自定义settings.gradle的Cordova应用程序,如下所示:

// GENERATED FILE - DO NOT EDIT
include ":"
include ":CordovaLib"
include 'manager'
project(':manager').projectDir = new File('libs/ConnectManager')

在build.gradle中,我可以将其称为:

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    // SUB-PROJECT DEPENDENCIES START
    debugCompile project(path: "CordovaLib", configuration: "debug")
    releaseCompile project(path: "CordovaLib", configuration: "release")
    // SUB-PROJECT DEPENDENCIES END
    compile project(':manager')
}

然而,当我执行命令' cordova build android'时,文件settings.gradle会自动生成为默认设置,如下所示:

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

因此,由于无法找到模块'经理'我已在settings.gradle中定义。

我想知道是否有任何方法可以阻止构建命令复制自定义settings.gradle文件。

1 个答案:

答案 0 :(得分:0)

今天我遇到了同样的问题,花了几个小时,我发现我们可以通过更改 project.properties

来做到这一点。

以下是步骤:

第1步。在根目录中编辑/制作project.properties,并将模块作为库引用添加到CordovaLib之后:

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
}

对于project(':manager').projectDir = new File('libs/ConnectManager')这种设置,我找不到简单的方法,但可以通过以下方式实现:

第1步。 /path/to/project/platforms/android/cordova/lib/builders/GradleBuilder.js

第2步。编辑fs.writeFileSync()函数(第100行)

  // Write the settings.gradle file.
fs.writeFileSync(path.join(this.root, 'settings.gradle'),
    '// GENERATED FILE - DO NOT EDIT\n' +
    'include ":"\n' + settingsGradlePaths.join('')+ "'include :"+libraryModule1+" \n'+ 'include :"+libraryModule2+"');

// Update dependencies within build.gradle.