Cannot build signed android package through visual studio cordova - android

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?

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:
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.

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.
Remove the build.json file.
Clean the solution set it to debug mode and make sure you can a successful build.
Clean the solution set it to release mode.
Go to your application directory find platforms\android you'll see build.gradle.
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.
In your build-extras.gradle put android { lintOptions {
disable MissingTranslation
} }
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.
Sign your apk manually by following android build - signing your app manually steps

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.

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.

Related

Cannot find apk architecture when run command "ionic cordova run android"

When I run command
ionic cordova run android
build is succeeded but app cannot be run on my android device.
The error log is as:
BUILD SUCCESSFUL in 4m 7s
47 actionable tasks: 45 executed, 2 up-to-date
Built the following apk(s):
ANDROID_HOME=C:\Users\AppData\Local\Android\sdk
JAVA_HOME=C:\Program Files\java\jdk1.8.0_121
No target specified, deploying to device 'UGM0217321003049'.
Skipping build...
Built the following apk(s):
Error: Could not find apk architecture: arm build-type: debug
[ERROR] An error occurred while running subprocess cordova.
cordova run android exited with exit code 1.
Re-running this command with the --verbose flag may provide more information.
Under the directory platforms/android/build/outputs/apk, there is only one folder called debug under which there are two file android-debug.apk and output.json.
apk
|---debug
|---android-debug.apk
|---output.json
I suspect there should be some arm architecture file in apk directory, but I don't know how to generate it.
Can you help me to solve this run android error?
To avoid extended discussions in comments, let me summarise the workaround as below:
Try to run below command to build the apk first.
cordova build android
or
ionic cordova build android
Look for the apk from platforms\android and manually install it.
How to automatically build the android-debug.apk in app folder?
the .json file is auto generated, usually you don't need to touch it.
By default, the apk path is under app/build/outputs/apk, you can copy it to a custom location wherever you prefer, for your case, maybe it is app/ root directory. You can do this with many means, e.g. bash scripts. Below gradle snippets is how I do this task, just copy below code to the end of your app/build.gradle:
def capitalizeFirstLetter(s) { s[0].toUpperCase() + s[1..-1] }
def publish = project.tasks.create("copyApks")
publish.description "Copies apks to custom directory"
android.applicationVariants.all { variant ->
def customPath
variant.outputs.all {
customPath = "$projectDir"
def capitalizedVariantName = capitalizeFirstLetter(variant.name)
def task = project.tasks.create("copy${capitalizedVariantName}Apk", Copy)
task.from(outputFile)
task.into(customPath)
task.dependsOn variant.assemble
publish.dependsOn task
}
}
From your command line, run ./gradlew copyApks or gradlew copyApks on macOS or Windows respectively.
Then you will find your apk is under app\ directory.

Node.js process with a non-zero exit code [duplicate]

I am new to cordova and am trying to create an android HelloWold project.
When I use cordova platforms add android, it throws some exception:
D:\CordovaSpace\helloWorld>cordova platforms add android
Adding android project...
Creating Cordova project for the Android platform:
Path: platforms\android
Package: com.example.helloworld
Name: HelloWorld
Activity: MainActivity
Android target: android-24
Subproject Path: CordovaLib
Android project created with cordova-android#6.0.0
Installing "cordova-plugin-whitelist" for android
ANDROID_HOME=D:\Java_Android_SDK\android_sdk
JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_73
Subproject Path: CordovaLib
Starting a new Gradle Daemon for this build (subsequent builds will be faster).
FAILURE: Build failed with an exception.
* What went wrong:
Unable to start the daemon process.
This problem might be caused by incorrect configuration of the daemon.
For example, an unrecognized jvm option is used.
Please refer to the user guide chapter on the daemon at https://docs.gradle.org/2.14.1/userguide/gradle_daemon.html
Please read the following process output to find out more:
-----------------------
Error occurred during initialization of VM
Could not reserve enough space for 2097152KB object heap
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Failed to install 'cordova-plugin-whitelist':Error: cmd: Command failed with exit code 1
at ChildProcess.whenDone (D:\CordovaSpace\helloWorld\platforms\android\cordova\node_modules\cordova-common\src\superspawn.js:169:23)
at emitTwo (events.js:106:13)
at ChildProcess.emit (events.js:191:7)
at maybeClose (internal/child_process.js:850:16)
at Process.ChildProcess._handle.onexit (internal/child_process.js:215:5)
Error: cmd: Command failed with exit code 1
Try again after increasing your Java VM(?) memory size.
Here is how to fix it on a Windows platform:
Go to Start -> Control Panel -> System -> Advanced(tab) -> Environment Variables -> System Variables -> New:
Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M
Don't ignore the score and underscore characters.
I had the exact same problem. I switched to use 64-bit JDK as suggested in here and it worked fine. I was using Ionic 3.5.0 on Windows 10.
this worked for my ionic app.
Change line on
// to allow dex in process
'org.gradle.jvmargs': '-Xmx1024m',
from
// to allow dex in process
'org.gradle.jvmargs': '-Xmx2048m',
in your
<app path>\platforms\android\cordova\lib\config
thanks
args.push('-Dorg.gradle.jvmargs=-Xmx2048m')
into
args.push('-Dorg.gradle.jvmargs=-Xmx1024m');
on the following location files.
project-folder\platforms\android\cordova\lib\builders\GradleBuilder.js
project-folder\platforms\android\cordova\lib\builders\StudioBuilder.js
Go to : \platforms\android\cordova\lib\config\GradlePropertiesParser.js
constructor (platformDir) {
this._defaults = {
// 10 seconds -> 6 seconds
'org.gradle.daemon': 'true',
// to allow dex in process
'org.gradle.jvmargs': '-Xmx2048m',
// allow NDK to be used - required by Gradle 1.5 plugin
'android.useDeprecatedNdk': 'true'
// Shaves another 100ms, but produces a "try at own risk" warning. Not worth it (yet):
// 'org.gradle.parallel': 'true'
};
And change the 'org.gradle.jvmargs': '-Xmx2048m', to 'org.gradle.jvmargs': '-Xmx1024m',
args.push('-Dorg.gradle.jvmargs=-Xmx2048m')
to
args.push('-Dorg.gradle.jvmargs=-Xmx1024m');
on the following location files.
project-folder\platforms\android\cordova\lib\builders\builders.js
project-folder\platforms\android\cordova\lib\builders\GradleBuilder.js
project-folder\platforms\android\cordova\lib\builders\StudioBuilder.js
Go to Start -> Control Panel -> System -> Advanced(tab) -> Environment Variables -> System Variables
Add/change JAVA_HOME value
from C:\Program Files (x86)\Java\jdk1.8.0_192
to C:\Program Files\Java\jdk1.8.0_192
That is, change from Program Files (x86) path to Program Files path.
I had a same problem.
Anyway, here is how to fix it:
Go to Start->Control Panel->System->Advanced(tab)->Environment Variables->System Variables->New:
Variable name: _JAVA_OPTIONS
Variable value: -Xmx512M
if 512 not work then change it to.
Variable value: -Xmx1024M
And also change the path.
Variable name: Path
Variable value: ;C:\Program Files\Java\jre6\bin;F:\JDK\bin;
Change this to your appropriate path.
Setting the environment variable at system level as in the #kemal's solution will set the memory size for all the JVM applications in the system, which is not a what most would want to do.
Instead, set the heap size in your cordova gradlebuilder property file.
args.push('-Dorg.gradle.jvmargs=-Xmx2048m');

Adding assets to Android Project, Visual Studio 2015 CTP

I am attempting to add assets to an android project, using Visual Studio 2015 CTP. When I add files to ProjectName.Packaging/res the build fails with the error:
1> -package-resources:
1> [aapt] Found Deleted Target File
1> [aapt] Creating full resource package...
1> [aapt] invalid resource directory name: C:\ProjectName\ARM\Release\Package\res image.png
1>
1> BUILD FAILED
1> C:\Program Files (x86)\Android\android-sdk\tools\ant\build.xml:932: The following error occurred while executing this line:
1> C:\Program Files (x86)\Android\android-sdk\tools\ant\build.xml:950: null returned: 1
1>
1> Total time: 0 seconds
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V140\AndroidPackaging\Android.Common.targets(280,5): error MSB3073: The command ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Apps\apache-ant-1.9.3\\bin\ant.bat" debug -Dout.final.file="C:\ProjectName\ARM\Release\ProjectName.Packaging.apk"" exited with code 1.
Please advise. Thanks.
Ok, I was able to answer my question. Right click ProjectName.Packages -> Add -> New Folder and name it assets. Right click assets -> Add -> Existing Item -> select the asset you wish to add.
If the error persists, clean project AND manually delete the build folder from C:\ProjectName\ARM\Release or whatever. Rebuild.
The file name of your resources can't contain spaces, use a _ instead

Unable to build Cordova project in Visual Studio

I'm using Visual Studio 2013 and the Cordova CTP 1.1. When I try to build the project, I get the following cryptic error (line breaks added for readability):
Error 22
The command ""C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\vs-cli"
prepare --platform Android --configuration Debug
--projectDir . --projectName "servermon"" exited with code 8.
C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda-targets\Microsoft.MDA.targets 115 5 servermon
I've tried the solutions from this question as well as this question, and this doesn't appear to solve the issue. My system path appears to have everything relevant in it:
C:\Program Files\Java\jdk1.7.0_60\bin
C:\Users\Nathan\AppData\Local\Android\android-sdk\tools
C:\Users\Nathan\AppData\Local\Android\android-sdk\platform-tools
C:\apache-ant-1.9.3\bin
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0\
C:\Program Files (x86)\Windows Kits\8.1\Windows Performance Toolkit\
C:\Program Files\Microsoft SQL Server\110\Tools\Binn\
C:\Program Files (x86)\Windows Live\Shared
C:\Program Files\Microsoft\Web Platform Installer\
C:\Program Files (x86)\nodejs\
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\
I've also opened up the SDK manager and made sure to get the latest (Rev 19) build tools. The system has been restarted more than once and I'm still not able to build. Does anyone have any ideas on how to fix this build issue?
Edit: Here is a screenshot of my SDK Manager
Edit2: Here is the build output
1>------ Build started: Project: servermon, Configuration: Debug Android ------
1>C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\TypeScript\Microsoft.TypeScript.targets(90,5): warning : The TypeScript Compiler was given no files for compilation, so it will skip compiling.
1> Your environment has been set up for using Node.js 0.10.26 (ia32) and npm.
1> ------ Ensuring correct global installation of package from source package directory: C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\Extensions\al3uox1v.u2n\packages\vs-mda
1> ------ Build settings:
1> ------ buildCommand: prepare
1> ------ platform: Android
1> ------ cordovaPlatform: android
1> ------ configuration: Debug
1> ------ cordovaConfiguration: Debug
1> ------ projectName: servermon
1> ------ projectSourceDir: C:\Users\Nathan\Documents\CordovaTest\cordovatest\cordovatest
1> ------ App dir C:\Users\Nathan\Documents\CordovaTest\cordovatest\cordovatest\bld\Debug already exists
1> ------ Copying app files to www
1> ------ Done copying app files to www
1> ------ Copying res files
1> ------ Done copying res files
1> ------ Platform android already exists
1> ------ Certificate copy from C:\Users\Nathan\Documents\CordovaTest\cordovatest\cordovatest\res\cert\android to platforms\android
1> ------ Done copying certificate files to platforms\android
1>
1> C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\q\q.js:126
1> throw e;
1> ^
1> CordovaError: The provided path "C:\Users\Nathan\Documents\CordovaTest\cordovatest\cordovatest\bld\Debug\platforms\android" is not an Android project.
1> at Object.android_parser (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\cordova\src\metadata\android_parser.js:37:15)
1> at C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\cordova\src\prepare.js:51:23
1> at Array.map (native)
1> at Object.prepare (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\cordova\src\prepare.js:49:35)
1> at preparePlatform (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\app.js:322:24)
1> at _fulfilled (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\q\q.js:787:54)
1> at self.promiseDispatch.done (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\q\q.js:816:30)
1> at Promise.promise.promiseDispatch (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\q\q.js:749:13)
1> at C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\q\q.js:557:44
1> at flush (C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\node_modules\q\q.js:108:17)
1>C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda-targets\Microsoft.MDA.targets(115,5): error MSB3073: The command ""C:\Users\Nathan\AppData\Roaming\npm\node_modules\vs-mda\vs-cli" prepare --platform Android --configuration Debug --projectDir . --projectName "servermon"" exited with code 8.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
It seems the Android template that was downloaded by Cordova is "corrupted".
To fix this:
Try deleting the cached android platform in your user directory:
C:\users\<username>\.cordova\android\*
Then, clean and build the Solution in Visual Studio. Or to be really sure, delete the ‘bld’ directory inside the project folder.
This should force Cordova to re-download the Android template and fix the issue.
for me I just deleted the bld folder and eveerything has become right
Some time visual studio catch creates problem, clear the cache.
Choose Tools, Options, Tools for Apache Cordova, and then choose
Cordova Tools.
Choose Clear Cordova Cache.
Close and re-open your project.
Choose Build, Clean Solution.

How to build apk using commandline

I Want to build apk using commandline, I am using ant server by using it I am building apk with commandline but when I give this command ant debug it shows error BUILD FAILED Unable to locate tools.jar and then it gives error source resource doesnt exist
This might be because your JAVA_HOME is not set to your C:\Program Files\Java\jdk folder.
instead it might be set to C:\Program Files\Java\jre so it's not getting tools.jar.
just check out this.
You can do this in this way:
C:\>set JAVA_HOME=C:\Program Files\Java\jdk
C:\>ant debug
You need tools.jar file from your jdk/lib/ folder.

Categories

Resources