Search code examples
androidandroid-studiococos2d-xcocos2d-x-3.0

Unable to run cocos2dx game with admobs banner


I tried to implement an AdMob in the cocos2dx project via android studio and got the following message after the build succeeded. The app doesn't run. I've set the gradle, manifest file, AppActivity in android studio folder.

Starting: Intent { cmp=com.test.puzzle/com.google.android.gms.ads.AdActivity }
java.lang.SecurityException: Permission Denial: starting Intent { flg=0x10000000 cmp=com.sample.mygame/com.google.android.gms.ads.AdActivity } from null (pid=27372, uid=2000) not exported from uid 10466
    at android.os.Parcel.readException(Parcel.java:1472)
    at android.os.Parcel.readException(Parcel.java:1426)
    at android.app.ActivityManagerProxy.startActivityAsUser(ActivityManagerNative.java:2174)
    at com.android.commands.am.Am.runStart(Am.java:680)
    at com.android.commands.am.Am.onRun(Am.java:270)
    at com.android.internal.os.BaseCommand.run(BaseCommand.java:47)
    at com.android.commands.am.Am.main(Am.java:76)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:243)
    at dalvik.system.NativeStart.main(Native Method)

Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.test.puzzle"
    android:installLocation="auto">

    <uses-feature android:glEsVersion="0x00020000" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <application
        android:label="@string/app_name"
        android:allowBackup="true"
        android:icon="@mipmap/puzzle">

        <!-- Tell Cocos2dxActivity the name of our .so-->
        <meta-data android:name="android.app.lib_name"
                   android:value="MyGame" />

        <meta-data android:name="com.google.android.gms.version"
                   android:value="@integer/google_play_services_version"/>
        <activity android:name="com.google.android.gms.ads.AdActivity"
                  android:exported="true"
                  android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

        <activity
            android:name="org.cocos2dx.cpp.AppActivity"
            android:screenOrientation="portrait"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

AppActivity

package org.cocos2dx.cpp;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.FrameLayout;

import com.google.android.gms.ads.*;

public class AppActivity extends Cocos2dxActivity {

    private AdView _adView;
    private final String AD_UNIT_ID = "xxx";  

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        FrameLayout.LayoutParams adParams = new FrameLayout.LayoutParams(
                FrameLayout.LayoutParams.WRAP_CONTENT,
                FrameLayout.LayoutParams.WRAP_CONTENT);
        adParams.gravity = (Gravity.TOP | Gravity.CENTER); 

        _adView = new AdView(this);
        _adView.setAdSize(AdSize.BANNER);
        _adView.setAdUnitId(AD_UNIT_ID);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)    
                .addTestDevice("Test Device ID")                
                .build();

        _adView.loadAd(adRequest);
        _adView.setBackgroundColor(Color.TRANSPARENT);
        addContentView(_adView, adParams);

    }

    @Override
    protected void onPause() {
        if (_adView != null) {
            _adView.pause();
        }
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (_adView != null) {
            _adView.resume();
        }
    }

    @Override
    protected void onDestroy() {
        _adView.destroy();
        super.onDestroy();
    }

Solution

  • set android:exported="true" in your AndroidManifest.xml file of this Activity.

    <activity
     ----
    android:exported="true">
    
    </activity>