在构建Android应用程序之前,将jars从其他目录拉到libs文件夹 [英] Pull jars to libs folder from other directory before building the Android application

查看:60
本文介绍了在构建Android应用程序之前,将jars从其他目录拉到libs文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android项目,该项目依赖于外部jar文件,即 A.jar .

I have an Android project, that is dependant on an external jar file, i.e. A.jar.

我已将自己的android build.gradle 配置为首先构建构建 A.jar 的项目.然后,Android构建继续进行.

I have configured my android build.gradle to first build the project that builds A.jar. Then the Android build proceeds.

在jar生成后,将jar从其build文件夹复制到android项目lib文件夹的最佳方法是什么?

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复制到Libs>构建Android项目.

Build Jar > Copy Jar to Libs > Build Android Project.

我不使用Android Studio,因此我只能通过gradle配置文件操作来配置我的项目.

I don't use Android Studio, so I configure my project only through gradle config file manipulation.

当前构建jar的项目已在 app/build.gradle

The project that currently builds the jar is already listed as a dependency in 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')
}

推荐答案

由于您未使用Android Studio,并且依赖项模块位于其他项目正在使用的其他位置,因此您可以考虑使用jar在依赖模块完成构建并创建要复制的jar之后,将其复制到您的 libs 目录中.因此,总体执行如下:

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. So the overall execution is as follows:

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

现在执行任务1和&2:在依赖模块的 build.gradle 文件中添加以下内容.因此,在构建依赖项模块之后,这会将jar复制到复制任务中指定的位置.查看下面的复制功能,了解如何在gradle中编写复制功能.

Now for Task 1 & 2: add the following in the build.gradle file of your dependency module. Hence after building the dependency module, this will copy the jar to a location specified in the copy task. Check the copy function below to get an idea on how to write a copy function in 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.

对于任务3:现在任务很简单.在使用gradle构建之前,需要将jar从该特定位置复制到Android应用程序项目中.这是在构建项目之前可以启动复制任务的方法.

For Task 3: Now the task is simple. You need to copy the jar from that specific location into your Android application project before building with gradle. 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

这将在启动gradle clean时运行 copyFiles 任务.

This will run the copyFiles task when the gradle clean is initiated.

因此,任务4:将jar添加到 dependencies 部分.

Hence for Task 4: Add the jar in your dependencies section.

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

这篇关于在构建Android应用程序之前,将jars从其他目录拉到libs文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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