How to compile Android library? - java

In my project I am using SlidingUpPanel.
Due to a feature that I needed, I cloned the project to my machine and manipulated the library. I tested my feature on its demo project and everything is okay based on this project.
Now, I want to compile its library module in order to get .aar file and add it to my project. What I did is:
I opened cmd and navigated to root folder of slidingUpPanel project.
Ran this command: ./gradlew :library:build
Library compiled and created .aar under /library/build/outputs/aar/library-debug.aar
I copy/pasted this file into /libs folder of my project and updated build.gradle like this:
dependencies {
compile ANDROID_SUPPORT
compile CRASHLYTICS
compile 'com.facebook.android:facebook-android-sdk:3.20.0'
compile 'com.googlecode.libphonenumber:libphonenumber:7.0.1'
compile 'com.android.support:recyclerview-v7:+'
// Local libs not in Maven Central
compile files('src/main/libs/httpclientandroidlib-1.2.1.jar')
compile files('src/main/libs/nineoldandroids-2.4.0.jar')
compile files('src/main/libs/niftynotification-1.2.jar')
compile files('src/main/libs/zendesk-1.0.0.1.jar')
compile files('src/main/libs/library-debug.aar')
}
After I sync gradle file, I'm getting error which is because SlidingUpPanelLayout does not recognise.
Any idea why the library cannot be recognised? Any idea would be appreciated. Thanks.

So, first, lets add this kind of repository in our build.gradle file.
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
Here, I assume that your aar files will be stored in the libs folder in your project.
You can now add a dependency like
dependencies {
compile 'net.my.package:mylib:1.0#aar'
}

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')
...
}

subproject not compiling Java/Android Studio

I am trying to add a subproject to my main one in Android Studio. I have it compiling in the build gradle of the whole project. When ever I try to build the project or compile it it gives me out this error.
Error:(9, 0) Could not find method compile() for arguments [project ':subProject.exude'] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Open File
Here is the code for the build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
compile project('subProject.exude')
I think this can be a solution to your problem. Let's say there are two modules in your project, typically named as app and lib. Now you want to use lib module in your app module. So, you need to add it to your build.gradle(app module).
compile project(':lib')
I have it compiling in the build gradle of the whole project.
It may imply two cases:
You're adding compile project('subProject.exude') to your root/project build.gradle
You're adding compile project('subProject.exude') to all of your module build.gradle
In first case, you must not add the compile project to your root build.gradle. Because it not belong there.
In second case, you're incorrectly adding the classpath to your dependencies block in your module build.gradle. This is incorrect, because you're adding classpath for dependencies:
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
compile project('subProject.exude')
...
}
This is corrrect:
dependencies {
compile project('subProject.exude')
...
}
Module dependencies should not exist in root/project build.gradle.

Gradle project dependency error in Jenkins

I have a project structure similar to
//Directory structure
Root Folder/
projectA/
build.gradle
projectB/
build.gradle
properties.gradle
Now, project B is dependent on project A
The settings.gradle and build.gradle for Project B is as follows
settings.gradle
include ':ProjectA'
project(':ProjectA').projectDir = new File(settingsDir, '../ProjectA')
build.gradle
dependencies{
compile project(':ProjectA')
}
When I try to build project B on my local machine (Gradle version 3.2) it builds successfully and everything looks good.
When i try to build the same project in jenkins (same gradle version as my local), i am getting the error
Caused by: org.gradle.api.artifacts.UnknownConfigurationException: Configuration with name 'default' not found.
It looks like in jenkins, it is not able figure out the relative path.
How do I solve this?
Is there a way in jenkins I can ignore the dependency from gradle and use the pre build to compile ProjectA and put that in the classpath? If so how can we do it?
I was able to solve this by adding a jenkins property which I check in the build file
if (project.hasProperty('jenkins')) {
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
} else {
dependencies {
compile project(':ProjectA')
}
}
So when we build it locally, since we are not passing the property it is able to compile ProjectA. In Jenkins, the build job, in the gradle command I am passing the following -Pjenkins clean build. This goes to the if condition and pulls the jar from projectA and puts that in the lib directory.
This has solved the problem for me, let me know if there are better solutions for it

Gradle "compile fileTree" not working when src in module

I use IDEA.
My gradle config is this:
The point is compile fileTree(dir: 'libs', include: ['*.jar']).
And this is my project structure:
Then I found compile fileTree is not working when java file is in module 'main', but when the java file is under main project, it is working.
How can I get the config valid on module 'main'?
Oh,god!I know why.
NOTE: "dir" is relative to the project root, if you add the dependencies to your android project, 'libs' would need to be in the android/ directory. If you added the dependencies in the core project, 'libs' would need to be in the core/ directory.
So,the libs must in module "main",then is done.

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

Categories

Resources