Android Studio中的Base64依赖项问题 [英] Base64 dependencies issue in Android Studio

查看:604
本文介绍了Android Studio中的Base64依赖项问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们当前的项目中,我们将Android Studio 0.6.1与Gradle插件0.11。+一起使用,并且遇到了 commons-codec 的依赖问题。我们从本地Artifactory实例中引入一个依赖项类,该类包含一个使用以下两行代码的 crypto服务:

We're using Android Studio 0.6.1 with Gradle plugin 0.11.+ in our current project, and we're running into a dependency issue with commons-codec. We're pulling in a dependency class from our local Artifactory instance that contains a "crypto" service that uses the following two lines of code:

byte[] encryptedOutput = cipherFactory.getEncryptCipher().doFinal(plaintext.getBytes());
byte[] encryptedCipherText = Base64.encodeBase64URLSafe(encryptedOutput);

问题是,即使我们定义了 commons-codec的特定依赖项, 在我们的Gradle配置中,我们得到以下异常

The problem is that even if we define a specific dependency of commons-codec in our Gradle configuration, we get the following exception

java.lang.NoSuchMethodError: org.apache.commons.codec.binary.Base64.encodeBase64URLSafe

首先,我们手动为' commons-codec:commons-codec:1.9',但是根据Android Studio,当我深入研究IDE中的代码时,它查看的是1.9中该方法的版本,但是当应用程序运行时,我们得到了例外。即使将依赖关系更改为1.4,即使按照该方法可用的Javadocs的说明,它仍然会失败。即使完全删除了手动依赖项,也会发生相同的事情。

At first, we were manually including a dependency for 'commons-codec:commons-codec:1.9', but according to Android Studio when I drill down into the code in the IDE, it's looking at the version of that method in 1.9, but when the app runs, we get the exception. Even changing the dependency to 1.4 it still fails, even though according to the Javadocs that's when that method became available. Even removing the manual dependency altogether causes the same thing to happen.

有什么方法可以找出正在运行的应用程序从何处获取此依赖项?这是目前我们完整的依赖项列表,我无法从任何这些文件中找到公共编解码器

Is there any way I can find out where the running app is pulling this dependency from? This is our complete dependency list at the moment, and I can't find commons-codec from any of these

compile files('libs/HockeySDK-3.0.2.jar')
compile files('libs/PushIOManager.jar')
compile 'commons-lang:commons-lang:2.6@jar'
compile 'org.codehaus.jackson:jackson-core-asl:1.9.2@jar'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.2@jar'
compile 'com.google.android.gms:play-services:4.4.52'
compile 'com.mcxiaoke.volley:library:1.0.4'
compile 'fr.avianey:facebook-android-api:3.14.1@aar'
compile 'javax.validation:validation-api:1.0.0.GA'

我担心的是,此类已被Android SDK埋在某个地方,我们将无法覆盖它以使用 commons-codec ,这将允许我们使用我们的库。即使我们可以做到,但我担心这样做可能会导致Android本身出现一些根本问题。我们可以(并且目前可以这样做)将该加密服务类的源放入我们的应用程序,并对其进行调整以使用适当的等效项,但这意味着每当我们更改一个或另一个版本时,我们都必须保留

My fear is that this class is buried somewhere with the Android SDK and we'll have no way to override it to use a version of commons-codec that will allow us to use our library. And even if we could do that, I'm concerned doing so might cause some fundamental issue with Android itself. We can (and currently do) have the source for that crypto service class pulled into our app and tweaked it to use the appropriate equivalent, but this means any time we make a change to one or the other version, we'd have to keep them in sync.

任何想法吗?

更新:在这种情况下,似乎可行的方法是扫描Gradle构建文件中的依赖项,一旦找到要查找的依赖项,便会覆盖您要使用的版本。例如:

UPDATE: What seems to work in this specific case is to scan the dependencies in the Gradle build files and once the dependency is found you're looking for, override is with the version you want to use. For example:

def versionOverrides = [
    "commons-codec:commons-codec": "1.9",
]

subprojects {
    configurations.all {
        resolutionStrategy.eachDependency { DependencyResolveDetails details ->

            def overrideVersion = versionOverrides[details.requested.group + ":" + details.requested.name]

            if (overrideVersion != null && details.requested.version != overrideVersion) {
                logger.info "Overriding dependency ${details.requested.group}:${details.requested.name} version ${details.requested.version} --> $overrideVersion"
                details.useVersion overrideVersion
            }
        }
    }
}


推荐答案


我担心的是,此类被Android SDK
所掩埋,我们将无法覆盖它使用 commons-codec
的版本,这将允许我们使用我们的库。

My fear is that this class is buried somewhere with the Android SDK and we'll have no way to override it to use a version of commons-codec that will allow us to use our library.

这就是正在发生的事情。

引导类加载器已预装了1.3版的Commons Codec库。

This is exactly what's happening.
The boot classloader is pre-loaded with classes from the version 1.3 of Commons Codec library.

您可以重新打包(重命名类的程序包/名称空间)Common Codec库,以避免发生这种冲突。请参阅我的答案此处以获取更多详细说明。

You can repackage (rename classes's package/namespace) the Commons Codec library to avoid this conflict. See my answer here for more detailed description.

这篇关于Android Studio中的Base64依赖项问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆