- Home /
App stops working as soon it starts.
Hi, I wrote my own plugin for Google Admob and Analytics, but as soon as the app starts, it closes.
This is the plugin code:
package com.demilp.plugins;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.FrameLayout.LayoutParams;
import android.widget.LinearLayout;
import com.google.android.gms.ads.*;
import com.google.android.gms.analytics.GoogleAnalytics;
import com.google.android.gms.analytics.HitBuilders;
import com.google.android.gms.analytics.Tracker;
import com.unity3d.player.UnityPlayerNativeActivity;
public class Plugins extends UnityPlayerNativeActivity {
//Variables de Analytics
private Tracker tracker;
//Variables de AdMob
private AdView adView;
private InterstitialAd interstitial;
//Mi Nexus 5 ID 04911cc734379b2b
//Metodos Compartidos
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(this).reportActivityStart(this);
}
@Override
protected void onResume() {
super.onResume();
if (adView != null) {
adView.resume();
}
}
@Override
protected void onPause() {
if (adView != null) {
adView.pause();
}
super.onPause();
}
@Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(this).reportActivityStop(this);
}
@Override
protected void onDestroy() {
DestroyAdBanner();
super.onDestroy();
}
//Metodos de Analytics
public void InitAnalytics(String propertyID)
{
if(tracker == null)
{
GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
tracker = analytics.newTracker(propertyID);
}
}
public void SendScreen(String screenName)
{
tracker.setScreenName(screenName);
tracker.send(new HitBuilders.AppViewBuilder().build());
}
public void SendEvent(String category, String action, String label, long value)
{
tracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.setValue(value)
.build());
}
public void SendEvent(String category, String action, String label)
{
tracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.setLabel(label)
.build());
}
public void SendEvent(String category, String action)
{
tracker.send(new HitBuilders.EventBuilder()
.setCategory(category)
.setAction(action)
.build());
}
public void SendTiming(String category, long value, String name, String label)
{
tracker.send(new HitBuilders.TimingBuilder()
.setCategory(category)
.setValue(value)
.setVariable(name)
.setLabel(label)
.build());
}
public void SendTiming(String category, long value, String name)
{
tracker.send(new HitBuilders.TimingBuilder()
.setCategory(category)
.setValue(value)
.setVariable(name)
.build());
}
public void SendTiming(String category, long value)
{
tracker.send(new HitBuilders.TimingBuilder()
.setCategory(category)
.setValue(value)
.build());
}
//Metodos de AdMob
public void CreateAdBanner(String adUnitID, int adSize, String testDeviceID, boolean isTesting){
// Create an ad.
//adView.setX([float x]);
//adView.sety([float y]);
adView = new AdView(this);
switch (adSize){
case 0:
adView.setAdSize(AdSize.BANNER);
break;
case 1:
adView.setAdSize(AdSize.FULL_BANNER);
break;
case 2:
adView.setAdSize(AdSize.LEADERBOARD);
break;
case 3:
adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
break;
case 4:
adView.setAdSize(AdSize.SMART_BANNER);
break;
case 5:
adView.setAdSize(AdSize.WIDE_SKYSCRAPER);
break;
default:
adView.setAdSize(AdSize.SMART_BANNER);
break;
}
adView.setAdUnitId(adUnitID);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest;
if(isTesting){
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(testDeviceID)
.build();
}else{
adRequest = new AdRequest.Builder().build();
}
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
public void ShowAdBanner(int gravity){
if(adView != null){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
switch(gravity){
case 0:
layout.setGravity(Gravity.NO_GRAVITY);
break;
case 1:
layout.setGravity(Gravity.CENTER_HORIZONTAL);
break;
case 2:
layout.setGravity(Gravity.LEFT);
break;
case 3:
layout.setGravity(Gravity.RIGHT);
break;
case 4:
layout.setGravity(Gravity.FILL_HORIZONTAL);
break;
case 5:
layout.setGravity(Gravity.CENTER_VERTICAL);
break;
case 6:
layout.setGravity(Gravity.CENTER);
break;
case 7:
layout.setGravity(Gravity.TOP);
break;
case 8:
layout.setGravity(Gravity.BOTTOM);
break;
case 9:
layout.setGravity(Gravity.FILL_VERTICAL);
break;
}
if(layout.indexOfChild(adView) >= 0){
adView.setLayoutParams(params);
layout.addView(adView);
}
}
}
public void HideAdBanner(){
if(adView != null){
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.removeView(adView);
}
}
public void DestroyAdBanner(){
if (adView != null) {
HideAdBanner();
adView.destroy();
}
}
public void CreateAdInterstitial(String adUnitID, String testDeviceID){
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(adUnitID);
// Create ad request.
AdRequest.Builder adRequestBuilder = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("testDeviceID");
//Add listener
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
ShowAdInterstitial();
}
});
// Begin loading your interstitial.
interstitial.loadAd(adRequestBuilder.build());
}
private void ShowAdInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
I replace the activity in the manifest with com.demilp.plugins.Plugins and copy google-play-services.jar to Assets/Plugins/Android. Am i missing something?
Are you also placing the compiled JAR version of your activity under Assets/Plugins/Android ? Also, please attach the logcat output of the game crashing when you test it (on the device or emulator, whichever you used).
Yes, i placed it in Assets/Plugins/Android. I'm trying to use other plugins I wrote, but it seems they are compiling wrongly. If you have any notion of compiling plugins, would you give it a try?
Your answer
Follow this Question
Related Questions
Where can I find Admob android plugin? 2 Answers
play sevices resolver not present in assets. 0 Answers
Hide/Show Admob View Android plugin 0 Answers
Editing Android Plugins 0 Answers
Using Ad plugins on Android leads to extra crashes 0 Answers