简体   繁体   English

在构建 Android 应用程序之前将 jars 从其他目录拉到 libs 文件夹

[英]Pull jars to libs folder from other directory before building the Android application

I have an Android project, that is dependant on an external jar file, ie A.jar .我有一个 Android 项目,它依赖于外部 jar 文件,即A.jar

I have configured my android build.gradle to first build the project that builds A.jar .我已经配置了我的 android build.gradle来首先构建构建A.jar的项目。 Then the Android build proceeds.然后 Android 构建继续进行。

What is the best way to copy the jar from its build folder into the android projects lib folder, after the jar builds?在 jar 构建之后,将 jar 从其构建文件夹复制到 android 项目 lib 文件夹的最佳方法是什么?

So the build should proceed as...所以构建应该继续......

Build Jar > Copy Jar to Libs > Build Android Project.构建 Jar > 将 Jar 复制到 Libs > 构建 Android 项目。

I don't use Android Studio, so I configure my project only through gradle config file manipulation.我不使用 Android Studio,所以我只通过 gradle 配置文件操作来配置我的项目。

The project that currently builds the jar is already listed as a dependency in app/build.gradle当前构建 jar 的项目已经在app/build.gradle列为依赖app/build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "saf.mobilebeats2"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        debug {
            applicationIdSuffix ".debug"
            debuggable true
        }
    }
}

dependencies {
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    implementation 'com.android.support:appcompat-v7:26.0.0'
    implementation project(':dawcore')
    // runtime files('../DawCore/build/libs/DawCore.jar')
}

As you are not using Android Studio and the dependency module is located in some other place where it is being used by some other projects, you might consider having the jar copied in your libs directory after the dependency module finishes the build and creates the jar to be copied.由于您没有使用 Android Studio 并且依赖模块位于其他一些项目正在使用的其他位置,因此您可以考虑在依赖模块完成构建并创建 jar 后将 jar 复制到libs目录中被复制。 So the overall execution is as follows:所以整体执行如下:

  • Task 1. Build the dependency module任务 1. 构建依赖模块
  • Task 2. Copy the jar file to a specific location to be used by your android application.任务 2. 将 jar 文件复制到您的 android 应用程序要使用的特定位置。
  • Task 3. While building your Android application, copy the jar file from that specific location to your libs directory.任务 3. 在构建您的 Android 应用程序时,将 jar 文件从该特定位置复制到您的libs目录。
  • Task 4. Build the jar using compile files('libs/jar-from-dependency.jar')任务 4. 使用compile files('libs/jar-from-dependency.jar')构建 jar compile files('libs/jar-from-dependency.jar')

Now for Task 1 & 2: add the following in the build.gradle file of your dependency module.现在对于任务 1 和 2:在依赖模块的build.gradle文件中添加以下内容。 Hence after building the dependency module, this will copy the jar to a location specified in the copy task.因此,在构建依赖模块后,这会将 jar 复制到复制任务中指定的位置。 Check the copy function below to get an idea on how to write a copy function in gradle.检查下面的复制功能以了解如何在 gradle 中编写复制功能。

apply plugin: 'java'

task finalize << {
    println('Here use the copyTask to copy the jar to a specific directory after each build of your library module')
}

build.finalizedBy(finalize)
// compileJava.finalizedBy(copyJarToAndroid) <-- In your case

Here is the doc for necessary functions related to this.这是与此相关的必要功能的文档

For Task 3: Now the task is simple.对于任务 3:现在任务很简单。 You need to copy the jar from that specific location into your Android application project before building with gradle.在使用 gradle 构建之前,您需要将 jar 从该特定位置复制到您的 Android 应用程序项目中。 Here's how you can initiate a copy task before building your project.以下是在构建项目之前启动复制任务的方法。

task copyFiles(type: Copy) {
    description = 'Copying the jar'
    from 'your/jar/directory'
    into project(':Path:To:ModuleFrom:Settings.gradle').file('./libs')
    include 'jar-from-dependency.jar'
}

project.afterEvaluate {
    preBuild.dependsOn copyFiles
}

clean.dependsOn copyFiles
clean.mustRunAfter copyFiles

This will run the copyFiles task when the gradle clean is initiated.这将在启动 gradle clean 时运行copyFiles任务。

Hence for Task 4: Add the jar in your dependencies section.因此对于任务 4:在您的dependencies部分添加 jar。

dependencies {
    // ... Other dependencies
    compile files('libs/jar-from-dependency.jar')
}

the most easy might be to remove the direct module dependency:最简单的方法可能是删除直接模块依赖项:

// implementation project(':dawcore')

a) and set up a local flatDir repository, which Android Studio will list as a "Library Repository": a) 并设置一个本地flatDir存储库,Android Studio 会将其列为“Library Repository”:

repositories {
    flatDir { dirs "/home/user/project/dawcore/build/outputs/aar" }
}

then one can depend on the artifacts of the library module:那么可以依赖库模块的工件:

dependencies {
    implementation "com.acme.dawcore:dawcore-debug:1.0.0@aar"
    // implementation "com.acme.dawcore:dawcore-release:1.0.0@aar"
}

b) or set up the libs directory as flatDir : b) 或将libs目录设置为flatDir

repositories {
    flatDir { dirs "libs" }
}

and add a Copy task to the library's build.gradle :并将Copy任务添加到库的build.gradle

tasks.whenTaskAdded { task ->
    if (task.name == 'assembleDebug' || task.name == 'assembleRelease') {
        task.finalizedBy copyArtifacts
    }
}

// dawcore:copyArtifacts
task copyArtifacts(type: Copy) {
    from("$buildDir/outputs/aar") {
        include "*.aar"
    }
    from("$buildDir/outputs/jar") {
        include "*.jar"
    }
    into file("${rootProject.projectDir}/mobile/libs")
    doLast {
        def path = ant.path {
            fileset(dir: "${rootProject.projectDir}/mobile/libs", includes: "*.aar, *.jar")
        }
        path.list().each {
            println it
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM