如何通过在Android-Studio中编辑build.gradle将我的库放在android.jar前面

WZY*_*WZY 11 android gradle android-studio android-gradle-plugin

首先,这是我在Eclipse中的Java构建路径: 在此输入图像描述

这四个jar的common.jar,core.jar,framework.jar,layout.jar'是从Android源代码打包的,它包含一些开发人员无法公开使用的类.它们不需要导出,因为它们是作弊编译器.在Eclipse中一切都很好.

现在我正在尝试使用gradle将我的项目导入Android-Studio.我已经将jar添加到依赖项中,但是我无法更改我的jar和android jar的编译顺序.我不能把这些罐放在android jar前面.我不熟悉gradle,现在编译器无法在这些jar中找到类.任何帮助将不胜感激!这是我的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')
    }
}
Run Code Online (Sandbox Code Playgroud)

Sco*_*rta 11

你不能在Gradle(*)中做你想做的事,至少在写这篇文章时的可预见的未来.一些问题妨碍了你:

  • 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插件中,因此您无法获得该依赖项并将其替换为其他内容.

(*)说了这么多,没有什么是不可能的 - 你可以让它工作,但是你将不得不一起破解一些东西,所以它会比Eclipse方法更容易出错,并且更难以维护面对SDK和工具更新.当出现问题时,你将自己独立.

  • 您可以使用自己的自定义SDK组装自己的自定义SDK android.jar.
  • 你可以破解Android Gradle插件.这种方法肯定是艰难的 - 那里的学习曲线相当陡峭,而且代码处于大量开发状态,这将是一个维护负担,因为你试图保持最新状态.

我对这些方法中的任何一种都提供了更多的洞察力,部分原因是我不太了解它并且很容易给你不好的建议,部分是因为我不希望没有经验的开发人员认为这是一个很棒的要做的事.但是如果你弄明白的话,那将是非常值得写的,因为我以前见过这样的问题,所以你不是唯一的问题.


小智 10

以下脚本适用于我:

allprojects {
    gradle.projectsEvaluated {
       tasks.withType(JavaCompile) {
           options.compilerArgs.add('-Xbootclasspath/p:/mylib.jar')
       }
    }
}
Run Code Online (Sandbox Code Playgroud)


Ber*_*tel 8

我从这篇文章中解决了使用系统库构建应用程序的问题:

假设你已经添加系统库,例如libframework.jarlibcore.jarapp/libs:

  • 添加Xbootclasspath到您的顶级build.gradle:

    allprojects {
    
        gradle.projectsEvaluated {
            tasks.withType(JavaCompile) {
                options.compilerArgs.add('-Xbootclasspath/p:app/libs/libframework.jar:app/libs/libcore.jar')
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在你的应用程序中build.gradle,使用provided:

    dependencies {
        provided fileTree(include: ['*.jar'], dir: 'libs')
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在同一个应用程序中build.gradle,添加一个任务以将<orderEntry>引用Android API 25 Platform放在最后一个位置app.iml,这样gradle将首先考虑您的系统库和最后的Android SDK:

    preBuild {
    
        doLast {
            def imlFile = file(project.name + ".iml")
            println 'Change ' + project.name + '.iml order'
            try {
                def parsedXml = (new XmlParser()).parse(imlFile)
                def jdkNode = parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
                parsedXml.component[1].remove(jdkNode)
                def sdkString = "Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"
                new Node(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
                groovy.xml.XmlUtil.serialize(parsedXml, new FileOutputStream(imlFile))
            } catch (FileNotFoundException e) {
                // nop, iml not found
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)


Kel*_*dos 0

您可以自动执行此操作,就像在 Eclipse 中一样:

File> Project structure...> > (select app in Modules)>(go to Dependencies tab)reposition with arrows on the right

另一种方法是编辑应用程序所在文件夹中的 [AppName].iml 文件。您要更改的是文件末尾的标签。但是,每次您清理或重新打开项目时,Android Studio 都会重新排列它们。