AndroidStudio小白,本文纯用来记录
一、AndroidStudio里面的操作
我使用的AndroidStudio的版本是:AndroidStudio 3.1.3,具体的环境配置,我也没怎么去弄。
遇到代码报错就按:Alt + Enter 看看能不解决 。
然后点击Run下面的小锤子,看看Build是否报错,有报错就百度
Unity接入SDK就我知道的有两种:我用第二种
1.将unity在安卓平台选择Gradle打包出来,然后在放在AndroidStudio中一顿眼花缭乱的操作。
2.将unity的class.jar包以及将穿山甲SDK压缩包内的open_ad_sdk.aar及android-query-full.0.26.7.jar,导入AndroidStudio中的新建的Library中,然后在导出来放到unity中去调用。
具体操作:
1.Project模式:下在左上角File->New->New Module->Android Library,新建一个Library..........名字叫MyLibrary
2.将unity的class.jar、open_ad_sdk.aar、android-query-full.0.26.7.jar包复制到MyLibrary的libs中。
3.然后选中两个jar后缀的右键他们,找到AddLibrary,编译一下。至于arr包,需要找到你这个Library下的build.gradle,然后在dependencies加入字段implementation(name: 'open_ad_sdk', ext: 'aar')。下面是我的全部引用,主要是有注释那个
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//按穿山甲开发文档加入他
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation files('libs/classes.jar')
implementation files('libs/android-query-full.0.26.7.jar')
//arr包的导入方式
implementation(name: 'open_ad_sdk', ext: 'aar')
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.6'
}
根据穿山甲的文档,在需要在dependencies的上面加入:
repositories {
flatDir {
dirs 'libs'
}
}
4.将模式切换为Android,然后和你的Library名字一样的文件,然后在下面的java文件下的第一个文件中,新建两个Activity。然后根据你的需要去写Java代码.....................
我这里只要激励广告所以只接入激励广告的:
初始化类:
package com.huowu.toutiaosdk2;
import android.content.Context;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdSdk;
/**
* 可以用一个单例来保存TTAdManager实例,在需要初始化sdk的时候调用
*/
public class TTAdManagerHolder {
private static boolean sInit;
public static TTAdManager get() {
if (!sInit) {
throw new RuntimeException("TTAdSdk is not init, please check.");
}
return TTAdSdk.getAdManager();
}
//初始化时需要传入UserId,在Unity中调用
public static void init(Context context, String userId)
{
doInit(context, userId);
}
//step1:接入网盟广告sdk的初始化操作,详情见接入文档和穿山甲平台说明
private static void doInit(Context context, String userId) {
if (!sInit) {
TTAdSdk.init(context, buildConfig(context, userId));
sInit = true;
}
}
private static TTAdConfig buildConfig(Context context, String userId) {
return new TTAdConfig.Builder()
.appId(userId)
.useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景,可以使用TextureView
.appName("APP测试媒体")
.titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
.allowShowNotify(true) //是否允许sdk展示通知栏提示
.allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页
.debug(true) //测试阶段打开,可以通过日志排查问题,上线时去除该调用
.directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_3G) //允许直接下载的网络状态集合
.supportMultiProcess(false)
.build();
}
}
继承Unity的方法类
package com.huowu.toutiaosdk2;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTRewardVid