Flutter Preparation
Development Environment Requirements
- Flutter: 2.0 or higher
- Dart: 2.12 or higher
- Android Studio: Latest version recommended
- Xcode: Latest version recommended (for iOS development)
- Android SDK: Android 8.0 or higher
- iOS SDK: iOS 10.0 or higher
Project Configuration
1. Add Dependencies
Add UJU Ad SDK dependencies to your pubspec.yaml file:
yaml
dependencies:
flutter:
sdk: flutter
# UJU Ad SDK
uju_ad_sdk: ^1.0.0
# Required dependencies
device_info: ^2.0.2
package_info: ^2.0.2
shared_preferences: ^2.0.13
webview_flutter: ^3.0.4Then run flutter pub get to install the dependencies.
2. Configure Android
AndroidManifest.xml
Add necessary permissions to android/app/src/main/AndroidManifest.xml:
xml
<!-- Required permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- Optional permissions -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- UJU configuration -->
<meta-data
android:name="com.ujusdk.APP_ID"
android:value="YOUR_APP_ID" />
<meta-data
android:name="com.ujusdk.APP_KEY"
android:value="YOUR_APP_KEY" />build.gradle
Add necessary configurations to android/build.gradle:
gradle
repositories {
mavenCentral()
// UJU repository
maven {
url 'https://maven.ujusdk.com/repository/maven-releases/'
}
}3. Configure iOS
Info.plist
Add necessary configurations to ios/Runner/Info.plist:
xml
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>NSUserTrackingUsageDescription</key>
<string>This identifier will be used to provide you with personalized ads.</string>
<!-- UJU configuration -->
<key>UjuAppID</key>
<string>YOUR_APP_ID</string>
<key>UjuAppKey</key>
<string>YOUR_APP_KEY</string>Podfile
Ensure your ios/Podfile has the correct platform version:
ruby
platform :ios, '10.0'
post_install do |installer|
installer.pods_project.targets.each do |target|
flutter_additional_ios_build_settings(target)
end
endSDK Initialization
Initialize the SDK in your main.dart file:
dart
import 'package:flutter/material.dart';
import 'package:uju_ad_sdk/uju_ad_sdk.dart';
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Initialize UJU SDK
initializeUjuSDK();
runApp(MyApp());
}
void initializeUjuSDK() {
UjuAdInitConfig config = UjuAdInitConfig(
appId: "YOUR_APP_ID",
appKey: "YOUR_APP_KEY",
isDebug: true,
userId: "USER_ID" // Optional, for user-level ad targeting
);
UjuAdSDK.initSDK(config, (success, error) {
if (success) {
print("UJU SDK initialized successfully");
// SDK initialized successfully, can start loading ads
} else {
print("UJU SDK initialization failed: $error");
}
});
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'UJU Flutter Demo',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('UJU Flutter Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'UJU Ad SDK Demo',
),
ElevatedButton(
onPressed: () {
// Load banner ad
loadBannerAd();
},
child: Text('Load Banner Ad'),
),
],
),
),
);
}
void loadBannerAd() {
UjuBannerAdConfig config = UjuBannerAdConfig(
placementId: "YOUR_BANNER_PLACEMENT_ID",
width: 320,
height: 100,
);
UjuAdSDK.loadBannerAd(config, (success, error) {
if (success) {
print("Banner ad loaded successfully");
} else {
print("Banner ad loading failed: $error");
}
});
}
}Testing
Test Mode
Enable test mode during development:
dart
// Enable test mode
UjuAdSDK.setTestMode(true);Test Devices
Add test devices to avoid invalid clicks:
dart
// Add test device
UjuAdSDK.addTestDevice("YOUR_DEVICE_ID");Best Practices
- Early initialization: Initialize the SDK as early as possible in the application startup process
- Platform-specific settings: Configure platform-specific settings correctly for Android and iOS
- Error handling: Implement proper error handling for ad loading failures
- Ad preloading: Preload ads before they need to be displayed
- Frequency control: Control ad display frequency to avoid user annoyance
- Test thoroughly: Test ads on both Android and iOS platforms
Common Questions
Q: Why isn't the SDK initializing?
A: Possible reasons:
- Incorrect App ID or App Key
- Network connection issues
- Platform-specific configuration issues
- Flutter version incompatibility
Q: Why aren't ads displaying?
A: Possible reasons:
- Incorrect placement ID
- Ad container size is 0 or hidden
- Insufficient ad inventory
- Ad loading failed due to network issues
- Platform-specific issues
Q: What ad formats are supported in Flutter?
A: Supported ad formats:
- Banner ad
- Interstitial ad
- Rewarded video ad
- Native ad
- Splash ad
Q: How to handle ad events?
A: Use event listeners to handle ad events:
dart
// Add ad event listener
UjuAdSDK.onAdLoaded((event) {
print('Ad loaded: ${event.placementId}');
});
UjuAdSDK.onAdError((event) {
print('Ad error: ${event.placementId}, ${event.error}');
});
UjuAdSDK.onAdClicked((event) {
print('Ad clicked: ${event.placementId}');
});
UjuAdSDK.onAdClosed((event) {
print('Ad closed: ${event.placementId}');
});
UjuAdSDK.onRewarded((event) {
print('Rewarded: ${event.placementId}, ${event.reward}');
});Next Steps
After completing the preparation work, you can proceed with:
