[英]Pull jars to libs folder from other directory before building the Android application
我有一个 Android 项目,它依赖于外部 jar 文件,即A.jar
。
我已经配置了我的 android build.gradle
来首先构建构建A.jar
的项目。 然后 Android 构建继续进行。
在 jar 构建之后,将 jar 从其构建文件夹复制到 android 项目 lib 文件夹的最佳方法是什么?
所以构建应该继续......
构建 Jar > 将 Jar 复制到 Libs > 构建 Android 项目。
我不使用 Android Studio,所以我只通过 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')
}
由于您没有使用 Android Studio 并且依赖模块位于其他一些项目正在使用的其他位置,因此您可以考虑在依赖模块完成构建并创建 jar 后将 jar 复制到libs
目录中被复制。 所以整体执行如下:
libs
目录。compile files('libs/jar-from-dependency.jar')
构建 jar compile files('libs/jar-from-dependency.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
这是与此相关的必要功能的文档。
对于任务 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:在您的dependencies
部分添加 jar。
dependencies {
// ... Other dependencies
compile files('libs/jar-from-dependency.jar')
}
最简单的方法可能是删除直接模块依赖项:
// implementation project(':dawcore')
a) 并设置一个本地flatDir
存储库,Android Studio 会将其列为“Library Repository”:
repositories {
flatDir { dirs "/home/user/project/dawcore/build/outputs/aar" }
}
那么可以依赖库模块的工件:
dependencies {
implementation "com.acme.dawcore:dawcore-debug:1.0.0@aar"
// implementation "com.acme.dawcore:dawcore-release:1.0.0@aar"
}
b) 或将libs
目录设置为flatDir
:
repositories {
flatDir { dirs "libs" }
}
并将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.