如何通过在 Android-Studio 中编辑 build.gradle 将我的库放在 android.jar 前面 [英] How to put my libraries in front of android.jar by editing build.gradle in Android-Studio

查看:29
本文介绍了如何通过在 Android-Studio 中编辑 build.gradle 将我的库放在 android.jar 前面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先这是我在 Eclipse 中的 Java 构建路径:

First Here's my Java Build Path in Eclipse:

这四个jar包'common.jar,core.jar,framework.jar,layout.jar'是从Android源代码中打包出来的,其中包含了一些开发者不能公开使用的类.导出是因为它们用于作弊编译器.在 Eclipse 中一切正常.

These four jars 'common.jar,core.jar, framework.jar,layout.jar' are packaged from Android source code, which contains some classes that can't be publicly used by developer.They needn't to be exported because they are for cheat compiler. In Eclipse everything is OK.

现在我正在尝试使用 gradle 将我的项目导入到 Android-Studio.我已经将 jars 添加到依赖项中,但是我无法更改我的 jars 和 android jar 的编译顺序.我不能把这些jar放在android jar前面.我对gradle不熟悉,现在编译器找不到这些jar中的类.任何帮助将不胜感激!这是我的 build.gradle:

Now I'm trying to import my project to Android-Studio with gradle.I've add the jars to dependencies,However I can't change the compile order of my jars and android jar. I can't put these jars in front of android jar.I'm not familiar with gradle, now the compiler can't find classes in these jars. Any help will be appreciated! Here's my build.gradle:

apply plugin: 'android'    
dependencies {

    compile files('jars/common.jar')
    compile files('jars/core.jar')
    compile files('jars/framework.jar')
    compile files('jars/layout.jar')
    compile fileTree(dir: 'libs', include: '*.jar')
    compile files('jars/animation_nineoldandroids_src.jar')
    compile files('jars/json_simple_src.jar')
    compile files('jars/jsoup-1.7.2-sources.jar')
}

android {

    compileSdkVersion 17
    buildToolsVersion "21.1.1"
    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

推荐答案

你不能在 Gradle(*) 中做你想做的事,至少在撰写本文的可预见的未来是这样.一些问题正在妨碍您:

You can't do what you want in Gradle(*), at least for the foreseeable future at the time this is written. A few problems are getting in your way:

  • Gradle 不像 Eclipse 那样对构建类路径中的依赖项进行排序,而这正是您将类置于 android.jar 之前所做的工作.Gradle 的理念是,您应该明确构建中的依赖项,以便可以理解和重复发生的事情;依赖类路径排序的系统往往是微妙和脆弱的.所以你需要做的是告诉 Gradle 你的项目依赖于你的自定义类而不是 android.jar,但是插件的 DSL 并没有给你这样做的方法.http://forums.gradle.org/gradle/topics/classpath_ordering_againhttp://www.gradle.org/docs/current/userguide/dependency_management.html
  • 另一种看待它的方式是对 android.jar 的引用被硬编码到 Android Gradle 插件中,因此您无法获得该依赖项并将其替换为其他内容.
  • Gradle doesn't do ordering of dependencies in the build classpath the way that Eclipse does, which is what you were doing to put your classes ahead of android.jar. Gradle has the philosophy that you should be explicit about dependencies in your build so what's going on is understandable and repeatable; systems that rely on classpath ordering tend to be subtle and fragile. So what you would need to do is to tell Gradle that your project depends on your custom classes and not android.jar, but the plugin's DSL doesn't give you the means to do that. There's some discussion at http://forums.gradle.org/gradle/topics/classpath_ordering_again and http://www.gradle.org/docs/current/userguide/dependency_management.html
  • Another way of looking at it is a reference to android.jar is hardcoded into the Android Gradle plugin, so you can't get at that dependency and replace it with something else.

(*) 说了这么多,没有什么是不可能的——你可以让它工作,但是你将不得不一起破解一些东西,所以它比 Eclipse 方法更容易出问题,也更难保持面对 SDK 和工具更新.当出现问题时,您将独自一人.

(*) Having said all that, nothing is impossible -- you could make it work, but you're going to have to hack something together, so it's going to be more trouble-prone than the Eclipse approach, and tougher to maintain in the face of SDK and tooling updates. And when something goes wrong you'll be on your own.

  • 您可以使用自己的 android.jar 组装自己的自定义 SDK.
  • 您可以破解 Android Gradle 插件.这种方法肯定会很困难 - 那里的学习曲线非常陡峭,而且代码正在大量开发中,当您尝试保持最新状态时,这将是一种维护负担.
  • You could assemble your own custom SDK with your own android.jar.
  • You could hack the Android Gradle plugin. This approach would definitely be tough -- the learning curve there is pretty steep, and the code is under heavy development, which would be a maintenance burden as you try to stay up-to-date.

我犹豫要不要对这两种方法中的任何一种提供更深入的了解,部分原因是我对此知之甚少,很容易给您不好的建议,部分原因是我不希望没有经验的开发人员看到这一点这是一件很棒的事情.但是如果你想通了,那就非常值得写出来了,因为我以前见过这样的问题,所以你不是唯一一个.

I hesitate to offer much more insight into either of those approaches, partly because I don't know a lot about it and could pretty easily give you bad advice, and partly because I don't want inexperienced developers seeing this to think it's an awesome thing to do. But if you figure it out, it would be very much worthy of writing up, because I've seen this sort of question before, so you're not the only one.

这篇关于如何通过在 Android-Studio 中编辑 build.gradle 将我的库放在 android.jar 前面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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