Help Center/Details/

SDK Initial configuration

Initial configuration

Focus:

  1. The CSJ SDK needs to be initialized in the main thread
  2. Multi-process involves the use of WebView. If users want to use their own data path, they can call WebView.setDataDirectorySuffix() before the SDK is initialized.
    Developers need to call the following code in the Application#onCreate() method to initialize the CSJ SDK.
    Important: At present, the SDK supports multiple processes. If it is clear that a certain process will not use the advertising SDK, you can initialize the advertising SDK only for a specific process
public class DemoApplication extends Application {
    public static String PROCESS_NAME_XXXX = "process_name_xxxx";

    @Override
    public void onCreate() {
        super.onCreate();
        //强烈建议在应用对应的Application#onCreate()方法中调用,避免出现content为null的异常
        TTAdSdk.init(context,
            new TTAdConfig.Builder()
                .appId("5001121")
                .useTextureView(true) //默认使用SurfaceView播放视频广告,当有SurfaceView冲突的场景,可以使用TextureView
                .appName("APP测试媒体")
                .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)//落地页主题
                .allowShowNotify(true) //是否允许sdk展示通知栏提示,若设置为false则会导致通知栏不显示下载进度
                .debug(true) //测试阶段打开,可以通过日志排查问题,上线时去除该调用
                .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI) //允许直接下载的网络状态集合,没有设置的网络下点击下载apk会有二次确认弹窗,弹窗中会披露应用信息
                .supportMultiProcess(false) //是否支持多进程,true支持
                ~~ .asyncInit(true) //是否异步初始化sdk,设置为true可以减少SDK初始化耗时。3450版本开始废弃~~
                //.httpStack(new MyOkStack3())//自定义网络库,demo中给出了okhttp3版本的样例,其余请自行开发或者咨询工作人员。
                .build());
        //如果明确某个进程不会使用到广告SDK,可以只针对特定进程初始化广告SDK的content
        //if (PROCESS_NAME_XXXX.equals(processName)) {
        //   TTAdSdk.init(context, config);
        //}
    }
}

Initialization interface description

SDK initialization of 3450 and above

Adjustment instructions:

CSJ has made major changes to the SDK initialization method in version 3450, supporting both synchronous initialization and asynchronous initialization, and the asynchronous initialization API supported in TTAdConfig.Builder will no longer take effect. After optimization, there is no significant difference in time consumption between synchronous initialization and asynchronous initialization. The latter puts part of the initialization logic in the child thread. If developers use the asynchronous initialization method, please note that they need to request advertisements after the success callback.
Add initialization interface
Important note: If you are accessing the CSJ Pro version of the SDK, you can only use asynchronous initialization. At the same time, the obfuscation rules should be adjusted simultaneously

CSJ SDK initialization API: This API must be called in the main thread, and the CSJ will execute the initialization operation in the child thread.

/**
 * 注意事项:
 *     1.该API必须在主线程中调用,若未在主线程初始化则会抛出异常 "Wrong Thread ! Please exec TTAdSdk.init in main thread."
 *     2.请求广告前需要保证已经调用了init方法。
 * 推荐使用该API执行穿山甲初始化操作
 * 
 * @param context  必须是Application context
 * @param config   初始化配置信息,必要参数
 * @param callback InitCallback 异步初始化回调
 */
public static void init( Context context,  TTAdConfig config,  InitCallback callback) 

//初始化回调说明
public interface InitCallback {
    /**
     * 初始化成功回调
     * 注意:开发者需要在success回调之后再去请求广告
     */
    void success();

    /**
     * @param code 初始化失败回调错误码
     * @param msg 初始化失败回调信息
     */
    void fail(int code, String msg);

}

Fail callback status code

Vale description
4000 Initialization error code caused by local execution API error

Initialization state interface

/**
 * @return 返回初始化是否成功的状态
 */
public static boolean isInitSuccess()

Adjust API
CSJ SDK initialization method: The API is adjusted to be called in the main thread, and the initialization operation will be executed in the main thread.

/**
 * @param context 必须是application context
 * @param config  初始化配置信息,必要参数
 * @return TTAdManager实例
 */
public static TTAdManager init(final Context context, final TTAdConfig config)

Expired & invalid API

//TTAdConfig.Builder中支持异步初始化API将过期
public Builder asyncInit(boolean asyncInit)

SDK initialization method below 3450

Note: AppId is required. If the appid is sent through the server, you need to make a judgment if it is not empty before initialization.

/**
  * 穿山甲sdk初始化入口
  * @param context 必须是application context
  * @param config  初始化配置信息,必要参数
  * @return TTAdManager实例
  */
public static TTAdManager init(Context context, TTAdConfig config);

初始化配置参数说明
    public static class TTAdConfig.Builder {
       private String mAppId;// 必选参数,设置应用的AppId
       private String mAppName;// 必选参数,设置应用名称
       private boolean mIsPaid = false;// 可选参数,设置是否为计费用户:true计费用户、false非计费用户。默认为false非计费用户。须征得用户同意才可传入该参数
       private String mKeywords;// 可选参数,设置用户画像的关键词列表 **不能超过为1000个字符**。须征得用户同意才可传入该参数
       private String mData;// 可选参数,设置额外的用户信息 **不能超过为1000个字符**
       private int mTitleBarTheme = TTAdConstant.TITLE_BAR_THEME_LIGHT;// 可选参数,设置落地页主题,默认为TTAdConstant#TITLE_BAR_THEME_LIGHT
       private boolean mAllowShowNotify = true;// 可选参数,设置是否允许SDK弹出通知:true允许、false禁止。默认为true允许
       private boolean mIsDebug = false;// 可选参数,是否打开debug调试信息输出:true打开、false关闭。默认false关闭
       private int[] mDirectDownloadNetworkType;//允许直接下载的网络状态集合,不调用则所有网络下均无下载确认弹窗,调用不传参数则所有网络均有下载确认弹窗
       private boolean mIsUseTextureView = false;// 可选参数,设置是否使用texture播放视频:true使用、false不使用。默认为false不使用(使用的是surface)
       private boolean mIsSupportMultiProcess = false;// 可选参数,设置是否支持多进程:true支持、false不支持。默认为false不支持
       private IHttpStack mHttpStack;//可选参数,设置外部网络请求,默认为urlconnection
       private boolean mIsAsyncInit = false;//是否异步初始化sdk,开启可减少初始化耗时
       private TTCustomController mCustomController;//可选参数,可以设置隐私信息控制开关
   }

Privacy information control switch

 public abstract class TTCustomController {
       /**
        * 是否允许SDK主动使用地理位置信息
        *
        * @return true可以获取,false禁止获取。默认为true
        */
       public boolean isCanUseLocation() {
           return true;
       }

       /**
        * 当isCanUseLocation=false时,可传入地理位置信息,穿山甲sdk使用您传入的地理位置信息
        *
        * @return 地理位置参数
        */
       public TTLocation getTTLocation() {
           return null;
       }

       /**
        * 是否允许SDK主动使用手机硬件参数,如:imei
        *
        * @return true可以使用,false禁止使用。默认为true
        */
       public boolean isCanUsePhoneState() {
           return true;
       }

       /**
        * 当isCanUsePhoneState=false时,可传入imei信息,穿山甲sdk使用您传入的imei信息
        *
        * @return imei信息
        */
       public String getDevImei() {
           return null;
       }

       /**
        * 是否允许SDK主动使用ACCESS_WIFI_STATE权限
        *
        * @return true可以使用,false禁止使用。默认为true
        */
       public boolean isCanUseWifiState() {
           return true;
       }

       /**
        * 是否允许SDK主动使用WRITE_EXTERNAL_STORAGE权限
        *
        * @return true可以使用,false禁止使用。默认为true
        */
       public boolean isCanUseWriteExternal() {
           return true;
       }

       /**
        * 开发者可以传入oaid
        *
        * @return oaid
        */
        public String getDevOaid() {
           return null;
        }

   }

Example:

private static TTAdConfig buildConfig(Context context) {
    return new TTAdConfig.Builder()
            ...
            .customController(new TTCustomController() {
                @Override
                public boolean isCanUsePhoneState() {
                    return true;
                }
            })
            ...
}

Note:
-Need to set isCanUseLocation() before overriding getTTLocation()
-You need to set isCanUsePhoneState() before overriding getDevImei()

Construct TTAdManager object
The TTAdManager object is the entry interface of the entire SDK, which can be used for advertisement acquisition, permission request, version number acquisition, etc.
Construction method

//一定要在初始化后才能调用,否则为空
TTAdManager ttAdManager = TTAdSdk.getAdManager();

TTAdManger interface description

public interface TTAdManager {

// 创建TTAdNative对象 必须采用activity
TTAdNative createAdNative(Context context);

//部分机型需要主动申请权限,如 READ_PHONE_STATE权限
void requestPermissionIfNecessary(Context context); 

//退出时尝试显示"提示安装app"对话框,返回值:true显示对话框、false不显示对话框
boolean tryShowInstallDialogWhenExit(Context context, final ExitInstallListener listener);

//获取穿山甲sdk版本号
String getSDKVersion();


// 3600版本新增接口设置夜间模式

// 设置主题类型,0:正常模式;1:夜间模式;默认为0;传非法值,按照0处理
void setThemeStatus(int themeStatus);

// 获取当前主题类型
 int getThemeStatus();
}

Night mode
3600版本新增接口设置夜间模式

// 设置主题类型,0:正常模式;1:夜间模式;默认为0;传非法值,按照0处理
void setThemeStatus(int themeStatus);

// 获取当前主题类型
 int getThemeStatus();

Points to note when importing SDK:

  • TTAdManagerHolder maintains singleton mode;
  • The SDK is initialized multiple times in a single process based on the information of the first initial setting;
  • appId is a 7-digit `character generated by the wearing app on the platform;
  • Any type of video ad loading and a black screen appears after loading, you can try to solve the problem in the following ways:
  • Whether to add the black screen permission in the manifest file
  • <uses-permission data-tomark-pass=""> </uses-permission>
  • Try to use the TextureView control to play the video
  • AppName is not empty. It is recommended to be consistent with the application name created by the platform
  • DirectDownloadNetworkType configuration:
    • Can be configured according to specific product requirements, the network set in the method is the network that allows direct download
    • If you need to have a confirmation pop-up window for downloading under any network, just write blank (not null) in the method
  • Description of supportMultiProcess:
    • If the project is a single process, it must be changed to false
    • If the project is multi-process, you need to set true. If you need to display ads in each process in multi-process, you must initialize the SDK in each process
    • If your application needs to support multiple processes, be sure to set TTAdConfig.supportMultiProcess(true).
    • Confirm the app multi-process support judgment method:
      • CSJ sdk initialization
      • Acquisition of CSJ advertisements
      • CSJ advertising display
    • The calls of these three key points are multi-process in different processes, otherwise they are single-process
    • If not necessary, try not to use multi-process switch, multi-process efficiency is not as high as single process
  • Provider configuration
  • Both single process and multiple processes must be configured with providers required by CSJ
  • The provider needs to be configured regardless of whether the platform application is in a test state or an official state
  • In order not to affect the conversion and revenue of ads, please be sure to configure xxx.TTMultiProvider in the manifest file
  • If your application also obfuscates resources (such as andResGuard), please don't obfuscate any resources of CSJ to prevent the problem of crashing due to missing resources.
  • The resources on the whiteList.txt whitelist in the SDK compressed file do not support obfuscation. Developers need to recheck the whitelist content during the SDK update iteration process.
Did the content solve your problem?
Contact us