Library dependency class use in app 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.
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
@AndyBoy that question is deleted it seems, can you explain your issue you are facing when using the api in the library module
Are you having the library as another module in the app project?
compile project(':my-library') in app and in library using above dependency. No another module only added aar file from libs
There are two ways to add library as a dependency to your app as mentioned in this stackoverflow.com/a/49445004/5134215 Try the first way & check whether you did it properly.
I have added that properly both the way it is working fine if I add given dependency again in app.