Home » Android » Cannot build signed android package through visual studio cordova

Cannot build signed android package through visual studio cordova

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am trying to build a signed release package for my Android application using Visual Studio 2015 Cordova Tools.
I am using Cordova 5.1.1, which requires that I supply the build process with a build.json file, telling the application where the keystore are and what password is using.
However when I add the build.json file, I am not able to make a successful build to release.

I followed this guide: https://github.com/Microsoft/cordova-docs/tree/master/tutorial-package-publish#android

And got this error (with path edited out):

1>  ANDROID_HOME=C:\Program Files (x86)\Android\android-sdk (TaskId:11)
1>  JAVA_HOME=C:\Program Files (x86)\Java\jdk1.7.0_55 (TaskId:11)
1>  \build.json (TaskId:11)
1>  Reading build config file: \build.json (TaskId:11)
1>  \platforms\android\cordova\node_modules\q\q.js:126 (TaskId:11)
1>                      throw e; (TaskId:11)
1>                            ^ (TaskId:11)
1>  SyntaxError: Unexpected token  (TaskId:11)
1>      at Object.parse (native) (TaskId:11)
1>      at parseOpts (\platforms\android\cordova\lib\build.js:475:27) (TaskId:11)
1>      at Object.module.exports.run (\platforms\android\cordova\lib\build.js:529:16) (TaskId:11)
1>      at \platforms\android\cordova\build:36:22 (TaskId:11)
1>      at _fulfilled (\platforms\android\cordova\node_modules\q\q.js:798:54) (TaskId:11)
1>      at self.promiseDispatch.done (\platforms\android\cordova\node_modules\q\q.js:827:30) (TaskId:11)
1>      at Promise.promise.promiseDispatch (\platforms\android\cordova\node_modules\q\q.js:760:13) (TaskId:11)
1>      at \platforms\android\cordova\node_modules\q\q.js:574:44 (TaskId:11)
1>      at flush (\platforms\android\cordova\node_modules\q\q.js:108:17) (TaskId:11)
1>      at process._tickCallback (node.js:355:11) (TaskId:11)
1>  Command finished with error code 1: cmd /s /c ""\platforms\android\cordova\build.bat" --release "--buildConfig=\build.json"" (TaskId:11)
1>ERROR building one of the platforms : error : cmd: Command failed with exit code 1
1>  You may not have the required environment or OS to build this project (TaskId:11)
1>MDAVSCLI : error : cmd: Command failed with exit code 1
1>Done executing task "MdaVsCli" -- FAILED. (TaskId:11)

What am I doing wrong? It seems like it cant parse the JSON?

How to&Answers:

This issue is caused because the default build.json has a BOM that the NodeJS JSON parser doesn’t like. You can fix this in a number of ways, but the simplest is to open the file using Visual Studio’s Binary editor.

Right-click build.json, select “Open With…”, then choose the “Binary Editor” from the list. You should see something like this:

enter image description here

Select the first three bytes as in the screenshot, and delete them, then save the file. The parser should now accept the file, and signing will work as expected.

Answer:

I tried this approach as well following the same documentation. and getting exactly the same error.

After looking at the android build documentation, i.e the Signing Your App Manually

I realised that it should be possible to build the application in release mode (i.e an apk that is unsigned is generated 1st), so i removed build.json and tried this, and I got errors. I did a few google searches and came up this [post][2].

[2]: Error when running cordova build –release android In summary this my advice.

  1. Remove the build.json file.
  2. Clean the solution set it to debug mode and make sure you can a successful build.
  3. Clean the solution set it to release mode.
  4. Go to your application directory find platforms\android you’ll see build.gradle.
  5. Create a new file build-extras.gradle, i got this suggestion from one of the on the [post][2] [2]: Error when running cordova build –release android you don’t want to be editing the auto generated build.gradle file.

  6. In your build-extras.gradle put android { lintOptions {
    disable MissingTranslation
    } }
  7. Now go and build your solution, it should work and you’ll get an unsigned apk i.e android-release-unsigned.apk in your bin\Android\release folder.
    1. Sign your apk manually by following android build – signing your app manually steps

Answer:

Additionally to the reason @dlev posted I found another reason that could cause this:

I was using a path containing folder names foo\bar. This worked before, but it seems now (I have no idea if node, cordova or whatever got updated) this doesn’t work any more and you have to escape the directory seperator: foo\\bar.

After adding this, I can build my project again.

Answer:

Another hack is to leverage android’s build system. In platforms\android put your keystore file and inside your build-extras.gradle. the following

 android {
     lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
    signingConfigs {
         release {
                keyAlias = ""
                keyPassword = "yourpassword" // And these must be set to non-empty in order to have the signing step added to the task graph.
                storeFile = file("yourfile.keystore")
                storePassword = "yourpassword"
            }
        }
        buildTypes {
            release {
                signingConfig signingConfigs.release
            }
        }
}

If you want to create the illusion of full automation then you could use a gulp task “a pre-build step” to copy your keystore file and build-extras gradle file to platforms\android from wherever you have them.