高级功能
版本 GroMore

🧪 测试助手

GroMore提供了测试工具来帮助开发者调试广告集成。

ℹ️ 启用前准备

  • 确保SDK已初始化launchTestTools 只有在 GromoreAds.initAd 成功、并且 SDK 完成启动后才会返回成功。
  • 仅限调试构建:穿山甲测试工具不允许出现在生产包中,请务必通过 kDebugMode 等条件限制入口。
  • Android 额外依赖:插件不会把测试工具 AAR 打进最终产物,宿主 demo / App 需要自行在调试构建里引入:
  // android/app/build.gradle(.kts)
  dependencies {
      debugImplementation(files("../../plugins/gromore_ads/android/libs/tools-release.aar"))
  }
>   如果项目采用远程依赖,可改为官方 Maven 坐标 `com.pangle.cn:mediation-test-tools:<version>`。  
> - **iOS 注意事项**:测试工具仅在 Debug scheme 下可用,插件已内置 `BUAdTestMeasurement.xcframework` 与资源,开发者无须额外引入,但发布前请确认未调用相关 API。

## 启动测试工具

```dart
class AdTestHelper {
  // 启动GroMore测试工具
  static Future<void> launchTestTools() async {
    try {
      final bool result = await GromoreAds.launchTestTools();
      
      if (!result) {
        debugPrint('GroMore测试工具启动失败:SDK未就绪或依赖缺失');
      }
    } catch (e) {
      debugPrint('测试工具启动异常: $e');
    }
  }
}

// 在开发阶段添加测试按钮
class DebugMenu extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        if (kDebugMode) // 仅在Debug模式显示
          ElevatedButton.icon(
            onPressed: () => AdTestHelper.launchTestTools(),
            icon: Icon(Icons.build),
            label: Text('启动广告测试工具'),
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.orange,
              foregroundColor: Colors.white,
            ),
          ),
        // 其他调试功能...
      ],
    );
  }
}

测试流程建议

class AdDebugHelper {
  // 执行完整的广告测试流程
  static Future<void> runFullAdTest() async {
    print('🚀 开始执行广告测试流程...');
    
    // 1. 测试SDK初始化
    print('📋 测试SDK初始化...');
    final initResult = await _testSDKInit();
    if (!initResult) {
      print('❌ SDK初始化失败,终止测试');
      return;
    }
    
    // 2. 测试预加载
    print('📋 测试预加载功能...');
    await _testPreloadAds();
    
    // 3. 依次测试各种广告类型
    await _testSplashAd();
    await _testBannerAd();
    await _testInterstitialAd();
    await _testRewardVideoAd();
    await _testFeedAd();
    
    print('✅ 广告测试流程完成');
  }
  
  static Future<bool> _testSDKInit() async {
    try {
      return await GromoreAds.initAd(
        'your_app_id',
        useMediation: true,
        debugMode: true,
      );
    } catch (e) {
      print('SDK初始化异常: $e');
      return false;
    }
  }
  
  static Future<void> _testPreloadAds() async {
    try {
      await GromoreAds.preload(
        configs: const [
          PreloadConfig.rewardVideo(['your_reward_video_ad_id']),
          PreloadConfig.interstitial(['your_interstitial_ad_id']),
        ],
        parallelNum: 2,
        requestIntervalS: 1,
      );
      print('✅ 预加载测试完成');
    } catch (e) {
      print('❌ 预加载测试失败: $e');
    }
  }
  
  static Future<void> _testSplashAd() async {
    print('📋 测试开屏广告...');
    try {
      await GromoreAds.showSplashAd('your_splash_ad_id', preload: true);
      print('✅ 开屏广告测试完成');
    } catch (e) {
      print('❌ 开屏广告测试失败: $e');
    }
  }
  
  static Future<void> _testBannerAd() async {
    print('📋 测试Banner广告...');
    try {
      final result = await GromoreAds.loadBannerAd(
        'your_banner_ad_id',
        width: 320,
        height: 50,
      );
      print('${result ? '✅' : '❌'} Banner广告测试${result ? '完成' : '失败'}');
      
      if (result) {
        await Future.delayed(Duration(seconds: 3));
        await GromoreAds.destroyBannerAd();
      }
    } catch (e) {
      print('❌ Banner广告测试异常: $e');
    }
  }
  
  static Future<void> _testInterstitialAd() async {
    print('📋 测试插屏广告...');
    try {
      final loadResult = await GromoreAds.loadInterstitialAd('your_interstitial_ad_id');
      if (loadResult) {
        await Future.delayed(Duration(seconds: 1));
        await GromoreAds.showInterstitialAd('your_interstitial_ad_id');
      }
      print('${loadResult ? '✅' : '❌'} 插屏广告测试${loadResult ? '完成' : '失败'}');
    } catch (e) {
      print('❌ 插屏广告测试异常: $e');
    }
  }
  
  static Future<void> _testRewardVideoAd() async {
    print('📋 测试激励视频广告...');
    try {
      final loadResult = await GromoreAds.loadRewardVideoAd('your_reward_video_ad_id');
      if (loadResult) {
        await Future.delayed(Duration(seconds: 1));
        await GromoreAds.showRewardVideoAd('your_reward_video_ad_id');
      }
      print('${loadResult ? '✅' : '❌'} 激励视频测试${loadResult ? '完成' : '失败'}');
    } catch (e) {
      print('❌ 激励视频测试异常: $e');
    }
  }
  
  static Future<void> _testFeedAd() async {
    print('📋 测试信息流广告...');
    try {
      final adIds = await GromoreAds.loadFeedAd('your_feed_ad_id', count: 1);
      if (adIds.isNotEmpty) {
        await Future.delayed(Duration(seconds: 2));
        await GromoreAds.clearFeedAd(adIds);
      }
      print('${adIds.isNotEmpty ? '✅' : '❌'} 信息流广告测试${adIds.isNotEmpty ? '完成' : '失败'}');
    } catch (e) {
      print('❌ 信息流广告测试异常: $e');
    }
  }
}

使用建议

  1. 开发阶段: 使用 kDebugMode 确保测试工具只在开发环境显示
  2. 测试流程: 建议按照上述顺序进行全面测试
  3. 错误排查: 查看控制台输出,及时发现和解决问题
  4. 生产环境: 发布前记得移除所有测试代码

下一步

需要进一步协助?

与 LightCore 技术顾问沟通,获取商业化策略与集成支持。