gpt4 book ai didi

android - 应用程序android中的库依赖类使用

转载 作者:行者123 更新时间:2023-12-02 13:30:49 25 4
gpt4 key购买 nike

我正在开发库并将该库用作我的应用程序的 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库中的依赖。该应用程序再次要求我强制添加这些依赖项。

如果我将依赖项添加到应用程序,它可以正常工作 ,但我不想再次添加该依赖项。 我不想将我的库依赖暴露给最终用户或开发人员 .将以下依赖项添加到我的库。
//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'

引用链接 : 我试过 thisthis也。我还检查了引用链接中的所有答案。

我试过 multidex , transitive并且还更改了 implementationapicompile .在这样的应用程序项目中。
// Use transitive
compile(name: 'adslibrary', ext: 'aar') {
transitive = true
}

----- 编辑 问题 -----
  • 什么是transitive ?这个怎么运作?
  • 如何multidex在图书馆工作?

  • 我可能会错过一些需要改变的东西。请帮我解决这个问题。

    最佳答案

    您必须使用 api而不是实现

    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 - 依赖将暴露给使用这个库的模块
    implementation - 依赖将仅在此模块中解决
    transitive=false是跳过 api库模块的依赖关系

    关于android - 应用程序android中的库依赖类使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61521343/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com