gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:19:32 26 4
gpt4 key购买 nike

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

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

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

所以构建应该按照...

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

我不使用 Android Studio,所以我只通过 gradle 配置文件操作来配置我的项目。

当前构建 jar 的项目已经在 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 复制到您的 libs 目录中依赖模块完成构建并创建要复制的 jar。所以整体执行如下:

  • 任务 1. 构建依赖模块
  • 任务 2. 将 jar 文件复制到特定位置以供您的 android 应用程序使用。
  • 任务 3. 在构建您的 Android 应用程序时,将 jar 文件从该特定位置复制到您的 libs 目录。
  • 任务 4. 使用 compile files('libs/jar-from-dependency.jar')
  • 构建 jar

现在执行任务 1 和 2:在依赖模块的 build.gradle 文件中添加以下内容。因此,在构建依赖模块之后,这会将 jar 复制到复制任务中指定的位置。查看下面的复制函数,了解如何在 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

这是 doc用于与此相关的必要功能。

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

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 任务。

因此对于任务 4:将 jar 添加到您的 dependencies 部分。

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

关于java - 在构建 Android 应用程序之前将 jars 从其他目录拉到 libs 文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54621869/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com