第一次搞SDK的开发,简直是一脸懵逼,逻辑层的代码实现还好说,可是如何搭建SDK的框架呢,总不能撸一个app的 框架
给SDK用吧,SDK本身要考虑的东西也很多,首先网络层 、整体架构、crash统计、事件统计,代码安全,热更 等问题不断
的涌现,当然最好的老师就是撸一遍 三方的SDK看他们如何做的,主要有头条和 广点通。头条主要是教会了我 使用interface
的编程思想,广点通呢主要是动态下发dex文件达到修复的效果,本文做以总结!
头条的广告结构
github 地址 里面的sdblibrary就是头条的部分源码https://github.com/comerss/NewFrame.git
头条的广告给到我们的全是接口,所以如果顺着接口进去,那只能看到一堆方法,啥也没有,这样做的好处就是我们从接入层面很难看到源码是怎么实现的,实现类我们根本不知道在哪,于是我用笨办法反编译过来,然后把混淆后的代码一点点还原,还原了一部分,但是想要的逻辑都是能找得到的。这个包名是我随便命名的,这个无所谓,一下分析只是本人学习SDK架构的样例,请勿用做非法商业途径。
一下我们拿头条的开屏逻辑为例查看一下源码。
头条开屏逻辑
首先我们拿到的回调是TTSplashAd
public abstract interface TTSplashAd
{
@NonNull
public abstract View getSplashView();
public abstract int getInteractionType();
public abstract void setSplashInteractionListener(AdInteractionListener paramAdInteractionListener);
public abstract void setDownloadListener(TTAppDownloadListener paramTTAppDownloadListener);
public abstract void setNotAllowSdkCountdown();
public static abstract interface AdInteractionListener
{
public abstract void onAdClicked(View paramView, int paramInt);
public abstract void onAdShow(View paramView, int paramInt);
public abstract void onAdSkip();
public abstract void onAdTimeOver();
}
}
我们首选寻找的肯定是这个类的实现
public class TTSplashAdImpl implements TTSplashAd, MineHandler.OnResult {
private static String TAG = "TTSplashAdImpl";
private int b = 3;
private Context mContext;
private NativeAdData d;
private SplashLayout mSplashLayout;
private TTSplashAd.AdInteractionListener mInteractionListener;
private boolean g;
private MineHandler mHandler = new MineHandler(Looper.getMainLooper(), this);
private DownLoadListenerImpl mLoadListener;
TTSplashAdImpl(@NonNull Context paramContext, @NonNull NativeAdData paramh) {
this.mContext = paramContext;
this.d = paramh;
a();
}
void a() {
this.mSplashLayout = new SplashLayout(this.mContext);
AdEvent.addEventData(this.d);
if (this.d.s() <= 0) {
a(3);
} else {
this.b = this.d.s();
a(this.b);
}
c();
}
void a(Drawable paramDrawable) {
this.mSplashLayout.setDrawable(paramDrawable);
}
private void c() {
//这个方法的主要功能是对开屏广告的展示以及显示时间进行监听以及回调,尔后我们再进一步进行分析
this.mLoadListener = getDownLoadListenerImpl(this.d);
SplashManager splashManager = new SplashManager(this.mContext, this.mSplashLayout);
splashManager.setAdType(3);
this.mSplashLayout.addView(splashManager);
splashManager.setCallback(new SplashManager.SplashListener() {
public void onWindowFocusChanged(boolean paramAnonymousBoolean) {
if (mLoadListener != null) {
if (paramAnonymousBoolean) {
mLoadListener.e();
} else {
mLoadListener.f();
}
}
}
public void onAttachedToWindow() {
}
public void onDetachedFromWindow() {
}
public void onShow(View paramAnonymousView) {
AdEvent.show(mContext, d, "splash_ad");
if (!g) {
mHandler.sendEmptyMessage(1);
}
if (mInteractionListener != null) {
mInteractionListener.onAdShow(mSplashLayout, d.c());
}
if (d.t()) {
ToolUtils.a(d, paramAnonymousView);
}
LogUtils.b(d.b(), "开屏广告展示");
}
});
splashManager.setNeedCheckingShow(true);
//这个类是所有的广告的点击事件的处理类,这样就方便了全部广告的点击集中处理
AdClickListerReal adClickListerReal = new AdClickListerReal(this.mContext, this.d, "splash_ad", 4);
adClickListerReal.setView(this.mSplashLayout);
adClickListerReal.b(this.mSplashLayout.getDislikeView());
adClickListerReal.setDownLoadListener(this.mLoadListener);
adClickListerReal.setOnClickLister(new AdClickListenerImpl.OnClick() {
public void onClick(View paramAnonymousView, int paramAnonymousInt) {
if (mInteractionListener != null) {
mInteractionListener.onAdClicked(paramAnonymousView, paramAnonymousInt);
}
if ((paramAnonymousInt != 4) && (paramAnonymousInt != -1)) {
mHandler.removeCallbacksAndMessages(null);
b = 0;
}
}
});
if (this.mLoadListener != null) {
this.mLoadListener.a(new com.bytedance.sdk.openadsdk.core.a.c(this.mContext, this.d, "splash_ad"));
}
this.mSplashLayout.setOnClickListenerInternal(adClickListerReal);
this.mSplashLayout.setOnTouchListenerInternal(adClickListerReal);
this.mSplashLayout.setSkipListener(new View.OnClickListener() {
public void onClick(View paramAnonymousView) {
if (!StringUtils.isEmpty(TTSplashAdImpl.this.d.o())) {
AdEvent.c(TTSplashAdImpl.this.d);
}
if (TTSplashAdImpl.this