How To Put My Libraries In Front Of Android.jar By Editing Build.gradle In Android-studio
Solution 1:
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 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 notandroid.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.
(*) 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.
- 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.
Solution 2:
Following script works for me:
allprojects {
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.compilerArgs.add('-Xbootclasspath/p:/mylib.jar')
}
}
}
Solution 3:
I solved the issue from this post to build application with system libraries :
Supposing you have added system libraries like libframework.jar
and libcore.jar
in app/libs
:
add
Xbootclasspath
to your top levelbuild.gradle
:allprojects { gradle.projectsEvaluated { tasks.withType(JavaCompile) { options.compilerArgs.add('-Xbootclasspath/p:app/libs/libframework.jar:app/libs/libcore.jar') } } }
in you app
build.gradle
, useprovided
:dependencies { providedfileTree(include: ['*.jar'], dir: 'libs') }
in the same app
build.gradle
, add a task to put<orderEntry>
referring toAndroid API 25 Platform
in the last position inapp.iml
, this way gradle will take into account your system libs first and Android SDK in last resort :preBuild { doLast { defimlFile= file(project.name + ".iml") println 'Change ' + project.name + '.iml order'try { defparsedXml= (newXmlParser()).parse(imlFile) defjdkNode= parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' } parsedXml.component[1].remove(jdkNode) defsdkString="Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"newNode(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK']) groovy.xml.XmlUtil.serialize(parsedXml, newFileOutputStream(imlFile)) } catch (FileNotFoundException e) { // nop, iml not found } } }
Solution 4:
A updated and somewhat more future-proof answer (since bootclasspath compilerargs have been changing in more recent JDKs):
- Supposing you have taken system libraries like framework.jar and libcore.jar from aosp intermediates (generated when building aosp) and added them into a folder (such as system_libs) in your project, add the libraries to the compile classpath in build.gradle:
dependencies {
compileOnly fileTree(dir: 'system_libs', include: ['*.jar'])
}
gradle.projectsEvaluated {
tasks.withType(JavaCompile) {
options.bootstrapClasspath = files(
newFile("./system_libs/framework.jar").path,
newFile("./system_libs/libcore.jar").path
)
}
}
Add a task to put referring to the Android API Platform in the last position in app.iml, this way gradle will take into account your system libs first and Android SDK last:
preBuild {
doLast {
defimlFile= file(project.name + ".iml")
println 'Change ' + project.name + '.iml order'try {
defparsedXml= (newXmlParser()).parse(imlFile)
defjdkNode= parsedXml.component[1].orderEntry.find { it.'@type' == 'jdk' }
parsedXml.component[1].remove(jdkNode)
defsdkString="Android API " + android.compileSdkVersion.substring("android-".length()) + " Platform"newNode(parsedXml.component[1], 'orderEntry', ['type': 'jdk', 'jdkName': sdkString, 'jdkType': 'Android SDK'])
groovy.xml.XmlUtil.serialize(parsedXml, newFileOutputStream(imlFile))
} catch (FileNotFoundException e) {
// nop, iml not found
}
}
}
Based on @Bertrand's answer
Solution 5:
You can do this automatically, just like in Eclipse:
File
> Project structure...
> (select app in Modules)
> (go to Dependencies tab)
> reposition with arrows on the right
Another way is to edit the [AppName].iml file in the folder your application is in. What you want to change are the tags at the end of the file. However, Android Studio will rearrange those each time you clean or re-open the project.
Post a Comment for "How To Put My Libraries In Front Of Android.jar By Editing Build.gradle In Android-studio"