One, foreword

At present, many apps on the market have the function of game center. At the earliest, there were wechat mini games and QQ mini games. Later, bilibili, Ximalaya, iqiyi, Bixin and other applications also added the game center module. This article will show you how to build a Cocos Creater container in the first place.

Two, preparation work

  1. Download and install the latest CocosDashboard
  2. Download the latest version of the editor from Dashborad

  1. Install the NDK in Android Studio. My version is 21.1.6352462, which is stable so far

  1. Create a new HelloWorld project in CocosDashboard and run it. I’m using version 3.1.1 here

  1. Open the CocosCreator menu bar Preferences and set the Android NDK and Android SDK paths in the external app bar

3. Build the cocos game.so file

  1. Go to the CocosCreator menu and select Project – Build release, select the release platform: Android, hit Build, and wait for about a few minutes

  1. After success, use Android Studio to open the generated ProJ project in the folder and run the project on the mobile phone. Here, the game resources are loaded in the directory assets, which is the same as proj. Later, we will store the assets compressed package ZIP in our server, so that users can load and start the game after downloading and decompressing.

  2. The Java_com_cocos_lib_CocosActivity_onCreateNative method in jnicocosActivity. CPP needs to be modified so that the game container can load the game resources in the local filePath

  1. ./gradlew assembleRelease/release/instantapp-release.apk/zip Libcocos. So under arm64-v8a/armeabi-v7a

Create your own game container

  1. Create a module whose package name is com.cocos.lib (to be consistent with the.so file, otherwise you can’t call c methods)

  2. Module manifest file plus

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
Copy the code
  1. Will/Applications/CocosCreator/Creator / 3.1.1 / CocosCreator app/Contents/Resources/Resources / 3 d/engine – native/cocos/platform / Copy Android/Java /libs to module/libs

  2. Module under build.gradle

implementation fileTree(include: ['*.jar'], dir: 'libs')
Copy the code
  1. Put the. So file under module/ SRC /main/jniLibs/
  2. Will/Applications/CocosCreator/Creator / 3.1.1 / CocosCreator app/Contents/Resources/Resources / 3 d/engine – native/cocos/platform / Android/Java/SRC/com/cocos Java file is copied to the module under the/lib/SRC/main/Java/com. The cocos. Under the lib
  3. Modify the file cocosActives.java. Because the official recommendation of the game page is to use multiple processes to do it, so quit the game here and kill the game process
// Add a filePath parameter
private native void onCreateNative(Activity activity, AssetManager assetManager, String obbPath, int sdkVersion, String filePath);

// External incoming game resource path
protected String filePath(a) {
    return "";
}

@Override
protected void onCreate(Bundle savedInstanceState) {... onCreateNative(this, getAssets(), getAbsolutePath(getObbDir()), Build.VERSION.SDK_INT, filePath());
}

@Override
public void onBackPressed(a) {
    super.onBackPressed();
    System.exit(0);
}
Copy the code

Five, the summary

Since then, our game container has been created, and I have also sent the game container module of this article to the Jitpack for direct use:

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
}
Copy the code
Dependencies {implementation 'com. Making. Qq326646683: cocos - creator - android: 1.0.0'}Copy the code

How to use it

  1. File read, write, and network permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />
Copy the code
  1. Download the game zip and unzip it
  2. Inherit CocosActivity and assign the decompressed path to filePath
class CocosGameActivity: CocosActivity() {
    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
    }

    override fun filePath(a) = intent.getStringExtra("path")}Copy the code
  1. The manifest file
 <application>
        <meta-data
            android:name="android.app.lib_name"
            android:value="cocos" />
        <activity android:name=".CocosGameActivity" android:process=":cocos"/>
Copy the code
  1. The module and sample app code for this article are in GitLab

Vii. Follow-up plan

Cocos games communicate with Android, because it involves multiple processes, communication becomes troublesome. In the future, we plan to encapsulates this part of content in module Library, which is convenient for users to call


End, scatter flowers 🎉