Posts tagged with android

I am developing library and using that library as aar to my app as below sample project.

Library Project:

public class UserDevice {     private static final String TAG = "UserDevice";     private static Context mContext;     public UserDevice(final Context context) {         mContext = context.getApplicationContext();     }     private static String getAdvertisementId(Context context) {         String _myads = null;         try {             String id = com.google.android.gms.ads.identifier.AdvertisingIdClient.getAdvertisingIdInfo(context).getId();             if (id != null) {                 _myads = id;             }         } catch (Exception e) {             Log.e(TAG, "Could not get det ID.", e);         }         return _myads;     } } //build.gradle android {     compileSdkVersion 29     buildToolsVersion "29.0.3"     defaultConfig {         minSdkVersion 16         targetSdkVersion 29         versionCode 1         versionName "1.0"         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"         consumerProguardFiles 'consumer-rules.pro'     } ... } dependencies {     // default dependencies     // ...     implementation 'com.google.android.gms:play-services-base:17.2.1'     implementation 'com.google.android.gms:play-services-ads:19.0.1' } 

App Project:

class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)         val device = UserDevice(this) //         btn_show_ads.setOnClickListener {             label_ads_id.text = device.myads             Log.d("App", "Device Ads id is: ${device.myads}")         }     } } /// build.gradle dependencies {     // default dependencies     // ...     compile(name: 'adslibrary', ext: 'aar')     // compile project(path: ':adslibrary') } 

But what happened here is that it giving error

2020-05-11 10:11:32.489 21925-21991/com.bhavdip.myadstest E/AndroidRuntime: FATAL EXCEPTION: Thread-2     Process: com.bhavdip.myadstest, PID: 21925     java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/ads/identifier/AdvertisingIdClient;         at com.bhavdip.adslibrary.CRDevice.getAdvertisementId(Unknown Source:2)         at com.bhavdip.adslibrary.CRDevice$1.run(Unknown Source:4)         at java.lang.Thread.run(Thread.java:764)      Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.identifier.AdvertisingIdClient" on path: DexPathList[[zip file "/data/app/com.bhavdip.myadstest-fP-f0WbH8_ErVLG3QBcgkQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.bhavdip.myadstest-fP-f0WbH8_ErVLG3QBcgkQ==/lib/arm, /system/lib, /vendor/lib]]         at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)         at java.lang.ClassLoader.loadClass(ClassLoader.java:379)

I already added and am using gms:play-services-ads dependency in library. The app again asks me to add those dependency forcefully.

If I will add the dependency to the app it works fine, but I don't want to add that dependency again. I don't want to expose my library dependency to end user or developer. Adds below dependency to my-library.

//For advertisement ID implementation 'com.google.android.gms:play-services-ads:19.1.0' implementation 'com.google.android.gms:play-services-base:17.2.1' 

Reference links: I tried this and this too. I also checked all answers in reference links.

I have tried multidex, transitive and also changed implementation to api or compile. in app project like this.

// Use transitive compile(name: 'adslibrary', ext: 'aar') {     transitive = true } 

----- EDIT for Questions -----

  • What is transitive? How it works?
  • How multidex works in library?

I might miss something to change. Please help me out of this problem.

My AdMob banner is not showing in my android application. After seeing older questions about the same problem none helped because they refer to a previous version of android studio. Now some things have become deprecated.

My NewRecord.java file contatins the following code:

package com.example.anxietycalendar; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.akaita.android.circularseekbar.CircularSeekBar; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.InterstitialAd; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.initialization.InitializationStatus; import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class NewRecord extends AppCompatActivity {     DatabaseHelper myDB;     private Button Submit;     public float pr;     private AdView mAdView;     private InterstitialAd mInterstitialAd;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_new_record);         MobileAds.initialize(this, new OnInitializationCompleteListener() {             @Override             public void onInitializationComplete(InitializationStatus initializationStatus) {             }         });         mAdView = findViewById(R.id.adView);         AdRequest adRequest = new AdRequest.Builder().build();         mAdView.loadAd(adRequest);         mAdView.setAdListener(new AdListener() {                                   @Override                                   public void onAdFailedToLoad(int errorCode) {                                       String onAdFailedToLoad = null;                                       Log.d(onAdFailedToLoad, "This is why: " + errorCode);                                   }         });         myDB=new DatabaseHelper(this);         Submit= (Button) findViewById(R.id.submit);         CircularSeekBar seekBar = (CircularSeekBar) findViewById(R.id.seekbar);         seekBar.setProgressTextFormat(new DecimalFormat("###,###,###,##0.00"));         seekBar.setProgress(0);         seekBar.setRingColor(Color.GREEN);         seekBar.setOnCircularSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {             @Override             public void onProgressChanged(CircularSeekBar seekBar, float progress, boolean fromUser) {                 if (progress < 5) {                     seekBar.setRingColor(Color.GREEN);                     pr = progress;                 } else if (progress < 8) {                     seekBar.setRingColor(Color.YELLOW);                     pr = progress;                 } else {                     seekBar.setRingColor(Color.RED);                     pr = progress;                 }             }             @Override             public void onStartTrackingTouch(CircularSeekBar seekBar) {             }             @Override             public void onStopTrackingTouch(CircularSeekBar seekBar) {             }         });         Submit.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 myDB.insertData(Math.round(pr));             }         });     } } 

While the activity_new_record.xml contains the following code:

<?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"     android:background="@drawable/background"     tools:context=".NewRecord">     <TextView         android:id="@+id/perctext"         android:layout_width="281dp"         android:layout_height="36dp"         android:layout_marginTop="36dp"         android:background="@drawable/bg_calen2"         android:ems="10"         android:gravity="center"         android:inputType="textPersonName"         android:text="How much stress do you feel?"         android:textColor="@color/white"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.43"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />     <com.akaita.android.circularseekbar.CircularSeekBar         android:id="@+id/seekbar"         android:layout_width="300dp"         android:layout_height="224dp"         android:layout_marginTop="52dp"         app:enabled="true"         app:innerCircleColor="#e0f7e9"         app:layout_constraintHorizontal_bias="0.495"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toBottomOf="@+id/perctext"         app:max="10"         app:min="0"         app:progress="1"         app:progressTextColor="@android:color/black"         app:progressTextSize="26"         app:ringColor="@color/colorAccent"         app:ringWidth="0.5"         app:showIndicator="true"         app:showInnerCircle="true"         app:showProgressText="true"         app:speedMultiplier="2" />     <Button         android:id="@+id/submit"         style="@style/BlueButton"         android:layout_width="271dp"         android:layout_height="31dp"         android:text="Submit Record"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/seekbar"         app:layout_constraintVertical_bias="0.168" />     <com.google.android.gms.ads.AdView         android:id="@+id/adView"         xmlns:ads="http://schemas.android.com/apk/res-auto"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_centerHorizontal="true"         ads:adSize="BANNER"         ads:adUnitId="ca-app-pub-6417467530528623/7094248892"         ads:layout_constraintBottom_toBottomOf="parent"         ads:layout_constraintEnd_toEndOf="parent"         ads:layout_constraintHorizontal_bias="0.615"         ads:layout_constraintStart_toStartOf="parent"         ads:layout_constraintTop_toBottomOf="@+id/submit"         ads:layout_constraintVertical_bias="0.198" /> </androidx.constraintlayout.widget.ConstraintLayout> 

This is the activity where I am trying to add the banner app. I have tried to use the test ad unit Id's but it is still failing.

I would also like to add this part of the code of the AndroidManifest.xml:

<application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="Anxiety Calendar"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/AppTheme"         tools:replace="android:label">         <activity android:name=".Statistics"></activity>         <!-- Include the AdActivity configChanges and theme. -->         <activity             android:name="com.google.android.gms.ads.AdActivity"             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"             android:theme="@android:style/Theme.Translucent" /> <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->         <meta-data             android:name="com.google.android.gms.ads.APPLICATION_ID"             android:value="ca-app-pub-6417467530528623~3872355031" /> 

How do I fix this? I have been trying for days... I am using a Galaxy Nexus API 22 simulator.

I am on a new App and in that I want to show some ads on the bottom. The AdView however is height 0dp at app startup and inflates only when the ads are loaded and then resizes the fragment and moves its content and children, which I want to avoid obviously.

My app is build like following:

In my MainActivity view I have a ConstraintLayout which has the Toolbar, the fragment container and the AdView as children and it's arranged like in the screenshot above. So the container is between the AdView and Toolbar which is causing the problem.

Of course I can make the fragment container align to the bottom of its parent but then the ads are shown above the containers content which can cause problems on smaller screens.

Is there any way I can somehow "reserve" the AdViews size upon app startup or something like this?

After updating Google Ads SDK to 19.0.0 gives a deprecated warning message for addTestDevice(), while I searched this link for resolving the issue but not succeed. how can I resolve it?

Here my code

mAdView.loadAd(new RequestConfiguration.Builder        .setTestDeviceIds(AdRequest.DEVICE_ID_EMULATOR) // show error        .setTestDeviceIds(DEV_ID) // show error        .build()); 

and developer site suggestion

Deprecated AdRequest.Builder.addTestDevice(). Use RequestConfiguration.Builder.setTestDeviceIds() instead.

our marketing team is running adwords for our google store ad campaign. they wants to check if an app installation event is triggered when a user clicked on the advertised link or not. (probably like conversion tracking for installation via adwords). is there anyway we can do this?