Android Library Gradle release JAR
While I haven't tried uploading the artifacts with a deployment to Sonatype (or even a local repo), here's what I managed to come up with a few weeks ago when trying to tackle the same problem.
android.libraryVariants.all { variant ->
def name = variant.buildType.name
if (name.equals(com.android.builder.core.BuilderConstants.DEBUG)) {
return; // Skip debug builds.
}
def task = project.tasks.create "jar${name.capitalize()}", Jar
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDir
artifacts.add('archives', task);
}
Then run the following:
./gradlew jarRelease
How to create an Android Library Jar with gradle without publicly revealing source code?
Note: The answer has been edited. Please see the 07/28/2014 update below.
Here is a solution I ended up coming up with. There may be a better way available, but I have not found it yet.
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
sourceSets {
main {
java {
srcDir 'src/main/java'
}
resources {
srcDir 'src/../lib'
}
}
}
}
task clearJar(type: Delete) {
delete 'build/libs/ProjectName.jar'
}
task makeJar(type: Copy) {
from('build/bundles/release/')
into('build/libs/')
include('classes.jar')
rename ('classes.jar', 'ProjectName.jar')
}
makeJar.dependsOn(clearJar, build)
Running gradlew makeJar
creates a ProjectName.jar
in the build/libs
directory. The structure of this jar is as follows:
ProjectName.jar
\- lib
| \- armeabi
| \- libNativeFirst.so
| \- libNativeSecond.so
\- com
\- package
\- sdk
\- PackageSDK.class
This is the exact result I needed. I am now able to use ProjectName.jar
successfully in other projects.
EDIT: While I am able to use the resulting jar in projects within Android Studio, I cannot do so in projects created in ADT due to a warning about native code being present inside a jar file. Supposedly there is a flag to turn off this check in settings, but it does not function correctly. Thus, if you want to create a library that uses native code, those using ADT will have to manually copy the armeabi directory into libs/.
07/28/2014 Update:
As of Android Studio 0.8.0, Gradle output directories have been changed and the configuration outlined above will not work. I have changed my configuration to the following:
task clearJar(type: Delete) {
delete 'build/outputs/ProjectName.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'ProjectName.jar')
}
IMPORTANT: Please note that ProjectName.jar
will now be placed into build/outputs/
and NOT into build/libs/
.
Build library jar with Gradle
You need to create a new gradle task, similar to mine
task clearJar(type: Delete) {
delete '../location/' + LIBRARY_NAME + '_' + LIBRARY_VERSION + '.jar'
}
task makeJar(type: Copy) {
dependsOn clearJar, build
from 'build/intermediates/bundles/release/'
into '../../location/'
include 'classes.jar'
rename 'classes.jar', LIBRARY_NAME + '_' + LIBRARY_VERSION + '.jar'
}
Where LIBRARY_NAME
and LIBRARY_VERSION
are strings I set at the top of the gradle build file.
You can add this task and then select it like this
Android Library: Release .aar getting classes.jar empty when using proguard
Ok, after some time I solved my problem.
I copy/paste a default proguard rules configuration (library.pro) to my proguard-rules.pro.
You can find this file and more examples in path-to-your-sdk/tools/proguard/examples.
For more information, read this.
In my build.gradle I chagend:
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
to:
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
consumerProguardFiles 'proguard-rules.pro' //added this line
}
}
Thanks for the help!
Publish an Android library to Maven with AAR and sources JAR
Here's a sample using the new maven-publish
plugin.
apply plugin: 'maven-publish'
task sourceJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier "sources"
}
publishing {
publications {
bar(MavenPublication) {
groupId 'com.foo'
artifactId 'bar'
version '0.1'
artifact(sourceJar)
artifact("$buildDir/outputs/aar/bar-release.aar")
}
}
repositories {
maven {
url "$buildDir/repo"
}
}
}
Publish with ./gradlew clean build publish
Related Topics
How to Get a Context in a Recycler View Adapter
How to Paginate Queries by Combining Query Cursors Using Firestorerecycleradapter
How to Make Rjava Use the Newer Version of Java on Osx
Generate All Combinations from Multiple Lists
Why Does JPA Have a @Transient Annotation
How Does Java Do Modulus Calculations with Negative Numbers
Covariance, Invariance and Contravariance Explained in Plain English
Integer Wrapper Class and == Operator - Where Is Behavior Specified
Android: Sending Data >20 Bytes by Ble
Firestore Query - Checking If Username Already Exists
How to Get Rjava 0.9-3 to Work on Os X 10.7.4 with Oracle Java 1.7
What Could Cause Java.Lang.Reflect.Invocationtargetexception
Why Call Super() in a Constructor
Getting the Filenames of All Files in a Folder