11

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

Cordova is generating a new settings.gradle file when I run "cordova build android". I have attempted using a script to modify this file using hooks after_prepare before_compile. But no matter what I do, this file is recreated. Has anyone solved this problem? Is there another way to add a module to the android project besides using the settings.gradle?

I know next to nothing about Java or Gradle, so any insight would be great.

EJK
  • 12,332
  • 3
  • 38
  • 55
Nathan
  • 153
  • 1
  • 8
  • I'm having my own difficulties trying to get a multi-project setup going with additional modules. Again, this requires the ability to modify the settings.gradle file, however it does not appear that cordova provides any ability to control the contents of this file. – Chris Noldus Jun 01 '16 at 00:22

2 Answers2

4

You 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
}
Raphaël Balet
  • 6,334
  • 6
  • 41
  • 78
NullPointer
  • 7,094
  • 5
  • 27
  • 41
2

You can include or exclude dependency using build-extras.gradle file. This file can be added along with build.gradle file in the same location using before_build hook action.

Request you to check Official Cordova documentation for more info on the same. Also check out this example which explains exclusion of duplicate modules. The same can be extended for module inclusion as well.

Updated: I do understand that the question is about settings.gradle and I m talking about build.gradle. That's because as far as i know, there is no way of directly manipulating settings.gradle with the exception of through build.gradle as its explained in the example link. Also i suggest you to have a look at this gradle thread which explains adding dependency via build.gradle file.

But if you are still looking for a solution to manipulate settings.gradle, you gotta edit build.js file in android platform as suggested in this post which is more of a quick fix or tweak.

I hope it helps.

Community
  • 1
  • 1
Gandhi
  • 11,875
  • 4
  • 39
  • 63
  • 1
    The settings.gradle file is different from the build.gradle file, the official documentation does not provide any insight as to how one might be able to add directives to the settings.gradle file. – Chris Noldus Jun 01 '16 at 00:16
  • @ChrisNoldus Hi Chris,I do understand settings.gradle is different from build,gradle and in official documentation there is no insight of the same. But as far as i investigated, there is no direct way of editing settings.gradle file with the exception of manipulating it through build.gradle. Request you to check out this link too - https://discuss.gradle.org/t/multiple-projects-dependencies-and-compiling/9730 But if you are still concerned about modifying setting.gradle file, have updates the quick fix to in my updated answer. Request-Kindly reconsider before down voting as it demotivates us. – Gandhi Jun 01 '16 at 02:34
  • 1
    @Ghandi - appreciate the effort, the downvote was originally because your answer does not answer the question - of course I assumed the asker has looked at the documentation and trying to do the same as me. As this may not be the case, downvote removed. – Chris Noldus Jun 01 '16 at 03:28
  • @Chris Thanks Chris. Hope my answer makes more sense now. Thanks again for helping me in refining the answer – Gandhi Jun 01 '16 at 03:34
  • This was partially correct once i had this information i was able to add the Library via plugin.xml this added what i needed to settings.gradle then i was able to also add a build-extras.gradle in the plugin because cordova overwrites the build.gradle in the sub project so the sub-project specific things needed added back in – Nathan Jun 09 '16 at 18:37
  • @Nathan Could you share how you used a plugin to manipulate settings.gradle? Maybe in a seperate answer to your original question how you solved the problem at the end. – janpio Oct 03 '17 at 23:26
  • @janpio what exactly are you looking for? – Gandhi Oct 04 '17 at 04:37
  • @Nathan See https://stackoverflow.com/questions/46554949/how-to-add-native-module-to-cordova-android-project Have to add a module, is done via settings.gradle which gets overwritten with each build. That's why your explanation sounded so interesting. – janpio Oct 04 '17 at 08:13
  • Today i faced same problem and found that there is a way to **manipulate settings.gradle** by using **project.properties**. I will give solution with in my answer – NullPointer Jul 13 '18 at 08:15