Use provided/compileOnly dependency with Android libraries - java

For a project, I have this type of structure :
--- app => my Android application
|
\- lib => my Android library
The application use methods from the lib, but I want a build without embedding classes from the lib (since they are also available in a custom AOSP build), which could be done with provided/compileOnly directive for dependencies. The problem is that this directive doesn't work with aar librairies generated by the Android plugin, only with jars.
I found a way to build also jar files by using the custom makeJar task found here :
Build library jar with Gradle
Then in the build.gradle for the application, I can add a dependency like this :
dependencies {
compileOnly fileTree(dir: '../lib/build/outputs/jar', include: ['*.jar'])
...
}
This works fine, but only if the jar has already been generated by a previous build, which means I have to launch ./gradlew build two time to make it work.
I suppose the problem is that unlike regular dependencies (ie compileOnly project(':lib')), the fileTree dependency is evaluated without requesting a build of the sub project and on the first build, no jar file is added to the classpath.
I tried things like preBuild.dependsOn(':lib:makeJar') but without success because it's executed after evaluation.
I can't switch lib to Java plugin because it's Android specific code and I need to build aar for other projects, so is there a better way to use the compileOnly directive with this module ? Or maybe a way to force the lib to be built before the app build.gradle is evaluated ?

add this on your build.gradle(app)
compile fileTree(include: ['*.jar'], dir: 'libs')
or directly compile your jar file as following
compile files('libs/your_filename_in_libs_folder.jar')

Related

How to create *.aar Android library with gradle

I would like to create my own library .aar library file, and add it to different projects as a dependency in gradle. Also, how can I add *.aar library with own gradle file in local repository?
If you are planning to release the lib, it will be better not to include the dependent libraries in the packaged aar and instead add the same compile dependencies found in lib build script inside the build script of the app as such:
app build.gradle:
dependencies {
compile ':my-lib'
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.facebook.android:facebook-android-sdk:4.14.1'
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.google.code.gson:gson:2.8.1'
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
compile 'com.google.android.gms:play-services:11.0.4'
}
That way users of your library won't face merging conflicts when they use public libraries like your library does, gradle will automatically resolve them.
If you are looking looking for a better way to include all dependencies in a single library: Don't. Gradle cannot resolve merging conflicts if a aar contains a certain lib packaged inside while the app as well depends on the lib and contains code calling methods from lib
You can read more details here: Exclude jar-library from aar
And here Generate AAR file with all dependencies
So, I guess you should move your .aar file to lib directory in the project folder (create it if necessary). After that in your build.gradle file in dependency section write like that
dependencies {
...
implementation(name: 'your-library-name', ext: 'aar')
...
}

How to add EasyPost Library to Android Studio Project

I have downloaded the library which is a folder containing several files from:
https://github.com/EasyPost/easypost-java/archive/master.zip
I have added my own folder named myLibs and added the unzipped project folder to it (folder named easypost-java-master).
My settings.gradle looks as follows:
include ':app'
include ':myLibs:easypost-java-master'
My build.gradle looks as follows:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile files('libs/gson-2.2.4.jar', 'libs/gson-2.2.4-javadoc.jar', 'libs/gson-2.2.4-javadoc.jar')
compile fileTree(dir: 'myLibs', include: ['easypost-java-master'])
}
I am getting no errors and able to sync. But when I try to import for example import com.easypost.EasyPost;at my MainActivity, I get the error
Cannot resolve easypost
Am I missing a step?
Am I missing a step?
Yes, the BIG one. On EasyPost library GitHub profile, there are installation instructions. Did you notice that:
Installation
mvn package or build the jar from src!
To do it, just follow these steps:
In the latest build of Android Studio 1.2, the creation of JAR library
has been made as simple as point and click.
Steps to follow :
Goto File -> New -> New Module
Select "Java Library" at the end of the options list
Enter the name of the jar lib and name of class in it and hit finish button
Thats it !
The next step is adding your Jar Library as dependency in your app.
Simple as that just
Goto File -> Project Structure -> Select 'app' -> Select 'Dependency'
Select the '+' at the bottom -> Select 'Module Dependency'
Select your jar lib module you just created above
Select Ok and thats it!
....Or you could just add the below line to your App gradle file
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) // Default Gradle Task, should be already present
compile 'com.android.support:appcompat-v7:21.0.3' // Default Gradle Task, should be already present
compile project(':nameOfYourJarLibraryModule') // This is your jar library module
}
Google is promoting the Android Archive(AAR), even though JAR
supported is brought back to android studio.To find out the difference
between AAR and JAR refer this link
From: Create an Android Jar library for distribution
Hope it help

How to use external Jars in javafxports-Application

i write a javafx andoid application in netbeans with javafxports and gradle. I added the dependencies to gradel, but now i dont know how to add the jars to my project or to use it in my app-code. . .
Do you know how i can us it? I tried searching the www for hours ...
Ok i tried it but i dont get it ...
I did exactly what you said but netbeans still says:
package io.netty.bootstrap does not exist
I created a folder unter src/android/ called libs and add my jar there ...
Here are my dependencies:
dependencies {
compile fileTree(dir: 'src/android/libs', include: ['*.jar'])
compile files('src/android/libs/netty-all-4.0.29.Final.jar')
}
FINAL SOLUTION:
You have to add: compile 'io.netty:netty-all:4.0.24.Final' to the build.gradle file. (Example for netty JAR-Libary)
I copy the Libary (netty) to an Folder called "libs" in my main Folder not in sry and so on. Create the folder if not exist
Write your code and you will see, import works.
Thank you to José Pereda for the time and the final solution!
Based on the edited question, these are a few suggestions for working with dependencies on a JavaFXPorts project.
Dependencies and build.gradle file
According to this, the default dependency configurations compile and runtime are supported, and the jfxmobile plugin adds extra configurations for each supported platform like androidCompile or desktopRuntime.
To access third party dependencies, from a given repository this should be added:
repositories {
jcenter()
}
dependencies {
compile 'groupId:artifactId:version'
}
Since jcenter() is a superset of 'mavenCentral()`, you can use any maven dependency that was in the form of:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.json</artifactId>
<version>1.0.4</version>
</dependency>
as compile 'groupId:artifactId:version'. So in this case:
dependencies {
compile 'org.glassfish:javax.json:1.0.4'
}
Local files
Accesing local jars can be done using files:
dependencies {
compile files('lib/my-jar.jar')
}
having my-jar.jar at a lib folder inside your project, but outside the src folder.
If you want to add several jars:
dependencies {
compile fileTree(dir: 'lib', include: ['*.jar'])
}
Gluon plugin for NetBeans
After any change in the build.gradle file, it is necessary to reload the project, so the new changes are taken into account, and the new dependencies are retrieved.
Under the Projects view, right click on the Project root and select Reload Project.
Check also the Dependencies folders, those should contain the jars included in the build.
Since there are several of these folders, you can see for instance that Compile for android includes android.jarand jfxdvk-8u60-b3.jar. Compile for main should contain all the jars defined for compile.
Samples
These are some projects where the build.gradle contains dependencies, so they are a good way to start with JavaFXPorts.
HelloPlatform and other samples.
2048FX
SMSTracker
HelloCharm

Android Studio cannot find AAR packages

I am having a problem with Android Studio recognizing classes inside an #aar library imported locally.
So... I've made a library and exported is an aar file. Inside android studio I selected Import Module and them Import .JAR or .AAR Package.
The project compiles and works with the classes inside the aar file but Android studio can not find the classes or offer any auto completion of so all.
Here is a few of screenshots:
The same problem also happens with other #aar libraries imported the same way:
Any suggestions?
Edit:
build.gradle:
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':UpPlatformSdk')
compile project(':simpleorm')
... // more libraries here
}
settings.gradle:
include ':app', ':UpPlatformSdk', ':wear', ':simpleorm'
Looks like you could do this How to manually include external aar package using new Gradle Android Build System
If you have them in your lib folder
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile(name:'UpPlatformSdk', ext:'aar')
}
This may not be the quickest way, but this works for autocompletion and also solved my problem of missing classes when I tried compiling my local AAR using the method #puj described. Essentially you need to create a local Maven repository to host the AAR, but any changes you make are pulled by the build system when you do a Gradle sync.
Android Library AAR depending on another library

java.lang.NoSuchMethodError on compile

I'm trying to compile an Android project unsuccessfully. The error message is:
Execution failed for task ':mobile:_compileAppDebug'.
java.lang.NoSuchMethodError: com.google.auto.common.MoreTypes.asTypeElements(Ljavax/lang/model/util/Types;Ljava/lang/Iterable;)Lcom/google/common/collect/ImmutableSet;
Here are my module's gradle dependencies in which I specify a number of libraries including google Auto:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(':library')
compile 'com.google.dagger:dagger:2.0-SNAPSHOT'
provided 'com.google.auto.value:auto-value:1.0-rc1'
apt 'com.google.dagger:dagger-compiler:2.0-SNAPSHOT'
provided 'org.glassfish:javax.annotation:10.0-b28'
compile 'com.jakewharton:butterknife:6.1.0'
compile 'com.f2prateek.dart:dart:1.1.0'
}
When I looked at the dependencies I thought I just needed google auto value since that is where the missing method resides but adding the provided does not resolve the issue.
The project gradle file includes the retrolambda plugin
dependencies {
classpath 'me.tatarka:gradle-retrolambda:2.5.0'
classpath 'com.android.tools.build:gradle:1.0.1'
classpath 'com.jakewharton.sdkmanager:gradle-plugin:0.12.+'
classpath 'io.fabric.tools:gradle:1.+'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
}
Can anyone help me identify which dependencies cause the compile error? Interestingly enough, when I copy the gradle files into an empty project everything runs fine.
Dagger 2.0-SNAPSHOT depends on an Auto SNAPSHOT which had an API change: https://github.com/google/dagger/issues/113
This is perfectly normal and acceptable thing for libraries which are under development. If you cannot tolerate an occasional broken build, do not depend on non-release versions in a manner that can change at any time without warning.
I ran in a similar issue. Some libary I'm using bundles Guava within the jar file.
Thus exluding this specific dependency from the apt configuration fixed the problem:
configurations {
apt.exclude module: 'artifactId-Of-Library'
}

Categories

Resources