【问题标题】:Library dependency class use in app android应用程序android中的库依赖类使用
【发布时间】: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'

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

我尝试过multidextransitive 并将implementation 更改为apicompile。在这样的应用程序项目中。

// 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


【解决方案1】:

你必须使用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依赖

【讨论】:

  • @AndyBoy 该问题似乎已被删除,您能否解释一下您在库模块中使用api 时遇到的问题
  • 您是否将该库作为应用项目中的另一个模块?
  • compile project(':my-library') 在应用程序和库中使用上述依赖项。没有其他模块只添加了来自libs的aar文件
  • stackoverflow.com/a/49445004/5134215 中所述,有两种方法可以将库作为依赖项添加到您的应用程序中,请尝试第一种方法并检查您是否正确执行。
  • 如果我在应用程序中再次添加给定的依赖项,我已经添加了正确的两种工作方式。
猜你喜欢
  • 2018-03-26
  • 1970-01-01
  • 2013-04-29
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多