Using Cordova 9.0.0 with NetBeans 8.2

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

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

Cordova plugins installed with the CLI will be removed by NetBeans on build time unless they have been added to the nbproject/plugin.properties file beforehand. The following dialog allows managing the standard plugins, but you must not use it:

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, such as this:

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

On build time, this will fail because the source URL is non existant. Probably there’s a NetBeans setting buried somewhere that allows changing this URL, however I was unable to find it.

Your options are to either:

A: Manually add them to plugin.properties with the correct URL, such as:

cordova-plugin-device=https://github.com/apache/cordova-plugin-device

B: Manage plugins externally from the command line: (recommended)

cordova plugin add cordova-plugin-device

However, when using the external command line, the plugins you add will be removed on build time unless you have also added them to the plugin.properties file.

To prevent this, and switch to the command line-only plugin management method (forgetting about the plugin.properties file) 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 commands.

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
}

All product names, logos, and brands in this website are property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement.