Tutorial: Cocos Creator | Android Environment Configuration for Creator 2.x

Cocos Creator | Android Environment Configuration for Creator 2.x

Official documentation

macOS

Installation

  1. Install the Oracle JDK

    1. Visit Oracle’s Download site.

    2. Select macOS x64 and download.
      Picture1

    3. Install
      Picture2
      Picture3

    4. Testing
      Test for successful installation by entering the following command in the terminal:

      java -version
      

      If the following message shows, the installation is successful:
      Picture4

  2. Installing Android Studio

    1. Download Android Studio

    2. Install Android Studio
      Picture6
      Picture7

    3. Run Android Studio - Select whether to import the configuration as needed:
      Picture8

      If the following prompt appears, click Cancel:
      Picture9

  3. Install SDK and NDK

    1. Open the SDK Manager in the welcome panel:
      Picture10
      Later on, it is also open the SDK Manager from the menu bar or toolbar and download the required version of the SDK or NDK.

      Menu bar option:
      Picture11

      Toolbar option:
      Picture12

    2. Install the SDK
      Picture13

    3. Install the NDK
      The NDK needs to be selected according to the Cocos Creator version, the official recommended version is r17 - r19.

      If the NDK version is too high and the Cocos Creator version does not support it, compilation errors will occur.
      Picture14

Configuring Cocos Creator

Once the SDK and NDK are downloaded, configure the native development environment of Cocos Creator.

Pay special attention to the <NDK path> and select the root directory of the NDK:

Picture15

Building from Cocos Creator

Open the build panel from within Cocos Creator and set the relevant parameters:

Picture16

For more information about the parameters, please refer to the Publish Native documentation.

Start the build:

Picture17

Running the project

After the build is finished, open the project’s Android project (i.e. the proj.android-studio directory) with Android Studio. Android Studio will automatically download the required Gradle and other dependent libraries:

Picture18

Configuring Gradle

Generally speaking, the default Gradle is the one that matches after the Creator build, there is no need to modify it manually.

Cocos Creator 2.4.x supports Android Gradle version 4.10.x, which corresponds to Gradle Tool version 3.x (v3.2.0 is recommended). If prompted to upgrade the Android Gradle extension when compiling with Android Studio, please do not upgrade.

If the version of Gradle or Gradle Tool is too recent, it will cause an error.

Gradle Tool:

Picture19

Gradle:

Picture20

After downloading, select the project to run (which is the name of the game we filled in the build panel of Cocos Creator) (instantapp will package the game in App Bundle format for uploading to the Google Play store):

Picture22

After clicking Run, the first run will compile the Cocos source code, so please be patient:

Picture22

Once the build is complete, the game is ready to run:

Picture23

Windows

Installation

  1. Install the Oracle JDK

    1. Visit Oracle’s Download site.

    2. Select Windows x64 and download.
      jdk01

    3. Choose the installation directory for jdk:
      jdk02

      After the JDK installation is complete, a JRE installation prompt will pop up, continue to click Change to modify the installation location, the same as the JDK installation process:

      jdk03

      After the installation is complete, the directory structure is as follows:

      jdk04

    4. Testing
      Test for successful installation by entering the following command in the terminal:

      java -version
      

      If the following message shows, the installation is successful:
      jdk05

  2. Setting Environment Variables

    1. Create a new system variable:

      Variable name: JAVA_HOME
      Variable value: D:\Java\jdk1.8.0_241

      variable01

    2. Edit environment variable

      Variable name: Path
      New: %JAVA_HOME%\bin

      variable02

    3. Create a new system variable

      Variable name: CLASSPATH
      Variable value: .;%Java_Home%\bin;%Java_Home%\lib\dt.jar;%Java_Home%\lib\tools.jar

      variable03

    4. Restart

      After modifying the environment variables, restart your computer to take effect.

  3. Installing Android Studio

    1. Download Android Studio

    2. Install Android Studio
      android01
      android02

    3. Run Android Studio - Select whether to import the configuration as needed:
      android03

      If the following prompt appears, click Cancel:
      android04

    4. Choose standard or custom installation:

      android05
      android06
      android07
      android08

    5. Completed

      android09

  4. Install SDK and NDK

    1. Open the SDK Manager in the welcome panel:
      sdk01
      Later on, it is also open the SDK Manager from the menu bar or toolbar and download the required version of the SDK or NDK.

      sdk02
      sdk03

    2. Install the SDK
      sdk04

    3. Install the NDK
      The NDK needs to be selected according to the Cocos Creator version, the official recommended version is r17 - r19.

      If the NDK version is too high and the Cocos Creator version does not support it, compilation errors will occur.
      sdk05
      sdk06

Configuring Cocos Creator

Once the SDK and NDK are downloaded, configure the native development environment of Cocos Creator.

Pay special attention to the <NDK path> and select the root directory of the NDK:

Picture15

Building from Cocos Creator

Open the build panel from within Cocos Creator and set the relevant parameters:

Picture16

For more information about the parameters, please refer to the Publish Native documentation.

Start the build:

Picture17

Running the project

After the build is finished, open the project’s Android project (i.e. the proj.android-studio directory) with Android Studio. Android Studio will automatically download the required Gradle and other dependent libraries:

Picture18

Configuring Gradle

Generally speaking, the default Gradle is the one that matches after the Creator build, there is no need to modify it manually.

Cocos Creator 2.4.x supports Android Gradle version 4.10.x, which corresponds to Gradle Tool version 3.x (v3.2.0 is recommended). If prompted to upgrade the Android Gradle extension when compiling with Android Studio, please do not upgrade.

If the version of Gradle or Gradle Tool is too recent, it will cause an error.

Gradle Tool:

Picture19

Gradle:

Picture20

After downloading, select the project to run (which is the name of the game we filled in the build panel of Cocos Creator) (instantapp will package the game in App Bundle format for uploading to the Google Play store):

Picture22

After clicking Run, the first run will compile the Cocos source code, so please be patient:

Picture22

Once the build is complete, the game is ready to run:

Picture23