티스토리 뷰
안드로이드 애드몹 보상형(리워드) 광고 google admob 버젼 20.2.0
build.gradle(Project)
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
build.gradle (Module)
implementation 'com.google.android.gms:play-services-ads:20.2.0' 추가하기
Cannot fit requested classes in a single dex file (# methods: 66104 > 65536) 에러 메시지 수정방법
minSdkVersion 16의 버전을 하나씩 올려서 확인해본다.
minSdkVersion 21 에서 에러가 사라졌다.
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.jw.rewarded_example"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
implementation 'com.google.android.gms:play-services-ads:20.2.0'
}
AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jw.rewarded_example">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Rewarded_Example">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
레이아웃 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/txtDisplayMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="296dp"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.547"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/showButton"
app:layout_constraintVertical_bias="0.673" />
<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="168dp"
android:layout_marginTop="184dp"
android:onClick="showAd"
android:text="Show AD"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.jw.rewarded_example;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
public class MainActivity extends AppCompatActivity {
Button btnShowAds;
TextView txtDisplayMess;
private static String ads_id= "ca-app-pub-3940256099942544/5224354917";
private RewardedAd mRewardedAd;
private String TAG = "MY_TAG";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShowAds= findViewById(R.id.showButton);
txtDisplayMess = findViewById(R.id.txtDisplayMessage);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
loadAdsRequest();
}
});
btnShowAds.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"Show Ads",Toast.LENGTH_SHORT).show();
if(mRewardedAd != null){
mRewardedAd.show(MainActivity.this, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
//Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
txtDisplayMess.setText(rewardAmount +" : "+ rewardType);
}
});
}else{
Log.d(TAG,"The rewarded ad wasn't ready yet.");
}
}
});
}
private void loadAdsRequest() {
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, ads_id,
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
//Handle the error.
Log.d(TAG, loadAdError.getMessage());
mRewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
mRewardedAd = rewardedAd;
Log.d(TAG,"Ad was loaded.");
mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG,"Ad was shown.");
mRewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.d(TAG,"Ad failed to show.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when ad is dismissed.
//Don't forget to set the ad reference to null so you
// don't show the ad a second time.
Log.d(TAG,"Ad was dismissed.");
loadAdsRequest();
}
});
}
});
}
}
2021.06.19 - [프로그래밍/안드로이드 앱프로그래밍] - 안드로이드 전면광고 google admob version 20.1.0
안드로이드 애드몹 보상형(리워드)광고 동영상 보면서 따라하기
'프로그래밍 > 안드로이드 앱프로그래밍' 카테고리의 다른 글
내 휴대폰에서 센서 알아보기 (0) | 2021.09.30 |
---|---|
국가코드 가져와서 TTS에 알맞게 언어 바꾸기 (0) | 2021.06.28 |
안드로이드 전면광고 google admob version 20.1.0 (0) | 2021.06.19 |
버튼 눌렀을때 메시지가 나타나게 하기 (0) | 2021.06.17 |
안드로이드 sqlite 삭제하기 (Delete) (0) | 2021.04.08 |
댓글