【发布时间】:2020-04-30 10:31:16
【问题描述】:
我正在开发库并将该库用作我的应用程序的 aar,如下示例项目。
图书馆项目:
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'
}
应用项目:
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')
}
但是这里发生的事情是它给出了错误
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)
我已经在库中添加并正在使用 gms:play-services-ads 依赖项。该应用程序再次要求我强制添加这些依赖项。
如果我将依赖项添加到应用程序,它可以正常工作,但我不想再次添加该依赖项。 我不想将我的库依赖项暴露给最终用户或开发人员。将以下依赖项添加到 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'
参考链接:我也试过this 和this。我还检查了参考链接中的所有答案。
我尝试过multidex、transitive 并将implementation 更改为api 或compile。在这样的应用程序项目中。
// Use transitive
compile(name: 'adslibrary', ext: 'aar') {
transitive = true
}
-----问题编辑 -----
- 什么是
transitive?它是如何工作的? -
multidex如何在图书馆工作?
我可能会错过一些需要改变的东西。请帮我解决这个问题。
【问题讨论】:
-
降级
com.google.android.gms:play-services-ads:19.0.1 -
我已经尝试过,但在将
gms:play-services-ads降级到 19.0.1 后没有工作。
标签: android kotlin android-gradle-plugin gradle-dependencies google-ads-api