How to prevent Cordova build command from auto-gen

2019-02-25 08:26发布

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条回答
乱世女痞
2楼-- · 2019-02-25 09:08

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.
查看更多
登录 后发表回答