[英]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.