Debuggable上的Libgdx Android Blackscreen错误

时间:2017-09-02 19:57:07

标签: android libgdx

我的Android应用程序出了问题。 我在Libgdx中使用Google服务和Admob创建了一个应用程序。 如果我将debuggable设置为false,则应用程序只显示黑屏和我手机上的广告(Android 6.0.1)......但是如果我将debuggable设置为true,它就可以工作。 我用另外一部手机(Android 7.0)和模拟器对它进行了测试,效果很好。

Edit2:ok再次测试:它仅适用于模拟器 顺便说一句:即时通讯使用Android Studio

我尝试卸载应用程序/重新启动手机。

编辑:

这是我的代码:

build.gradle(android):

android {
signingConfigs {
    config {
        keyAlias '*****'
        keyPassword '*****'
        storeFile file('******')
        storePassword '******'
    }
}
buildToolsVersion '26.0.1'
compileSdkVersion 26
sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        aidl.srcDirs = ['src']
        renderscript.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
        jniLibs.srcDirs = ['libs']
    }

    instrumentTest.setRoot('tests')
}
packagingOptions {
    exclude 'META-INF/robovm/ios/robovm.xml'
}
defaultConfig {
    applicationId "*****"
    minSdkVersion 14
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    signingConfig signingConfigs.config
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        debuggable false
        signingConfig signingConfigs.config
    }
}
productFlavors {
}
}
task copyAndroidNatives() { 
file("libs/armeabi/").mkdirs();
file("libs/armeabi-v7a/").mkdirs();
file("libs/arm64-v8a/").mkdirs();
file("libs/x86_64/").mkdirs();
file("libs/x86/").mkdirs();

configurations.natives.files.each { jar ->
    def outputDir = null
    if(jar.name.endsWith("natives-arm64-v8a.jar")) outputDir = file("libs/arm64-v8a")
    if(jar.name.endsWith("natives-armeabi-v7a.jar")) outputDir = file("libs/armeabi-v7a")        
    if(jar.name.endsWith("natives-armeabi.jar")) outputDir = file("libs/armeabi")
    if(jar.name.endsWith("natives-x86_64.jar")) outputDir = file("libs/x86_64")
    if(jar.name.endsWith("natives-x86.jar")) outputDir = file("libs/x86")
    if(outputDir != null) {
        copy {
            from zipTree(jar)
            into outputDir
            include "*.so"
        }
    }
}
}

task run(type: Exec) {
def path
def localProperties = project.file("../local.properties")
if (localProperties.exists()) {
    Properties properties = new Properties()
    localProperties.withInputStream { instr ->
        properties.load(instr)
    }
    def sdkDir = properties.getProperty('sdk.dir')
    if (sdkDir) {
        path = sdkDir
    } else {
        path = "$System.env.ANDROID_HOME"
    }
} else {
    path = "$System.env.ANDROID_HOME"
}

def adb = path + "/platform-tools/adb"
commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.*******/AndroidLauncher'
}

// sets up the Android Eclipse project, using the old Ant based build.
eclipse {
// need to specify Java source sets explicitly, SpringSource Gradle Eclipse plugin
// ignores any nodes added in classpath.file.withXml
sourceSets {
    main {
        java.srcDirs "src", 'gen'
    }
}

jdt {
    sourceCompatibility = 1.6
    targetCompatibility = 1.6
}

classpath {
    plusConfigurations += [ project.configurations.compile ]        
    containers 'com.android.ide.eclipse.adt.ANDROID_FRAMEWORK', 'com.android.ide.eclipse.adt.LIBRARIES'       
}

project {
    name = appName + "-android"
    natures 'com.android.ide.eclipse.adt.AndroidNature'
    buildCommands.clear();
    buildCommand "com.android.ide.eclipse.adt.ResourceManagerBuilder"
    buildCommand "com.android.ide.eclipse.adt.PreCompilerBuilder"
    buildCommand "org.eclipse.jdt.core.javabuilder"
    buildCommand "com.android.ide.eclipse.adt.ApkBuilder"
}
}

// sets up the Android Idea project, using the old Ant based build.
idea {
module {
    sourceDirs += file("src");
    scopes = [ COMPILE: [plus:[project.configurations.compile]]]        

    iml {
        withXml {
            def node = it.asNode()
            def builder = NodeBuilder.newInstance();
            builder.current = node;
            builder.component(name: "FacetManager") {
                facet(type: "android", name: "Android") {
                    configuration {
                        option(name: "UPDATE_PROPERTY_FILES", value:"true")
                    }
                }
            }
        }
    }
}
}
dependencies {
compile project(':core')
compile project(':BaseGameUtils')
}

build.gradle(BaseGameUtils):

apply plugin: 'com.android.library'

buildscript {
repositories {
    jcenter()
}

dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
}
}

dependencies {
// Set defaults so that BaseGameUtils can be used outside of BasicSamples
if (!project.hasProperty('appcompat_library_version')) {
    ext.appcompat_library_version = '25.3.+'
}
if (!project.hasProperty('support_library_version')) {
    ext.support_library_version = '25.3.+'
}
if (!project.hasProperty('gms_library_version')) {
    ext.gms_library_version = '11.0.4'
}

compile "com.android.support:appcompat-v7:${appcompat_library_version}"
compile "com.android.support:support-v4:${support_library_version}"
compile "com.google.android.gms:play-services-games:${gms_library_version}"
compile "com.google.android.gms:play-services-plus:${gms_library_version}"
}

android {
// Set defaults so that BaseGameUtils can be used outside of BasicSamples
if (!project.hasProperty('android_compile_version')) {
    ext.android_compile_version = 25
}
if (!project.hasProperty('android_version')) {
    ext.build_tools_version = "25.0.2"
}
// Set defaults so that BaseGameUtils can be used outside of BasicSamples
if (!project.hasProperty('android_min_sdk_version')) {
    // Google Play Services minimum requirements is 14
    ext.android_min_sdk_version = 14
}

compileSdkVersion android_compile_version
buildToolsVersion "25.0.2"

defaultConfig {
    minSdkVersion android_min_sdk_version
    targetSdkVersion android_compile_version
}
}

AndroidLauncher:

package com.*****;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;
import com.google.android.gms.games.Games;
import com.google.example.games.basegameutils.GameHelper;
import com.*****.R;

public class AndroidLauncher extends AndroidApplication implements AdHandler,PlayServices {

@Override
protected void onPause() {
    super.onPause();
    rewardedAd.pause(this);
    adView.pause();
}

@Override
protected void onResume() {
    super.onResume();
    adView.resume();
    rewardedAd.resume(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    adView.destroy();
    rewardedAd.destroy(this);
    log("destroy", "android");
}

//Play Service
 private GameHelper gameHelper;
private final static int requestCode = 1;

//AD'S
private static final String TAG = "AndroidLauncher";
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
private final int SHOW_BIG_ADS = 3;
private final int HIDE_BIG_ADS = 2;
private final int SHOW_REWARD_ADS = 5;
private final int HIDE_REWARD_ADS = 4;
protected AdView adView;
protected InterstitialAd interstitialAd;
private boolean interstitialLoaded = false;
protected RewardedVideoAd rewardedAd;

private String rewardType;
private int beforeReward;

private boolean rewardedIsLoaded = false;

Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
            case SHOW_ADS:
                adView.setVisibility(View.VISIBLE);
                break;
            case HIDE_ADS:
                adView.setVisibility(View.GONE);
                break;
            case SHOW_BIG_ADS:
                if (interstitialLoaded) {
                    interstitialAd.show();
                }
                break;
            case HIDE_BIG_ADS:
                break;
            case SHOW_REWARD_ADS:
                if (rewardedAd.isLoaded()) {
                    rewardedAd.show();
                } else {
                    loadRewardedVideoAd();
                }
                break;

        }
    }
};

@Override
public boolean rewardIsLoaded() {
    return rewardedIsLoaded;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Play Services

    gameHelper = new GameHelper(this, GameHelper.CLIENT_GAMES);
    gameHelper.enableDebugLog(false);

    GameHelper.GameHelperListener gameHelperListener = new GameHelper.GameHelperListener()
    {
        @Override
        public void onSignInFailed(){ }

        @Override
        public void onSignInSucceeded(){ }
    };

    //AD'S

    MobileAds.initialize(this, "*******");

    rewardedAd = MobileAds.getRewardedVideoAdInstance(this);
    rewardedAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
        @Override
        public void onRewardedVideoAdLoaded() {
            rewardedIsLoaded = true;
        }

        @Override
        public void onRewardedVideoAdOpened() {

        }

        @Override
        public void onRewardedVideoStarted() {
            rewardedIsLoaded = false;
        }

        @Override
        public void onRewardedVideoAdClosed() {
            loadRewardedVideoAd();
        }

        @Override
        public void onRewarded(RewardItem rewardItem) { }

        @Override
        public void onRewardedVideoAdLeftApplication() {

        }

        @Override
        public void onRewardedVideoAdFailedToLoad(int i) {
            rewardedIsLoaded = false;
            loadRewardedVideoAd();
        }
    });
    loadRewardedVideoAd();

    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("********");
    final AdRequest adRequest = new AdRequest.Builder().addTestDevice("********")
            .build();
    interstitialAd.loadAd(adRequest);

    interstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            interstitialLoaded = true;
        }

        @Override
        public void onAdClosed() {
            super.onAdClosed();
            interstitialLoaded = false;
            interstitialAd.loadAd(adRequest);
        }
    });


    RelativeLayout layout = new RelativeLayout(this);
    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    View gameView = initializeForView(new com.marcol.colorion.Gaming(this,this), config);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);


    adView = new AdView(this);
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
        }
    });
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId("*********");

    AdRequest.Builder builder = new AdRequest.Builder();

    builder.addTestDevice("*********");

    RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT
    );

    RelativeLayout.LayoutParams gameParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    gameParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

    log("add", "GameView");
    layout.addView(gameView, gameParams);

    layout.addView(adView, adParams);
    adView.loadAd(builder.build());
    setContentView(layout);


    //initialize(new Gaming(), config);
    gameHelper.setup(gameHelperListener);
}

private void loadRewardedVideoAd() {
    rewardedAd.loadAd("**********", new AdRequest.Builder()
            .addTestDevice("*************").build());
}

@Override
public void showAds(boolean show) {
    handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}

@Override
public void showBigAd(boolean show) {
    handler.sendEmptyMessage(show ? SHOW_BIG_ADS : HIDE_BIG_ADS);
}

@Override
public void showRewardAd(boolean show, String rewardType, int beforeReward) {
    this.rewardType = rewardType;
    this.beforeReward = beforeReward;
    handler.sendEmptyMessage(show ? SHOW_REWARD_ADS : HIDE_REWARD_ADS);
}

//Play Services
@Override
protected void onStart() {
    super.onStart();
    gameHelper.onStart(this);
}

@Override
protected void onStop() {
    super.onStop();
    gameHelper.onStop();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    gameHelper.onActivityResult(requestCode, resultCode, data);
}


@Override
public void signIn() {
    try {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                gameHelper.beginUserInitiatedSignIn();
            }
        });
    } catch (Exception e) {
        Gdx.app.log("MainActivity", "Log in failed: " + e.getMessage() + ".");
    }
}

@Override
public void signOut() {
    try {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                gameHelper.signOut();
            }
        });
    } catch (Exception e) {
        Gdx.app.log("MainActivity", "Log out failed: " + e.getMessage() + ".");
    }
}

@Override
public void rateGame() {
    String str = "Your PlayStore Link";
    startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
}

@Override
public void unlockAchievement() {
   /* Games.Achievements.unlock(gameHelper.getApiClient(),
            getString(R.string.achievement_dum_dum));*/
}

@Override
public void submitScore(int highScore) {
    if (isSignedIn() == true) {
        Games.Leaderboards.submitScore(gameHelper.getApiClient(),
                getString(R.string.leaderboard_highscores), highScore);
    }
}

@Override
public void showAchievement() {
    if (isSignedIn() == true) {
        startActivityForResult(Games.Achievements.getAchievementsIntent(gameHelper.getApiClient()), requestCode);
    } else {
        signIn();
    }
}

@Override
public void showScore() {
   if (isSignedIn() == true) {
        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(gameHelper.getApiClient(),
                getString(R.string.leaderboard_highscores)), requestCode);
    } else {
        signIn();
    }
}

@Override
public boolean isSignedIn() {
    return gameHelper.isSignedIn();
}
}

我希望任何一个人都可以帮助我。

谢谢:) Discor

1 个答案:

答案 0 :(得分:0)

好的我修好了:

永远不要在主线程中调用HTTP请求:D

"在Android上:如果不禁用严格模式,则无法在主线程上访问网络。这样做是为了防止网络操作挂起主线程。见这里",

https://github.com/libgdx/libgdx/wiki/Networking