How do we create an aar file that includes all its dependent projects in Android gradle build?
Say I have ProjectA that is Android gradle library that depends on another library called ProjectB which is in ant build. I would like to build an aar file thats inclusive of 'ProjectB`.
If its not possible across two different build systems.
Related
I am having my android studio project with four library modules and one application module in it.
I am having six jar files which I have kept in one directory.
Each of these modules are using some of the jar files.
So I have added dependencies of these jars in build.gradle file of respective module with relative path using "compile files('./../../deliverables/xyz.jar')".
Application module is dependent on all these library modules.
When I create release build of my application module, it fails with "duplicate zip entry" error of proguard as I have enabled proguard in gradle build script.
This error is for six jar files.
The debug build of this project is working fine.
I tried below solutions:
1. injar option in proguard-rules.pro file
2. dontwarn option in proguard-rules.pro file
3. keep option in proguard-rules.pro file
Please help me.
Thanks.
One solution is to create a libraries module whose sole purpose is to contain the .jar files. Once all the jar files that you need are in this module, you can remove them from the other modules and reference your library module in your other modules.
For example, let's say we have the app module depend on these four modules:
app
|---module1
|---module2
|---module3
|---module4
We can create a libraries module that you will reference in the modules that use at least one of the libraries contained in the module, by adding the following to the module's build.gradle:
compile project(':libraries')
I am using Android studio 1.5. I have a library project, core-speech-service.aar. This AAR file is generated correctly (all classes are built and packed inside classes.jar in the AAR file).
Now I am creating another AAR library google-speech-service.aar which depends on the first AAR (core-speech-service). So I add it as library dependency (compile) in the new AAR library project.
So far, so good. Android studio copies core-speech-service.aar in to the new library project as a library module. But when I build the google-speech-service library project, it creates an AAR file, but the packed AAR file neither contains classes from the core-speech-service.aar neither it has the core-speech-service.aar itself.
Am I missing something ?
Thanks
abhay
You will have to reference the library as an outside dependency on your apk. I believe that as of now you cannot have an aar inside another
is it possible to build an apk and an aar library with gradle from the same project source?
Challenge is to modify the build.gradle so that both products will be built. Do you have any advice?
Finally, Gradle offers me a really simple solution for this issue:
I've created a multi module project with Android Studio and Gradle (see http://www.petrikainulainen.net/programming/gradle/getting-started-with-gradle-creating-a-multi-project-build/). One module for the app (Android Application Project) and one for the library (Android Library Project).
After moving all sources, that belong to the library, to the library project, I'm able to build the .aar file and the .apk file (which uses also sources from the library project, no problem thanks to the multi module project).
I've created a project and a library separately using Android Studio, each in a separate folder inside some directory.
I tried to add the library as a module to the project, and noticed that instead of just referencing the library like in Eclipse, the library was copied inside the project directory.
That means that if this happened N times for N projects, then I'll have N copies of the library and I'll need to update them all when any update is to be done.
I'm working on v 1.0.2 of Android Studio.
Any one has a better idea to do it?
Three options I know of:
You can specify the path to the external library:
Android studio add external project to build.gradle
Include the compiled jar file from the library in the libs directory of the N apps.
Publish the artifact (the jar from library project) to a gradle repository and then you can add dependencies to that project just like you would for the support library etc.
See http://gradle.org/docs/current/userguide/artifact_management.html
all.
I've created an android library project and it's works perfectly when i reference it from main project. But when i build the library project apart it doesn't contains R.java and resources. Is there way to build a library project with resources and R.java?
It's not possible now.
Now we can create a binary-only library project via the following steps:
Create an Android library project, with your source code and such –
this is your master project, from which you will create a version of
the library project for distribution
Compile the Java source (e.g., ant compile) and turn it into a JAR file
Create a distribution Android library project, with the same
resources as the master library project, but no source code
Put the JAR file in the distribution Android library project's libs/
directory
The resulting distribution Android library project will have everything a
main project will need, just without the source code.
There is some restrictions in this solution:
We still have to ship the resources.
We have to rewrite our code to avoid using R. values, as they
will be wrong. We will have to look up all resource IDs using
getResources().getIdentifier() and/or reflection.
I use Eclipse and never manually build my Android Library Project independently, but I think the development considerations stated on official dev guide here should answer your question:
Each library project creates its own R class
When you build the dependent application project, library projects are compiled and merged with the application project. Each library has its own R class, named according to the library's package name. The R class generated from main project and the library project is created in all the packages that are needed including the main project's package and the libraries' packages.
Update with Another note quoted from the official dev giude Library Projects:
However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.