Native support
1. iOS Native support
When you build the iOS project for the first time, project code is generated in the ./build/ios/ and ./native/engine/ios/ directories. Then configure the iOS project.
1.1 Manual configuration
-
Add the iOS project dependency files
CocosCreatorProxyApi.hCocosCreatorProxyApi.mmThinkingSDK.frameworkThinkingDataCore.framework
-
Configure Build Phases
- In
Link Binary With Libraries, addThinkingSDK.framework
- In
-
Configure Build Settings
- In Framework Search Paths, add the reference path of
ThinkingSDK.framework, such as"$(SRCROOT)/../Classes" - In
Other Linker Flags, add-ObjC
- In Framework Search Paths, add the reference path of
1.2 CMakeList configuration
The path of the CMakeList file is ./native/engine/ios/CMakeLists.txt.
The CMakeList file is generated after you build the iOS project for the first time. After you configure it, the configuration takes effect the next time you build.
First copy the iOS resource files to the corresponding folder, such as ./native/engine/common/Classes/ThinkingAnalytics/ios/, and then enter that path when you configure CMakeList.
- Add the iOS project dependency files
# Find this line of code
set(PROJ_COMMON_SOURCES
${CMAKE_CURRENT_LIST_DIR}/../common/Classes/Game.h
${CMAKE_CURRENT_LIST_DIR}/../common/Classes/Game.cpp
# Add the following code
${CMAKE_CURRENT_LIST_DIR}/../common/Classes/ThinkingAnalytics/ios/CocosCreatorProxyApi.h
${CMAKE_CURRENT_LIST_DIR}/../common/Classes/ThinkingAnalytics/ios/CocosCreatorProxyApi.mm
${CMAKE_CURRENT_LIST_DIR}/../common/Classes/ThinkingAnalytics/ios/ThinkingSDK.framework
${CMAKE_CURRENT_LIST_DIR}/../common/Classes/ThinkingAnalytics/ios/ThinkingDataCore.framework
)
- Configure Build Phases
# Find this line of code
target_link_libraries(${LIB_NAME} cocos2d)
# Add the following code
target_link_libraries(${LIB_NAME} ${CMAKE_CURRENT_LIST_DIR}/../common/Classes/ThinkingAnalytics/ios/ThinkingSDK.framework)
target_link_libraries(${LIB_NAME} ${CMAKE_CURRENT_LIST_DIR}/../common/Classes/ThinkingAnalytics/ios/ThinkingDataCore.framework)
- Configure Build Settings
# Find this line of code
set(PRODUCT_NAME ${APP_NAME})
# Add the following code
set(CMAKE_EXE_LINKER_FLAGS -ObjC)
The Cocos Creator v2.x editor does not support CMake configuration yet. Configure the project manually.
2. Android Native support
Build the Android project, and then configure it.
- In the app project in the Android view, add the dependency file
CocosCreatorProxyApi.javatocom.cocos.game, as shown below
- Add the obfuscation rule to the proguard-rules.pro file in the project's app folder
-keep public class com.cocos.game.CocosCreatorProxyApi {*;}
- Create a libs directory in the project's app folder, and copy
TDAnalytics.aarandTDCore.aarinto it - Add the dependencies to the
build.gradlefile underModule
dependencies {
...
implementation fileTree(dir: 'libs', include: ['*.aar'])
}
3. OpenHarmony Native support
First, build the OpenHarmony project
- Put TDAnalytics.har and CocosCreatorProxyApi.ts in the following directory
- Import the SDK in the entry/oh-package.json5 file
"@thinkingdata/analytics": "file:./libs/TDAnalytics.har"
- Import the bridge file in entry/build-profile.json5
arkOptions: {
runtimeOnly: {
sources: [
'./src/main/ets/CocosCreatorProxyApi.ts',
],
},
}
- Pass in context
The current CocosCreator release is the first version that supports HarmonyOS. Interaction runs on the worker thread, and there is no official API for getting context yet. Until an official update is available, you can add it manually
Add the following to aboutToAppear() in entry/src/main/ets/pages/index.ets:
aboutToAppear(): void {
console.log('[LIFECYCLE-Index] cocos aboutToAppear');
this.workPort.postMessage('context', getContext(this));
...
}
Add a context branch to the message switch in entry/src/main/ets/workers/cocos_worker.ts:
switch (msg.name) {
...
case "context":
globalThis.appContext = msg.param
break;
default:
console.error("cocos worker: message type unknown");
break;
}
The steps above pass context to the worker thread, where it is needed when the SDK is initialized
In initWithConfig in CocosCreatorProxyApi.ts, pass in globalThis.appContext during initialization:
function initWithConfig(param: string): void {
try {
let obj = JSON.parse(param)
if (obj['enableLog'] === true) {
TDAnalytics.enableLog(true)
}
let config = new TDConfig()
config.appId = obj['appId']
config.serverUrl = obj['serverUrl']
TDAnalytics.initWithConfig(globalThis.appContext, config)
} catch (e) {
}
}
If you have another way to get context, you can also modify the initWithConfig method in the CocosCreatorProxyApi.ts file yourself
4. Enable Native support
When you initialize the SDK, add enableNative: true to config to enable Native support.
// TA SDK configuration object
var config = {
appId: "YOUR_APPID", // Project APP ID
serverUrl: "YOUR_SERVER_URL", // Reporting URL
enableNative: true,// Allow calling Native code
autoTrack: {
appShow: true, // Auto-track the start event
appHide: true, // Auto-track the close event
appCrash: true, // Auto-track the crash event (native only)
appInstall: true // Auto-track the install event (native only)
}
};
// Initialize
TDAnalytics.init(config);

