Reputation: 1840
I have an Android project, that is dependant on an external jar file, i.e. A.jar
.
I have configured my android build.gradle
to first build the project that builds A.jar
. Then the Android build proceeds.
What is the best way to copy the jar from its build folder into the android projects lib folder, after the jar builds?
So the build should proceed as...
Build Jar > Copy Jar to Libs > Build Android Project.
I don't use Android Studio, so I configure my project only through gradle config file manipulation.
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')
}
Upvotes: 2
Views: 2580
Reputation: 24211
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:
libs
directory. compile files('libs/jar-from-dependency.jar')
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.
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
This will run the copyFiles
task when the gradle clean is initiated.
Hence for Task 4: Add the jar in your dependencies
section.
dependencies {
// ... Other dependencies
compile files('libs/jar-from-dependency.jar')
}
Upvotes: 1
Reputation: 76569
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":
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
:
repositories {
flatDir { dirs "libs" }
}
and add a Copy
task to the library's 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
}
}
}
Upvotes: 0