How to launch another Android app with native C++ code?


Hi all, I’m making a game with cocos2d-x for Android using C++. Now I’m looking for a way to open another Android App (likes YouTube, Google Play Store, …) using their package name from inside my game through a button . I have searched around and found that it could be done in Java code with something like this:

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
if (launchIntent != null) { 
    startActivity(launchIntent);//null pointer check in case package name was not found

My question is: Is it possible to open another Android App in my native code (.cpp files) or I have to put them in java side (.java files)? If I have to do it in .java files, where should I put it? I always work with .cpp files in Visual Studio, compile with cmd and run with emulator on Android Studio, I have never worked with .java files that generated by cocos2d-x in Android Studio, the engine just makes everything ready for me so I got a little confused here.
Your attention and help is very much appreciated.


Yes you can. Use JNI.
See this blog.


You can use


and the Url you pass in can be a system url if you google Android URL schemes it should put you in the right direction


you can use JNI for this …
if you are using android-studio add this to in
and if you’re using eclipse add this to

Your new :

package org.cocos2dx.cpp;

import android.content.Intent;
import android.os.Bundle;

public class AppActivity extends Cocos2dxActivity {

    public static Activity _activity;

    public void onCreate(Bundle savedInstanceState){
        _activity = this;
    public static void openAnotherApp(){
    	Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
	if (launchIntent != null) { 
	    startActivity(launchIntent);//null pointer check in case package name was not found

and then in your Scene or another class just call this java method like this :
Cpp class :

// C and C++ headers

// cocos2d-x headers
#include "platform/android/jni/JniHelper.h"


Scene* GameScene::createScene()
    // 'scene' is an autorelease object
    auto scene = Scene::create();
    // 'layer' is an autorelease object
    auto layer = GameScene::create();

    // add layer as a child to scene

    // return the scene
    return scene;

// implementation of GameScene



GameScene* GameScene::create()
    GameScene* ret = new (std::nothrow) GameScene();
    if (ret && ret->init())
    return ret;

// on "init" you need to initialize your instance
bool GameScene::init()
    // 1. super init first
    if ( !Layer::init() )
        return false;

    cocos2d::JniMethodInfo methodInfo;
    if(!cocos2d::JniHelper::getStaticMethodInfo(methodInfo , "org/cocos2dx/cpp/AppActivity" , "openAnotherApp" , "()V")){

    methodInfo.env->CallStaticVoidMethod(methodInfo.classID , methodInfo.methodID);

    return true;

i hope this can help you with your problem.


Hi @michio, thanks for your help, it works like a charm. However, I’m running into another problem that my Game crashed.

My expect: Press the button => pause my game and open You Tube on Android => Press Back Button => pause You Tube and resume my game.
What happens: I could open You Tube but my App crashed when I press back button. I got the following error code from Android Studio:

A/libc: Fatal signal 11 (SIGSEGV) at 0x0004fb18 (code=1), thread 1975 (Thread-55)

I’m trying to performs some actions before opening others app and they might cause the crash because my Game runs without any problem if I remove those actions and leave openApp function alone.

My code :

   auto imageOpeningAction = CallFunc::create([&]() {
   auto imageClosingAction = CallFunc::create([&]() {
   auto openAnotherApp = CallFunc::create([&]() {  // Open YouTube App
runAction(Sequence::create(imageOpeningAction->clone(), DelayTime::create(0.5f), openAnotherApp->clone(), nullptr));

Any idea how to fix it?


Hi @sinhviencodon , your welcome buddy.
your code seems fine , but if i recall correctly there is a problem with Sequence in android platform. i’m not sure about that though. check it out. and what is your cocos2dx version and android studio version?


@michio, I’m using cocos2d-x 3.14.1 downloaded from the home page and Android Studio 2.2.3. You have any idea how should I workaround this ?