Using Cordova 9.0.0 with Apache NetBeans

This brief guide shows how to make a few tweaks to fix some issues with Apache Cordova 9.0.0 projects in NetBeans IDE.

Netbeans Cordova Logo

Setup Cordova, NodeJS and Gradle

First, your system should be pre-configured with the Cordova, Node and Gradle shown below. This guide does not focus on how to install those components, these are relatively simple tasks.

Cordova, Node and Gradle versions

Cordova, Node and Gradle versions

Manage plugins with command line rather than NetBeans

The following dialog was designed to add standard plugins for your Cordova app from NetBeans, but you must not use it for the reason outlined below.

Using this dialog to add plugins will cause your build to fail!
Do not use the NetBeans Cordova Plugins manager

Avoid using the NetBeans Cordova Plugins manager

Adding those default plugins will insert them to the plugin.properties file with an invalid source URL, therefore your build will fail. Sample URL used for device plugin:

cordova-plugin-device=https://git-wip-us.apache.org/repos/asf/cordova-plugin-device.git

Disable NetBeans’ plugin.properties file to install Cordova plugins manually

On build time, plugins added via command line will be removed if not found in the plugin.properties file. Read on to learn how to prevent this!

To switch to the command line-only plugin management method, follow these steps:

  • Open the nbproject/build.xml and replace this:
<target name="update-plugins">
    <plugintask/>
</target>
  • With this:
<target name="update-plugins">
    <echo level="info" message="Ignoring the update-plugins section, please manage plugins from the command line" />
</target>

Save the file, and now whenever you build your app from NetBeans, it will neither install nor delete plugins. From now on, you must add/remove plugins with the cordova plugin add and cordova plugin rm command.

Cordova plugins from NetBeans npm section

Cordova plugins from NetBeans npm section

You can still see that installed plugins from the npm Libraries item, and view their versions by accessing the npm Libraries Properties dialog.

Fix the Clean and Build Project buttons

NetBeans IDE Toolbar

NetBeans IDE Toolbar

Whenever you try to build or clean your initial Apache Cordova app in NetBeans, you may receive the following error:

D:\Cordova\HTML5Application\nbproject\build.xml:285: Execute failed: 
java.io.IOException: Cannot run program ".\gradlew" (in directory "D:\Cordova\HTML5Application\platforms\android"): CreateProcess error=2, The system cannot find the file specified

To fix this error, open the nbproject/build.xml file and replace:

<target name="clean-android" depends="check-android-template" if="android.generated.exists">
    <exec executable="./gradlew" dir="platforms/android">
        <arg value="clean" />
    </exec>
</target>

with:

<target name="clean-android">
    <echo level="info" message="cordova.cmd clean"/>
    <exec executable="cordova.cmd" resolveexecutable="true" searchpath="true" failonerror="true">
        <env key="${cordova.path.key}" path="${cordova.path.value}:${android.sdk.home}/tools:${android.sdk.home}/platform-tools:${jdk.home}/bin:${ant.home}/bin:${jdk.home}/bin"/>
        <env key="JAVA_HOME" path="${jdk.home}"/>
        <arg value="clean"/>
    </exec>
</target>

Save and close this file.

Now you can use the Clean and Build Project and Clean Project toolbar buttons normally, and the project will be cleaned just like when you type cordova clean in the command line.

NetBeans Cordova Clean command output

NetBeans Cordova Clean command output

Please remember that this Build button generates a debug (unsigned) APK. To create a signed APK, use the build command from the command line like this:

cordova build android --release

Set target Android SDK to 29

Open config.xml and add these lines or edit accordingly:

<preference name="android-minSdkVersion" value="19" />
<preference name="android-targetSdkVersion" value="29" />

By setting minSdkVersion to 19 it means your app requires at least Android 4.4. Check the Android Codenames, Tags, and Build Numbers table for more information.

Android build numbers and codenames

Android build numbers and codenames

Open platforms/android/build.gradle and make sure your repositories and build tools versions and targets are set as follows:

buildscript {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    dependencies {
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.android.tools.build:gradle:3.4.3'
    }
}
allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
    }
    //This replaces project.properties w.r.t. build settings
    project.ext {
      defaultBuildToolsVersion="29.0.3" //String
      defaultMinSdkVersion=19 //Integer - Minimum requirement is Android 4.4
      defaultTargetSdkVersion=29 //Integer - We ALWAYS target the latest by default
      defaultCompileSdkVersion=29 //Integer - We ALWAYS compile with the latest by default
    }
}
task clean(type: Delete) {
    delete rootProject.buildDir
}

How to debug your Cordova Android app