The AyanAdManager SDK is a robust solution for managing advertisements in Android applications. It seamlessly integrates with multiple ad providers (e.g., AdMob, HamrahAds, Adivery), enabling developers to display ads efficiently while tracking essential statistics.
To use Google Ads (AdMob) in your application, you must add the following metadata tag to your AndroidManifest.xml file. This tag specifies your AdMob appId, which is required for initializing Google Ads.
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID"/>
</application>
</manifest>
Replace YOUR_ADMOB_APP_ID with your actual AdMob app ID. This step is mandatory for Google Ads to function correctly.
Step 1: Add the SDK to your Project In your project-level build.gradle file, include the following repository:
repositories {
maven { url "https://jitpack.io" }
}
In the app-level build.gradle, add the dependency:
dependencies {
implementation 'com.github.AyanTech:AyanAds:Tag'
}
Step 1: Initialize the SDK Before using the SDK, initialize it in your Application class:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
AyanAdManager.initialize(
appCompatActivity = this,
appMarket = "YOUR_APP_MARKET",
appKey = "YOUR_APP_KEY",
onSuccess = { Logger.d("AyanAdManager initialized successfully.") },
onError = { Logger.e("Failed to initialize AyanAdManager: $it") }
)
}
}
Step 2: Display an Ad To display an ad, use the following example:
AyanAdManager.showAd(
useDefaultNativeAdView = true,
containerKey = "CONTAINER_KEY",
appCompatActivity = this,
adSize = BannerAdSize.SMALL, // Required for banner ad [SMALL, MEDIUM, LARGE]
adContainerId = findViewById(R.id.view)
)
When displaying native ads, you can configure the behavior based on whether you want to use the default native ad view or customize it:
-
Use the Default Native Ad View: Set
useDefaultNativeAdView = trueto use the SDK's default view without customization.AyanAdManager.showAd( useDefaultNativeAdView = true, containerKey = "CONTAINER_KEY", appCompatActivity = this, adContainerId = findViewById(R.id.view) )
-
Customize the Default Native Ad View: Set
useDefaultNativeAdView = trueand provide anativeAdAttributesobject to customize the default view.AyanAdManager.showAd( useDefaultNativeAdView = true, containerKey = "CONTAINER_KEY", appCompatActivity = this, adContainerId = findViewById(R.id.view), nativeAdAttributes = NativeAdAttributes( titleColor = ContextCompat.getColor(this, R.color.black), buttonTextColor = Color.parseColor("#000000"), buttonBackgroundTint = Color.parseColor("#ffffff"), typeface = ResourcesCompat.getFont(this, R.font.medium) ) )
-
Use a Custom Native Ad View: If you want to provide your custom view for displaying native ads, set
useDefaultNativeAdView = falseand define your layout.
AyanAdManager.showAd(
useDefaultNativeAdView = false,
containerKey = "CONTAINER_KEY",
appCompatActivity = this,
adContainerId = findViewById(R.id.view)
)Note: When using a custom native ad view, you must use the following specific view IDs in your layout to ensure the ad is displayed correctly:
| ID Name | View Type |
|---|---|
ad_title |
TextView |
ad_media |
com.google.android.gms.ads.nativead.MediaView |
ad_price |
TextView |
ad_store |
TextView |
ad_cta |
Button |
ad_banner |
ImageView |
ad_stars |
RatingBar |
ad_icon |
ImageView |
ad_description |
TextView |
ad_cta_view |
Button |
These IDs are required regardless of the ad type being displayed. They ensure that the SDK can properly bind the ad content to your custom view.
When using a custom native ad view (useDefaultNativeAdView = false), all layout containers — including the parent and all nested views — must use LinearLayout.
Avoid using RelativeLayout, ConstraintLayout, or any other layout types anywhere in the hierarchy. These layouts are not supported and may lead to rendering issues or SDK integration problems.
For Google Ads, it is necessary to obtain user consent for personalized advertising, especially for users in specific regions like the European Union (EU). The AyanAdManager SDK handles this automatically. However, if you must manually request consent or display a consent dialog at a specific point in your app, you can use the ConsentManager.requestConsent() function.
It's crucial to handle the lifecycle of ads when the activity or fragment is destroyed to ensure proper resource management and avoid memory leaks. Use the destroy method to release resources associated with the ad when the activity is destroyed.
override fun onDestroy() {
super.onDestroy()
AyanAdManager.adProvider.destroy()
}
This method ensures that all ad resources are properly released, preventing memory leaks and improving your application's overall performance.
If you're using ProGuard, add the following rules to your ProGuard configuration file:
-keep public class ir.ayantech.hamrahads.** { *; }
-keep class ir.ayantech.ayanadmanager.model.api.** { *; }
-keep class ir.ayantech.ayanadmanager.utils.constant.** { *; }
To view SDK logs in Logcat, filter logs using the following tag:
TAG: "AyanAdManager"
adb logcat -s AyanAdManager- Open the Logcat window
- Enter
AyanAdManagerin the search/filter box to isolate SDK logs
This will help you monitor the SDK’s behavior, initialization process, and any errors or debug information provided during integration.