I have two, separate Android projects. One is a regular Android application and the other one is a libgdx project.
My goal is to be able to compile the libgdx project as an Android library into aar file, so I could use it in the regular, non libgdx, Android application (I'm going to start the libgdx game's activity from the regular Android project).
The libgdx project consists of several modules (I'm using only the android and the desktop modules), so in my libgdx project I can find 3 modules: android, desktop and core (where basically the whole game's code is resides). When compiling and running the game on Android, the android module kicks in, but it uses the core module as a dependency.
When trying to change the libgdx project into an Android library project and compiling it into aar, it seems like it lacks the needed dependencies (like the core module, in addition to some other dependencies).
How can I create an aar file from the libgdx project which has all the needed dependencies?
So eventually I managed to find a solution for my problem.
Lets start with the fact that my first impression was misleading, and the problem I had was not a libgdx specific problem, but a gradle "problem".
In short, the reason behind that is that the aar/jar files don't contain the transitive dependencies and don't have a pom file which describes the dependencies used by the library.
To overcome this behaviour you need somehow to specify the dependencies in your project. You can choose between 2 approaches:
first approach:
You can use a central repository, such as JCenter, and publish the project as library. In this case, gradle will be able to download the dependencies using the pom file which contains the dependencies list.
Second approach:
You need to manually copy all your dependencies to the libs folder. You can do this relatively easy by writing a small gradle task:
task copyCompileDependenciesToLibs(type: Copy) {
def libsPath = project.projectDir.toString() + "/libs"
from configurations.compile
into libsPath
}
This snippet will copy all your dependencies to the libs folder, and once you compile your library project the dependencies will be included.
Related
I am creating SDK and want to modularize it.
And prevent internal APIs from being exposed on your public interface.
Want optional dependency based on features required. If features is not required we can remove dependency and code and imports should not work.
Tried Ways
Submodule dependencies
Fat AAR
Submodule dependencies
when a library module gets built, the .aar artifact will only include code and resources that are in the library module itself. It won’t include:
any code or resources from database and ui-components
links to its transitive dependencies (these go into the build.gradle)
So when the app module directly includes the library as a gradle dependency, it would crash due to missing classes from database and ui-components on its classpath.
Fat AAR
In the fat .aar solution, code and resources of the submodules are bundled into the main SDK module, hence creating a fat .aar
the fat .aar plugin breaks on almost every minor Android Gradle plugin update! This is because it hooks itself into particular tasks of the Android Gradle plugin and these very often get renamed/moved. However, the project maintainer need to do job at fixing those within a few weeks after the breaking change.
Also, because of the way fat .aar references dependencies from submodules, it can significantly increase the binary size of your SDK.
Is there any other possible solution?
I'm currently starting up in android dev. I have a dozen of non-sharing independent modules/folders which I'd be using in my main App Folder. Now for gradle to compile my project takes upto more than 3-5 minutes.
With some help from the web, I learnt that if by some means I can precompile my independent modules/folder into artifacts I could add them as dependancies (like adding precompiled Glide or ButterKnife dependancies) in build.gradle and circumvent recompiling again.
Now, How do I build artifacts of my own Android projects to be able to use it in build.gradle dependancies list?
I'm building some Android apps using Android Studio. Adding dependencies/libraries is really easy by going to file -> project structure -> app -> Dependencies.
But this got me thinking, what exactly is the different between dependencies and libraries and how do they compare? (what's better)
Library is a dependency. Dependency means things that your app depends on. Dependency can be a module, library or a file.
We can find some very good open source libraries for android. I want to know what is the best way to integrate them to our own projects in Android studio. Here are some basic methods:
Copy the source code and resource files into our own project. We need to change a lot of codes (the package name, and the name in xml,etc)
If jar files is provided, I just create libs folder for my project and copy the jar files inside. And add the jar file in Module setting's dependencies. But unfortunately I got a lot of error messages like "Gradle: Package com.google.gson doesn't exist".
Is there a general rule to add third party source or jar files into an existing android studio project? Thanks
I prefer to use central repository for dependencies management. So for gson 2.3 dependency you should add to build.gradle file:
Specify that you want to use maven central repository for your dependency
repositories {jcenter()}
Add compile dependency to gson 2.6.2
dependencies {compile 'com.google.code.gson:gson:2.6.2'}
Android Studio as well as your CI server should easily build your project now. And you can continue app development.
I prefer to use central repository for dependencies management because:
easier scope management - some libraries are only for testing, some should be included to apk and some are part of running environment (like android.jar itself)
easier transitive dependencies management - it is quite hard to collect libraries dependencies and if you use "jar-with-dependencies" you could get error "class already added" during dexing
lighter repository and easier dependency upgrade
Examples:
Robolectric jar should be used for unit testing only and shouldn't be part of apk itself
Repository is clean from different folders with jars, checkout takes much less. No needs to download and replace old jars with new jars
I should notice:
Not many libraries are in maven central and you should make some effort to use them such way in your project
You could much easier get to "class already added" error during dexing with central repository approach
You can mix usage of dependencies from central repository and from lib folder, but I prefer to use only one way for simplicity
Put the Gson jar (in my case, gson-2.2.4.jar) into the libs folder
Right click it and hit 'Add as library'
Ensure that compile files('libs/gson-2.2.4.jar') is in your build.gradle file
Do a clean build (you can probably do this fine in Android Studio, but to make sure I navigated in a terminal to the root folder of my app and typed gradlew clean. I'm on Mac OS X, the command might be different on your system
This series of steps was taken from Android Studio: Add jar as library? and is not my original answer. I am posting them here, again, because your question was the third in search results on Google when looking up this same topic. Hence, copying.
All credits to the one who wrote the steps.
Download & Copy Your .jar file in libs folder then adding one line to build.gradle:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar']) ----> AS creates this
compile 'com.google.code.gson:gson:2.3.4' ----------> I added this one
}
Do not forget to click "Sync now"
I´m using Android Studio 1.1.0
Download and copy your jar to libs folder then add the following to your app.gradle file and SYNC.
dependencies {
compile 'com.google.code.gson:gson:{version_you_need}'
}
repositories{
flatDir{
dirs 'libs'
}
}
I'm using the new Android build system that is based on Gradle, together with the early access preview Android Studio. Now, I have two projects: an Android library project, and an Android app project (basically a demo for the library).
In the library project I have added a dependency to the gson library, so my build.gradle file looks like this:
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile 'com.google.code.gson:gson:2.2.+'
}
Still, everything works fine and dandy and I'm able to use gson in my library and then my app. But I want to understand where this library is embedded. I've opened both the .aar that is built by the library project and the .apk of the demo app. I was expecting to find the jars for the two dependencies in at least one of these, but I didn't.
So where are they?
From Android Tools website:
These items, plus the output of the compilation of the project’s own source code, are sent to dex for bytecode conversion and inclusion in the final APK.
In other words, they are in your *.dex file inside the APK.
As #SharkyXTS said, the code from any external dependencies is compiled into the final .dex file inside your APK. The reason why you can't find any references to these dependencies in the .aar is because there aren't any.
The .aar format is only supported through Maven for now, so dependencies are found through there. I believe there are plans to eventually support local .aar dependencies (without Maven), but the Android plugin isn't quite there yet. You can see this issue for more information.