AppLovin / AppLovin-MAX-SDK-Android

Other
210 stars 95 forks source link

Custom Native Ad not showing Icon because MaxNativeAdViewBinder's setIconContentViewId is protected #675

Open linversion opened 1 week ago

linversion commented 1 week ago

MAX SDK Version

12.5.0

Device/Platform Info

all device

Current Behavior

I'm trying to show my custom native ad template. But the icon will not show. Because in MaxNativeAdView's a() method, there's a logic assert this.h(which is the iconContentViewId cast to FrameLayout) not null, only if this.h != null will go in the setImageDrawable logic to this.g(the Icon ImageView) and the only way to set this iconContentViewId is MaxNativeAdViewBinder's setIconContentViewId which is protected

// MaxNativeAdView.class
private void a(MaxNativeAd var1) {

        MaxNativeAd.MaxNativeAdImage var2 = var1.getIcon();
        View var3 = var1.getIconView();
        FrameLayout var4;
        if ((var4 = this.h) != null) {
            if (var2 != null && this.g != null) {
                if (var2.getDrawable() != null) {
                    this.g.setImageDrawable(var2.getDrawable());
                } else if (var2.getUri() != null && StringUtils.isValidString(var2.getUri().toString())) {
                    ImageViewUtils.setAndDownscaleImageUri(this.g, var2.getUri());
                } else {
                    this.h.setVisibility(8);
                }
            } else if (var3 != null) {
                var3.setLayoutParams(new FrameLayout.LayoutParams(-1, -1));
                this.h.removeAllViews();
                this.h.addView(var3);
            } else {
                var4.setVisibility(8);
            }
        }

    }

Expected Behavior

can show icon image view without setting iconContentViewId or provide a method to set it

How to Reproduce

  1. create custom native template layout
  2. create a MaxNativeAdViewBinder by it' Builder, create a MaxNativeAdView(binder, activity) val binder = MaxNativeAdViewBinder.Builder(R.layout.applovin_native_banner) .setTitleTextViewId(R.id.title_text_view) .setBodyTextViewId(R.id.body_text_view) .setAdvertiserTextViewId(R.id.advertiser_textView) .setIconImageViewId(R.id.icon_image_view) .setOptionsContentViewGroupId(R.id.ad_options_view) .setCallToActionButtonId(R.id.cta_button) .build() val adView = MaxNativeAdView(binder, activity)
  3. render MaxAd by MaxNativeAdLoader.render(adView, maxAd)

Additional Info

No response

NanaAmoah commented 1 week ago

Hi @linversion I tested the code snippet you shared and have not been able to reproduce the issue.

Could you reproduce the issue in our demo app and share a screenshot showing the issue you are seeing?

linversion commented 1 week ago

Hi @linversion I tested the code snippet you shared and have not been able to reproduce the issue.

Could you reproduce the issue in our demo app and share a screenshot showing the issue you are seeing?

Hi @NanaAmoah , here is how I reproduce the issue in kotlin demo app.

  1. dependency
    //app build.gradle
    if (isLocalAppLovinWorkspace) {
        implementation(project(":Android-SDK"))
    } else {
        implementation("com.applovin:applovin-sdk:12.5.0@aar")
    }
  2. create layout

    
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    >
    
    <ImageView
        android:id="@+id/icon_image_view"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_margin="6dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:contentDescription="icon" />
    <FrameLayout
        android:id="@+id/icon_content_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <FrameLayout
        android:layout_width="26dp"
        android:layout_height="15dp"
        android:id="@+id/options_view"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent">
    
    </FrameLayout>
    
    <ImageView
        android:id="@+id/ad_icon"
        android:layout_width="27dp"
        android:layout_height="18dp"
        app:layout_constraintStart_toEndOf="@id/icon_image_view"
        app:layout_constraintTop_toTopOf="@id/icon_image_view"
        android:layout_marginStart="7dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/title_text_view"
        tools:text="Test Ad: "
        app:layout_constraintStart_toEndOf="@id/ad_icon"
        app:layout_constraintTop_toTopOf="@id/ad_icon"
        app:layout_constraintBottom_toBottomOf="@id/ad_icon"
        android:layout_marginStart="8dp"
    />
<TextView
    android:id="@+id/advertiser_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintStart_toEndOf="@id/title_text_view"
    app:layout_constraintTop_toTopOf="@id/title_text_view"
    tools:text="Flood-it"
/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:id="@+id/body_text_view"
    app:layout_constraintStart_toStartOf="@id/ad_icon"
    app:layout_constraintEnd_toStartOf="@id/cta_button"
    app:layout_constraintTop_toBottomOf="@id/ad_icon"
    android:layout_marginTop="5dp"
    tools:text="Text text tesxt texge tegn  tejogne gegegn gegoentete tee nnn ted tedgn tete"
    android:maxLines="2"
    android:ellipsize="end"
    android:layout_marginEnd="5dp"
    android:textSize="11sp"
/>
<Button
    android:layout_width="76dp"
    android:layout_height="wrap_content"
    android:id="@+id/cta_button"
    android:text="GO"
    android:background="@color/colorAccent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    android:layout_marginEnd="8dp"
/>

</androidx.constraintlayout.widget.ConstraintLayout>


3. build the binder
```kotlin
    // ManualNativeLateBindingAdActivity
    private fun createNativeAdView(): MaxNativeAdView {
        val binder: MaxNativeAdViewBinder = MaxNativeAdViewBinder.Builder(R.layout.applovin_native_banner)
                .setTitleTextViewId(R.id.title_text_view)
                .setBodyTextViewId(R.id.body_text_view)
                .setAdvertiserTextViewId(R.id.advertiser_text_view)
                .setIconImageViewId(R.id.icon_image_view)
                .setOptionsContentViewGroupId(R.id.options_view)
                .setCallToActionButtonId(R.id.cta_button)
                .build()

        return MaxNativeAdView(binder, this)
    }
  1. load the native ad.

Screenshot_20240618_124518_AppLovin MAX Demo App

the icon view can not show

but if i set the iconContentViewId by reflection

    // ManualNativeLateBindingAdActivity
    private fun createNativeAdView(): MaxNativeAdView {
         val binder: MaxNativeAdViewBinder = MaxNativeAdViewBinder.Builder(R.layout.applovin_native_banner)
                .setTitleTextViewId(R.id.title_text_view)
                .setBodyTextViewId(R.id.body_text_view)
                .setAdvertiserTextViewId(R.id.advertiser_text_view)
                .setIconImageViewId(R.id.icon_image_view)
                .setOptionsContentViewGroupId(R.id.options_view)
                .setCallToActionButtonId(R.id.cta_button)
                .build()
        val iconContentViewId = binder.javaClass.getDeclaredField("iconContentViewId")
        iconContentViewId.isAccessible = true
        iconContentViewId.set(binder, R.id.icon_content_view)

        return MaxNativeAdView(binder, this)
    }

Screenshot_20240618_124432_AppLovin MAX Demo App

NanaAmoah commented 1 week ago

Thanks @linversion. Can you please share the project which reproduces the issue here?

linversion commented 1 week ago

@NanaAmoah bro I already said the project I used was the Applovin Max Demo App-Kotlin,just add your sdk key and native ad unit ID,follow the step I mentioned before

NanaAmoah commented 4 days ago

@linversion could you double check that you are setting the correct package name, SDK key and ad unit ID in the demo app?

A mismatch in your credentials will result in the issue you are seeing. To verify that you are using the correct package name and SDK key, tap "Launch Mediation Debugger". You should see your package name under "App Info" and all the ad unit IDs created for that app under "View Ad Units".

I am only able to reproduce your issue in our demo app with mismatched credentials so if after double checking, you are still reproducing the issue, please share the demo app project where the issue is reproducible here.