How to prevent Cordova build command from auto-generating settings.gradle

女生的网名这么多〃 提交于 2019-12-01 22:38:59

问题


I have created a Cordova App with a custom settings.gradle as folows:

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

and in build.gradle, I can refer to it as:

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')
}

However, when I execute command 'cordova build android', the file settings.gradle is auto-generated to a default setting which looks like:

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

As a result, build always failed due to unable to locate module 'manager' that I have defined in settings.gradle.

I wonder if there is any way to prevent build command from duplicating a custom settings.gradle file.


回答1:


Today i faced the same problem and by spending hours i found that we can do this by change in project.properties

Below are the steps:

Step-1. Edit/Make project.properties in root directory and add your module as library reference after CordovaLib:

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

Step-2. Run cordova build android. This will make an entry in your setting.gradle file.

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

Also your app build.gradle will look like this:

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
}

For project(':manager').projectDir = new File('libs/ConnectManager') this kind of setting there is no easy way i found but you can achieve in this way:

Step-1. /path/to/project/platforms/android/cordova/lib/builders/GradleBuilder.js

Step-2. Edit fs.writeFileSync() function(Line-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.


来源:https://stackoverflow.com/questions/33036614/how-to-prevent-cordova-build-command-from-auto-generating-settings-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!