How to put my libraries in front of android.jar by editing build.gradle in Android-Studio

前端 未结 9 807
你的背包
你的背包 2020-12-01 07:19

First Here\'s my Java Build Path in Eclipse: \"enter

These four jars \'common.jar,core

相关标签:
9条回答
  • 2020-12-01 07:24

    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 level build.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, use provided :

      dependencies {
          provided fileTree(include: ['*.jar'], dir: 'libs')
      }
      
    • in the same app build.gradle, add a task to put <orderEntry> referring to Android API 25 Platform in the last position in app.iml, this way gradle will take into account your system libs first and Android SDK in last resort :

      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
              }
          }
      }
      
    0 讨论(0)
  • 2020-12-01 07:29

    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(
                new File("./system_libs/framework.jar").path,
                new File("./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 {
            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
            }
        }
    }
    

    Based on @Bertrand's answer

    0 讨论(0)
  • 2020-12-01 07:31
    1. Make dir (ex: exlibs)
    2. Copy your jar file to exlibs dir
    3. ..

      dependencies {
              provided files("$projectDir/exlibs/yourlib.jar")
      }
      
    0 讨论(0)
  • 2020-12-01 07:34

    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.

    0 讨论(0)
  • 2020-12-01 07:35

    The simplest solution for me was to replace android.jar with one with the hidden API included. Get android.jar from this project library that provides access to Android hidden API and internal resources and place it to your ASDK platforms folder, to the platform you're compiling against (compileSdkVersion).

    I'm sure it works with Eclipse as well ))

    0 讨论(0)
  • 2020-12-01 07:38

    Update app/app.iml file order as

    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" exported="" name="common" level="project" />
    <orderEntry type="library" exported="" name="framework" level="project" />
    <orderEntry type="library" exported="" name="layout" level="project" />
    <orderEntry type="jdk" jdkName="Android API 21 Platform" jdkType="Android SDK" />
    
    0 讨论(0)
提交回复
热议问题