Skip to content

Android Publish Example

In this section, we will introduce how to build Cocos Creator project with Android Studio.

Please prepare a Cocos Creator project with at least one scene.

project.png

Set Android Development Environment

To publish an Android native application, you need to install the Android Studio development environment, as well as specific versions of JDK (or OpenSDK), Android SDK, NDK, and more. For more details, please refer to the Android Native Development Environment Setup.

Publish Process

The next step is to create a new build task and publish an Android APK on the Build panel in Cocos Creator.

Create Build Task

  1. By clicking the Project -> Build menu to open Build panel.

    cc-build-menu.png

  2. At the top of Build panel, click on the New Build Task button

    new-build-task.pngP

  3. To select Android as the build target platform, click on the drop-down menu

    select-platform.png

  4. Make sure to have at least one scene designated as the Start Scene. If you have only one scene, it will be added by default

    start-scene.png

  5. Please refer to Publishing to Native Platforms - Render BackEnd to select an appropriate render backend

     ![render-backend.png](images/render-backend.png)
    
  6. Enter the Game Package Name

    game-package-name.png

    Please refer to Bundle Identifier for guidance your app's bundle identifier

  7. Select Target API Level

    target-api-level.png

    In case no dropdown content, please check the SDK and NDK configuration in Cocos Creator

Other build options please refer to Publishing to Native Platforms

Build and Publish

  1. To begin a build process, click on the Build button for the selected build task.

    build.png

  2. Please wait until the build process finished

    building.png

  3. Click on the button below to open the folder containing the exported Android Project

    open

  4. Locate the corresponding directory

    find-proj

  5. Click on the open menu on android studio

    android studio open project menu

  6. Navigate to the project path in the {Your project}/build directory, which is named 'android' by default, and open it using Android Studio.

    android studio open project

  7. Generate APK in Android Studio

    After the preparation has been completed by Android Studio, you can start building an Android APK. However, the sync process may take a long time to complete. If this happens, you can stop any ongoing background tasks by following the steps below:

    Click on the background task on the status bar: background-task.png

    Close all background tasks by clicking on the close button interrupt-sync.png

  8. Click on Build Bundle(s) / APK(s) in the Build menu

    build-apk.png

  9. The debug APK could be found in the {proj/build} directory

    apk.png

Keystore Generation and Usage

For the release of the final version, the debug key cannot be used. You need to create your own key.

Creating a Key

You can generate a key using Android Studio:

  1. In Android Studio, click on the "Build" menu and select "Generate Signed Bundle / APK": gen-sign-apk.png

  2. In the pop-up window, select "APK" and click "Next": gen-sign-apk.png

  3. In the guided window, click on "Create new": create-sign.png

  4. Fill in the required information in the pop-up window: android-keystore-panel.png

It is recommended to use different keys for different projects. You can store each project's key in the project's root directory.

Using the Key

After successfully creating the key, a key file will be generated in the selected directory. You can fill in the key information in the Android build panel. This way, both the debug and release versions will use the key you created.

As shown in the following image, uncheck the "Use debug keystore" option and select your custom key file from the "Keystore Path" below. Fill in the relevant information: debug-keystore.png

Considerations

  1. It is recommended to create a dedicated key for each project from the first release.
  2. Some SDK services require APK key signature verification. Using the default debug key may result in service call failures.

Advanced

Java and TypeScript Communication

The engine provides various methods to solve the communication issues between TypeScript and the native layer.

When integrating common SDKs, we often need to perform login operations through the SDK's login interface, and then pass the results to the TypeScript layer for further processing in the game.

The engine provides three methods for communication between TypeScript and Android native layer.

Importing Third-Party Libraries

To publish your application to the app market, you usually need to integrate certain third-party SDKs. These SDKs are typically provided in JAR or AAR format. You can refer to Using Your Library in Other Projects to import the local library into your project.

Extending Build Process

Please refer to Extending Build Process to extend the publishing mechanism by the extension system.

Q&A

  • Q: How to debug publishing errors

    • A: You can debug publishing errors by opening the log.txt file using the log button

      show-log.png

  • Q: Missing LIB_EGL

    • A: Change your NDK to the version mentioned above.
  • Q: Asset name mechanism

    • A: Since Android is based on Linux, some file specifications of Linux still apply in Android, such as
        1. Android is case-sensitive, make sure the path is case-sensitive, otherwise, it will not load correctly
        1. Do not include spaces in the folder or file name