FlutterでiOSアプリにgoogle_mobile_adsを使うと、以下のようにCocoaPods関連のエラーで起動しません。
解決方法を教えてもらえないでしょうか。
●エラー
Terminal
1Launching lib/main.dart on iPhone 11 in debug mode... 2Running Xcode build... 3Xcode build done. 11.3s 4Failed to build iOS app 5Error output from Xcode build: 6↳ 7 objc[42521]: Class AMSupportURLConnectionDelegate is implemented in both /usr/lib/libauthinstall.dylib (0x20c6bec10) and /System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x122d802b8). One of the two will be used. Which one is undefined. 8 objc[42521]: Class AMSupportURLSession is implemented in both /usr/lib/libauthinstall.dylib (0x20c6bec60) and /System/Library/PrivateFrameworks/MobileDevice.framework/Versions/A/MobileDevice (0x122d80308). One of the two will be used. Which one is undefined. 9 ** BUILD FAILED ** 10 11 12Xcode's output: 13↳ 14 Error: Cannot run with sound null safety, because the following dependencies 15 don't support null safety: 16 17 - package:google_mobile_ads 18 19 For solutions, see https://dart.dev/go/unsound-null-safety 20 21 Command PhaseScriptExecution failed with a nonzero exit code 22 note: Using new build system 23 note: Building targets in parallel 24 note: Planning build 25 note: Analyzing workspace 26 note: Constructing build description 27 note: Build preparation complete 28 /Users/flutter/adtest/ios/Pods/Pods.xcodeproj: warning: The iOS Simulator deployment target 'IPHONEOS_DEPLOYMENT_TARGET' is set to 8.0, but the range of supported deployment target versions is 9.0 to 14.5.99. (in target 'Flutter' from project 'Pods') 29 30Could not build the application for the simulator. 31Error launching application on iPhone 11. 32
●実行環境
Flutter 2.2.2 • channel stable • https://github.com/flutter/flutter.git Framework • revision d79295af24 (8 days ago) • 2021-06-11 08:56:01 -0700 Engine • revision 91c9fc8fe0 Tools • Dart 2.13.3 OS:Mac 11.4 (M1 MacBook Air) IDE:Android Studio 4.2.1
●main.dart
dart
1import 'package:flutter/material.dart'; 2import 'package:google_mobile_ads/google_mobile_ads.dart'; 3import 'dart:io'; 4void main() { 5 //以下の二行を追加 6 WidgetsFlutterBinding.ensureInitialized(); 7 MobileAds.instance.initialize(); 8 //追加ここまで 9 runApp(MyApp()); 10} 11 12class MyApp extends StatelessWidget { 13 // This widget is the root of your application. 14 15 Widget build(BuildContext context) { 16 return MaterialApp( 17 title: 'Flutter Demo', 18 theme: ThemeData( 19 primarySwatch: Colors.blue, 20 ), 21 home: MyHomePage(title: 'Flutter Demo Home Page'), 22 ); 23 } 24} 25 26class MyHomePage extends StatefulWidget { 27 MyHomePage({Key? key, required this.title}) : super(key: key); 28 29 final String title; 30 31 32 _MyHomePageState createState() => _MyHomePageState(); 33} 34 35class _MyHomePageState extends State<MyHomePage> { 36 // This widget is the root of your application. 37 final BannerAd myBanner = BannerAd( 38 adUnitId: getTestAdBannerUnitId(), 39 size: AdSize.banner, 40 request: AdRequest(), 41 listener: AdListener(), 42 ); 43 44 Widget build(BuildContext context) { 45 myBanner.load(); 46 47 final AdWidget adWidget = AdWidget(ad: myBanner); 48 49 final Container adContainer = Container( 50 alignment: Alignment.center, 51 child: adWidget, 52 width: myBanner.size.width.toDouble(), 53 height: myBanner.size.height.toDouble(), 54 ); 55 return Scaffold( 56 appBar: AppBar( 57 title: Text(widget.title), 58 ), 59 body: Center( 60 61 child: Column( 62 63 mainAxisAlignment: MainAxisAlignment.center, 64 children: <Widget>[ 65 adContainer, 66 ], 67 ), 68 ), 69// This trailing comma makes auto-formatting nicer for build methods. 70 ); 71 } 72} 73 74//プラットホームごとのテスト広告IDを取得するメソッドをどこかに配置してください。 75String getTestAdBannerUnitId(){ 76 String testBannerUnitId = ""; 77 if(Platform.isAndroid) { 78 // Android のとき 79 testBannerUnitId = "ca-app-pub-3940256099942544/6300978111"; 80 } else if(Platform.isIOS) { 81 // iOSのとき 82 testBannerUnitId = "ca-app-pub-3940256099942544/2934735716"; 83 } 84 return testBannerUnitId; 85}
Podfile
※2行目でiOSのバージョンを11に指定
txt
1# Uncomment this line to define a global platform for your project 2platform :ios, '11.0' 3 4# CocoaPods analytics sends network stats synchronously affecting flutter build latency. 5ENV['COCOAPODS_DISABLE_STATS'] = 'true' 6 7project 'Runner', { 8 'Debug' => :debug, 9 'Profile' => :release, 10 'Release' => :release, 11} 12 13def flutter_root 14 generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__) 15 unless File.exist?(generated_xcode_build_settings_path) 16 raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first" 17 end 18 19 File.foreach(generated_xcode_build_settings_path) do |line| 20 matches = line.match(/FLUTTER_ROOT\=(.*)/) 21 return matches[1].strip if matches 22 end 23 raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get" 24end 25 26require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root) 27 28flutter_ios_podfile_setup 29 30target 'Runner' do 31 use_frameworks! 32 use_modular_headers! 33 34 flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__)) 35end 36 37post_install do |installer| 38 installer.pods_project.targets.each do |target| 39 flutter_additional_ios_build_settings(target) 40 end 41end
●Runner.xcworkspace
※Deployment Targetを11に変更
●pubspec.yaml
yaml
1name: testapp 2description: A new Flutter project. 3publish_to: 'none' # Remove this line if you wish to publish to pub.dev 4version: 1.0.0+1 5 6environment: 7 sdk: '>=2.12.0 <3.0.0' 8 9dependencies: 10 flutter: 11 sdk: flutter 12 13 google_mobile_ads: 14 cupertino_icons: ^1.0.2 15 16dev_dependencies: 17 flutter_test: 18 sdk: flutter 19flutter: 20 uses-material-design: true
回答1件
あなたの回答
tips
プレビュー