Not able to access source code of AAR file build in Kotlin - android

I build a module in Kotlin, then import in my project. I put minifyEnabled false in module's build.gradle file. But unable to see the source code or unable to put a breakpoint.
It's working with Java but not with Kotlin.

Maybe it's still helpful for someone.
To see the Kotlin sources you'll need to have a sources.jar, besides the .aar file.
To do that you can add the following in the .gradle file where you make your maven publishing
task androidSourcesJar(type: Jar) {
archiveClassifier = 'sources'
from (android.sourceSets.main.java.sourceFiles, android.sourceSets.main.kotlin.sourceFiles)
}
artifacts {
archives androidSourcesJar
}
This will generate a sources.jar file in your <<library name>>/build/libs.
Then you can include this jar file along with your .aar library file.
It seems like Android Studio knows how to download the sources for .jar automatically, but if not, you can download them and then use the Choose sources option in the toolbar above the code.

Related

Dependency error when using aar library

I have a module with a .aar file in libs folder. I used the solution posted here
[1]: http://kevinpelgrims.com/blog/2014/05/18/reference-a-local-aar-in-your-android-project/ to add the .aar file as dependency and was able to compile the module properly.
Now I want to use this module as a dependency to the main module in my project and compile. However when i try to compile, i do see an error which says that gradle was not able to find the particular .aar file. why would my main module not find a file which is in the libs folder of my sub module. Was wondering if anyone came across this issue.
my project structure is like this
--mainmodule
--build.gradle (submodule as a dependency)
--submodule
--libs
-- abc.aar
Here is the error gradle throws: When unzipping library ':abc:, either group, name or version is empty
If I understand your problem right and you've followed the steps described in the link you shared, then adding this to your mainmodule's build.gradle should do the job:
flatDir {
dirs "../submodule/libs"
}
You basically have the same issue that you fixed in your submodule, since the mainmodule is struggling to resolve transitive dependencies (abc.aar) of submodule.
Recommended way:
While the answer above should fix your issue, Android Studio supports a better way to do this. Import a local aar file via the File>New>New Module>Import .JAR/.AAR Package option in Android Studio v1.3+.
You can then have your submodule depend on that aar-module as follows:
dependencies {
compile project(':aar-module')
}

Include external module in Android Library aar

I have a simple enough Android Library build.gradle file (irrelevant parts removed),
apply plugin: 'com.android.library'
repositories {
maven { url 'https://dl.bintray.com/alexeydanilov/maven' }
}
dependencies {
compile 'com.danikula:videocache:1.0.1'
}
And I would like to be able to build an Android Library .aar which has all the files I want in it, but also all the files which come down from the videocache external module.
I've tried many different techniques to try and achieve this (another project, changing settings of 'transitive', attempting 'export = true') but all have proven unsuccessful and I'm not sure what else I can try.
If I download the source .jar file drop it in to libs, add the necessary bits to the settings file, it packages into the .aar correctly, but I can't seem to find any way to do it via referencing the external module like this.
First of all it is not recommended to include external lib into aar file. A previous answer for this can be found here:
I quoted the text as below:
Using an artifact will not include it in the aar. The whole point of
using remote artifacts is to not rely on jars but instead on the
artifact adress so that the project using your 'aar' can resolve all
its dependency graph, find duplicates, resolve conflicts, etc...
If you publish your 'aar' on Maven, the artifact POM will contain the
dependencies. If you use it from a multi-project setup, the project
generating the 'aar' will send those dependencies to projects
referencing it.
For local jars, because those are no ways of knowing what the jar file
is we have to package it locally, but this is really not something you
should use if you are going to submit the 'aar' to an artifact repo.
So if you want to include and use your 'aar' lib from remote repo, you can publish it first and then add this line to your dependencies:
compile 'Replace with the link to your lib'
If you want to use the 'aar' file from a local place, there is also a not so perfect but working way to include all the external libs, which is simply copy this line:
compile 'com.danikula:videocache:1.0.1'
to dependencies in the project which is using your lib. But anyhow this is not recommended.
Hope my answer can help you.

Where do you set the class path in a cordova cli plugin build

I am putting together an android plugin for cordova using the cli that uses a third party aar and am getting symbol does not exist errors.
As best that I can work out it is because cordova's build needs to be told that there is an an aar in libs. It needs to be included on the classpath.
Normally any configuration changes required by a plugin would be made in plugin.xml. Does anyone know what I need to set to tell cordova about the aar.
(I have checked and the aar is actually in project/libs as a result of the plugin line)
<source-file src="libs/android/mopub-sdk.aar" target-dir="libs" />
or is my problem a little deeper and its not a plugin.xml problem - its something I need to configure in cordova cli?
Okay, I have a similar problem. What works for me was adding this .aar to a folder named 'aars' inside the root/platforms/android folder. And also creating the build-extras.gradle file (you have to create this file on the root/platforms/android folder) with the following code:
ext.postBuildExtras = {
android {
repositories {
mavenCentral()
flatDir {
dirs 'aars'
}
}
dependencies {
compile 'io.filepicker:filepicker-android:3.8.14#aar'// IN YOUR CASE, PUT THE CORRECT NAME THAT MATCHS YOURS AAR
}
}
Now, when it compiles, it gets the .aar and set up in the build path.
If yours aar uses a thrid library, you should include (copy/paste to libs) folder. You have to make this to include those libraries on the build-path so it will be on the .apk when you generated.
Now, you just need to import the /platforms/android folder in the eclipse, and then click with the right button on the project>properties>JAva build path>order and export. Now you just need to check Android private libraries and you're good to go.
Hope it helps you.

Custom gradle project with jar output

I want to have a gradle "project" that creates a jar using a custom task (doesn't use any of the gradle java plugins). What is the proper way to construct the build.gradle so that I can depend on it in my android app's gradle project?
More Details:
Currently my jar file is being created in a cmake project that I call from gradle.
I want to have a proper build.gradle file that calls my cmake command then uses the resulting jar as the artifact. Then I would like to be able to simply add "compile project(':mylib') from my android project's build.gradle.
you can make a jar task like:
task myJar(type:Jar) {
archiveName='my.jar'
from sourceSets.main.output
include 'foo/com/**/*', 'bar/com/**/*'
}

How to export library to Jar in Android Studio?

I have downloaded some library sources and would like to export it as a Jar file using
Android Studio. Is there a way to export to jar file using Android studio ?
edit:
The library I want to export as jar is an Android library.
It's called "StandOut" and can be downloaded from GitHub.
https://github.com/pingpongboss/StandOut
It is not possible to export an Android library as a jar file. It is possible, however, to export it as aar file. Aar files being the new binary format for Android libraries. There's info about them in Google I/O, the New Build System video.
First, build the library in Android Studio or from command line issuing gradle build from your library's root directory.
This will result in <yourlibroot>/libs/build/yourlib.aar file.
This aar file is a binary representation of your library and can be added to your project instead of the library as a dependency project.
To add aar file as a dependency you have to publish it to the maven central or to your local maven repository, and then refer the aar file in your project's gradle.build file.
However, this step is a bit convoluted. I've found a good explanation how to do so here:
http://www.flexlabs.org/2013/06/using-local-aar-android-library-packages-in-gradle-builds
I was able to build a library source code to compiled .jar file, using approach from this solution:
https://stackoverflow.com/a/19037807/1002054
Here is the breakdown of what I did:
1. Checkout library repository
In may case it was a Volley library
2. Import library in Android Studio.
I used Android Studio 0.3.7. I've encountered some issues during that step, namely I had to copy gradle folder from new android project before I was able to import Volley library source code, this may vary depending on source code you use.
3. Modify your build.gradle file
// If your module is a library project, this is needed
//to properly recognize 'android-library' plugin
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.3'
}
}
apply plugin: 'android-library'
android {
compileSdkVersion 17
buildToolsVersion = 17
sourceSets {
main {
// Here is the path to your source code
java {
srcDir 'src'
}
}
}
}
// This is the actual solution, as in https://stackoverflow.com/a/19037807/1002054
task clearJar(type: Delete) {
delete 'build/libs/myCompiledLibrary.jar'
}
task makeJar(type: Copy) {
from('build/bundles/release/')
into('build/libs/')
include('classes.jar')
rename ('classes.jar', 'myCompiledLibrary.jar')
}
makeJar.dependsOn(clearJar, build)
4. Run gradlew makeJar command from your project root.
I my case I had to copy gradlew.bat and gradle files from new android project into my library project root.
You should find your compiled library file myCompiledLibrary.jar in build\libs directory.
I hope someone finds this useful.
Edit:
Caveat
Althought this works, you will encounter duplicate library exception while compiling a project with multiple modules, where more than one module (including application module) depends on the same jar file (eg. modules have own library directory, that is referenced in build.gradle of given module).
In case where you need to use single library in more then one module, I would recommend using this approach:
Android gradle build and the support library
Since Android Studio V1.0 the jar file is available inside the following project link:
debug ver: "your_app"\build\intermediates\bundles\debug\classes.jar
release ver: "your_app"\build\intermediates\bundles\release\classes.jar
The JAR file is created on the build procedure,
In Android Studio GUI it's from Build->Make Project and from CMD line it's "gradlew build".
Include the following into build.gradle:
android.libraryVariants.all { variant ->
task("generate${variant.name}Javadoc", type: Javadoc) {
description "Generates Javadoc for $variant.name."
source = variant.javaCompile.source
ext.androidJar = "${android.plugin.sdkDirectory}/platforms/${android.compileSdkVersion}/android.jar"
classpath = files(variant.javaCompile.classpath.files) + files(ext.androidJar)
}
task("javadoc${variant.name}", type: Jar) {
classifier = "javadoc"
description "Bundles Javadoc into a JAR file for $variant.name."
from tasks["generate${variant.name}Javadoc"]
}
task("jar${variant.name}", type: Jar) {
description "Bundles compiled .class files into a JAR file for $variant.name."
dependsOn variant.javaCompile
from variant.javaCompile.destinationDir
exclude '**/R.class', '**/R$*.class', '**/R.html', '**/R.*.html'
}
}
You can then execute gradle with: ./gradlew clean javadocRelease jarRelease which will build you your Jar and also a javadoc jar into the build/libs/ folder.
EDIT: With android gradle tools 1.10.+ getting the android SDK dir is different than before. You have to change the following (thanks Vishal!):
android.sdkDirectory
instead of
android.plugin.sdkDirectory
I was able to export a jar file in Android Studio using this tutorial:
https://www.youtube.com/watch?v=1i4I-Nph-Cw
"How To Export Jar From Android Studio "
I updated my answer to include all the steps for exporting a JAR in Android Studio:
1) Create Android application project, go to app->build.gradle
2) Change the following in this file:
modify apply plugin: 'com.android.application' to apply plugin: 'com.android.library'
remove the following: applicationId, versionCode and versionName
Add the following code:
// Task to delete old jar
task deleteOldJar(type: Delete){
delete 'release/AndroidPlugin2.jar'
}
// task to export contents as jar
task exportJar(type: Copy) {
from ('build/intermediates/bundles/release/')
into ('release/')
include ('classes.jar')
rename('classes.jar', 'AndroidPlugin2.jar')
}
exportJar.dependsOn(deleteOldJar, build)
3) Don't forget to click sync now in this file (top right or use sync button).
4) Click on Gradle tab (usually middle right) and scroll down to exportjar
5) Once you see the build successful message in the run window, using normal file explorer go to exported jar using the path: C:\Users\name\AndroidStudioProjects\ProjectName\app\release
you should see in this directory your jar file.
Good Luck :)
Here's yet another, slightly different answer with a few enhancements.
This code takes the .jar right out of the .aar. Personally, that gives me a bit more confidence that the bits being shipped via .jar are the same as the ones shipped via .aar. This also means that if you're using ProGuard, the output jar will be obfuscated as desired.
I also added a super "makeJar" task, that makes jars for all build variants.
task(makeJar) << {
// Empty. We'll add dependencies for this task below
}
// Generate jar creation tasks for all build variants
android.libraryVariants.all { variant ->
String taskName = "makeJar${variant.name.capitalize()}"
// Create a jar by extracting it from the assembled .aar
// This ensures that products distributed via .aar and .jar exactly the same bits
task (taskName, type: Copy) {
String archiveName = "${project.name}-${variant.name}"
String outputDir = "${buildDir.getPath()}/outputs"
dependsOn "assemble${variant.name.capitalize()}"
from(zipTree("${outputDir}/aar/${archiveName}.aar"))
into("${outputDir}/jar/")
include('classes.jar')
rename ('classes.jar', "${archiveName}-${variant.mergedFlavor.versionName}.jar")
}
makeJar.dependsOn tasks[taskName]
}
For the curious reader, I struggled to determine the correct variables and parameters that the com.android.library plugin uses to name .aar files. I finally found them in the Android Open Source Project here.
We can export a jar file for Android library project without resource files by Android studio. It is also requirement what I met recently.
1. Config your build.gradle file
// Task to delete old jar
task clearJar(type: Delete){
delete 'release/lunademo.jar'
}
// task to export contents as jar
task makeJar(type: Copy) {
from ('build/intermediates/bundles/release/')
into ('build/libs/')
include ('classes.jar')
rename('classes.jar', 'lunademo.jar')
}
makeJar.dependsOn(clearJar, build)
2. Run gradlew makeJar under your project root
You will see your libs under dir as build/libs/ if you are luckily.
============================================================
If you met issue as "Socket timeout exception" on command line as below,
You can follow this steps to open Gradle window in the right part and click "makeJar" on Android studio like this,
Then go to build/libs dir, you will see your jar file.
Hope that it is helpful for u.
Good Luck #.#
Luna

Categories

Resources