1

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.

AndyBoy
  • 564
  • 6
  • 29

1 Answers1

1

You have to use api instead of implementation

api 'com.google.android.gms:play-services-ads:19.1.0'
api 'com.google.android.gms:play-services-base:17.2.1'

implementation vs api

api - dependency will be exposed to the module which uses this library

implementation - dependency will be resolved only in this module

transitive=false is to skip the api dependencies of the library modules

Vignesh Sundaramoorthy
  • 1,827
  • 1
  • 25
  • 38
  • @AndyBoy that question is deleted it seems, can you explain your issue you are facing when using the `api` in the library module – Vignesh Sundaramoorthy May 06 '20 at 07:27
  • Are you having the library as another module in the app project? – Vignesh Sundaramoorthy May 06 '20 at 07:37
  • `compile project(':my-library')` in app and in library using above dependency. No another module only added aar file from `libs` – AndyBoy May 06 '20 at 07:54
  • There are two ways to add library as a dependency to your app as mentioned in this https://stackoverflow.com/a/49445004/5134215 Try the first way & check whether you did it properly. – Vignesh Sundaramoorthy May 06 '20 at 08:58
  • I have added that properly both the way it is working fine if I add given dependency again in app. – AndyBoy May 06 '20 at 09:52
  • Please see issue again as I have edited with more details with code – AndyBoy May 07 '20 at 08:14
  • Hey, I told you to use `api` instead of `implementation` in your library `build.gradle`, Still you are having `implementation`? Kindly read my solution properly & the link I have gave above – Vignesh Sundaramoorthy May 07 '20 at 14:53
  • Yes, I had tried with `api` also but still was crashing while using it. You can try with given project. – AndyBoy May 08 '20 at 04:12
  • You must use `api` in library if you want to expose the dependency to app, try to fix the crash you are getting on using `api`, post the crash log here – Vignesh Sundaramoorthy May 08 '20 at 06:13
  • I have added error log in issue description. please have a look into. – AndyBoy May 11 '20 at 04:51
  • You are getting the error why using `api` in the library right? Did you have Application class in your app? Try disabling the instant run & check did you configured the multidex properly https://stackoverflow.com/q/43435658/5134215 https://stackoverflow.com/q/22399572/5134215 – Vignesh Sundaramoorthy May 11 '20 at 07:30