How do I build jar file in Android studio using gradle? - android

I want to build dependency.
I have gcm-src.jar file.and i want to show compile files('libs/gcm-src.jar') in app build.gradle.

Use this in your gradle file
// For making jar
task clearJar(type: Delete) {
delete 'build/libs/nameOfYourJar.jar'
}
task makeJar(type: Copy) {
from('build/intermediates/bundles/release/')
into('release/')
include('classes.jar')
rename('classes.jar', 'nameOfYourJar.jar')
}
makeJar.dependsOn(clearJar, build)

Related

Unable to get transitive dependencies of my library in my Android app

I created an Android Library in Android studio, which has some external dependencies(Retrofit, for example).
But when i tried to use this Library in an Android app, The app doesn't include the transitive dependencies(the ones included in the library).
I've already tried publishing the library to Bintray, change the 'implementation' keyword in app-gradle file to 'api'.
I've also tried setting transitive = true in my app's gradle file
When trying to build the Android app, It shows Resource Linking Failed for CardView which's used in my Library.
I faced the same problem while building a Library for Android.
I was trying to directly upload the .aar file from the android studio to the bintray. But appparantly, it was not including the pom.xml file.
So I followed this tutorial to generate the zip file.
Then create a new version at bintray and then manually upload the zip file using the UI upload option
Also, do not forget to check the explode this Archive option while uploading.
Your can then, publish the library and use it with all its transitive dependencies.
Here's an example build.gradle code:
def version = 'your.version'
def localReleaseDest = "${buildDir}/release/${version}"
uploadArchives {
repositories.mavenDeployer {
pom.groupId = 'your.package.name'
pom.artifactId = 'yourModuleName'
pom.version = 'your.version'
// Add other pom properties here if you want (developer details / licenses)
repository(url: "file://${localReleaseDest}")
}
}
task androidJavadocs(type: Javadoc) {
source = android.sourceSets.main.java.srcDirs
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
}
task androidJavadocsJar(type: Jar, dependsOn: androidJavadocs) {
classifier = 'javadoc'
from androidJavadocs.destinationDir
}
task androidSourcesJar(type: Jar) {
classifier = 'sources'
from android.sourceSets.main.java.srcDirs
}
artifacts {
archives androidSourcesJar
archives androidJavadocsJar
}
task zipRelease(type: Zip) {
from localReleaseDest
destinationDir buildDir
archiveName "release-${version}.zip"
}
task generateRelease {
doLast {
println "Release ${version} can be found at ${localReleaseDest}/"
println "Release ${version} zipped can be found ${buildDir}/release-${version}.zip"
}
}
generateRelease.dependsOn(uploadArchives)
generateRelease.dependsOn(zipRelease)
put this code outside all blocks in your app level gradle after modifying it as needed.
Now, sync project and open terminal inside the Android studio, and execute this command:
./gradlew clean build generateRelease
this will generate a .zip file in your app/build directory which you can upload to bintray like described above

Obfuscated interface parameter names in gradle library

I have made a library with some interfaces (HybridMediaPlayer on github). When I import it from gradle in new project and use those interfaces I get changed parameter names such as:
player.setOnPositionDiscontinuityListener(new ExoMediaPlayer.OnPositionDiscontinuityListener() {
#Override
public void onPositionDiscontinuity(int i, int i1) {
}
});
Where the "i" is "reason" and "i1" is "currentWindowIndex".
Why it is changed in other project and how to fix that? Proguard is disabled.
I think this is because on the Maven repo only a Android Archive Library (AAR) exists and no source jar. The AAR only contains the compiled classes and in compiled classes the full variable name is not known anymore. So when your IDE implements the method it doesn't know the names anymore so it defaults to standard naming based on argument types (hence the i for integer).
If you want the correct variable names you should publish a source jar of your project as well to the Jitpack repo. This answer might provide a way to also publish a source jar next to the AAR. When a source jar is also published an IDE will use Gradle to also pull the source jar into the project and will use this when implementing methods to get argument names and such.
The solution for jitpack was adding javadoc for interfaces and this in lib gradle build file:
// build a jar with source files
task sourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
classifier = 'sources'
}
task javadoc(type: Javadoc) {
failOnError false
source = android.sourceSets.main.java.sourceFiles
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
classpath += configurations.compile
}
// build a jar with javadoc
task javadocJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives sourcesJar
archives javadocJar
}
There are two reasons why this could happen:
1. You haven't included any documentation as artifact in your AAR
If this the case then you need to add following tasks in AAR's build.gradle as follows:
task javadoc(type: Javadoc) {
description "Generates Javadoc for ${archivesBaseName}-${version} API"
group JavaBasePlugin.DOCUMENTATION_GROUP
title = "${archivesBaseName}-${version} API References"
source android.sourceSets.main.java.srcDirs, configurations.doc.collect { zipTree(it) }
classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
options {
memberLevel = JavadocMemberLevel.PUBLIC
linksOffline('http://developer.android.com/reference/', "${android.sdkDirectory}/docs/reference");
}
include '<path of your public api>/*.java'
exclude '**/BuildConfig.java'
exclude '**/R.java'
exclude '**/test/**/*.java'
failOnError false
}
task androidJavadocsJar(type: Jar, dependsOn: javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir
}
artifacts {
archives androidJavadocsJar
}
2. You have already done step 1
If this is the case you need to inform android studio to download the javaDoc. Place following code in your target app's main build.gradle
apply plugin: 'idea'
...
idea {
module {
downloadJavadoc = true
downloadSources = true
}
}
Alternatively, you can follow this SO to do it through android studio.
Note: You need to properly document the Javadoc download in User guide of your AAR so that user of your AAR knows how to overcome the impediment

configurations.compile is empty

I have been using the following task to copy libraries:
task copyLibs(type: Copy, dependsOn: 'cleanLibs') {
from configurations.compile
into 'libs'
}
But with newer version of gradle, the compile configuration was replaced with implementation. I think this is why the configurations.compile list is now empty. I've tried referencing configuration.implementation and configuration.implementation.resolvedConfiguration directly, but this produces the following error:
Resolving configuration 'implementation' directly is not allowed
If I try to use configurations.compile.resolvedConfiguration I get org.gradle.api.internal.artifacts.ivyservice.ErrorHandlingConfigurationResolver$ErrorHandlingResolvedConfiguration#5bb68e71.
How can I achieve the expected result (copy all dependencies to 'libs' folder)?
Try configurations.compileClasspath instead.
This worked for me with Gradle 4.5.1:
task listJars {
doLast {
configurations.compileClasspath.each { println it }
}
}

How to export library to Jar which include some jcenter library like this compile 'com.lzy.net:okgo:3.0.4'

task makeJar(type: Copy) {
delete 'build/libs/ble_sdk.jar'
from('build/intermediates/bundles/release/')
into('build/libs/')
include('classes.jar')
rename('classes.jar', 'ble_sdk.jar')
}
makeJar.dependsOn(build)
and i want to include some jcenter libraries how to Embedded them?? help

use packageReleaseJar in depends on

I have a Gradle task that depends on other tasks. For instance:
//Dependent tasks will be executed first before executing requested task
makeJar.dependsOn(clearJar)
makeJar and clearJar are the task which I defined as follow:
task clearJar(type: Delete) {
delete 'build/outputs/myProject.jar'
}
task makeJar(type: Copy) {
def someString = 'build/intermediates/bundles/release/'
from(someString)
into('build/outputs/')
include('classes.jar')
rename ('classes.jar', 'myProject.jar')
}
I want to add another dependency to makeJar task. Gradle has a task called packageReleaseJar which I want to use.
Following script fails:
makeJar.dependsOn(clearJar,packageReleaseJar)
Do you know how can I use packageReleaseJar using dependsOn ?
This worked for me:
makeJar.dependsOn(clearJar, ':ModuleName:packageReleaseJar')
We can also add dependsOn when we define the task such as :
task makeJar(type: Copy, dependsOn: ':ModuleName:packageReleaseJar') {
...
}

Categories

Resources