I want to create a single uber/fat aar. I have a library (libA) which is dependant on another library (libB) and I would like to bundle them together, preferably using gradle. This uber/fat aar will then be used in an app.
Initially I looked at 2 similar gradle plugins, shadowJar and fataar but due to version incompatibilities they were a no go. So to do this, I've created a custom configuration in my gradle file and added the dependencies:
configurations { privateLibs }
privateLibs ('libB.1.0.0#aar') {
transitive=true
}
compile configurations.privateLibs.asFileTree
I can then run gradle install to produce libA-1.0.0.aar.
When I inspect libA.aar and look at it's contents using
jar tvf libA-1.0.0.aar
I can see LibB listed under the libs folder:
BST 2018 libs/libB-1.0.0.aar
I've now created an app inclduing my new library libA-1.0.0.aar as a local dependency. Later it will be resolved through an external repository.
implementation files('libs/libA-1.0.0.#aar')
The app compiles and appears to be ready to run. However when I try to run the app in the android simulator it throws up an error message:
error: cannot access ExampleClass class file for com.sample.ExampleClass
ExampleClass is a class created in libB and implemented in libA. It appears as though libA has no knowledge of libB despite it being available in the libs folder of the libA aar. Is the build steps here wrong? Should this be done another way? Thanks in advance!
Related
Upgrade to Android Studio 3.0.0 mentions this, and doesn't elaborate on how to handle it:
Library modules no longer process local JARs. This is to speed up incremental builds that are caused by changes to a library module's code.
So I have a project with a library project in it. In my library project's build.gradle file I have this:
compile files('libs/com.somelib.somepackage.jar')
I changed compile to implementation and when I tried to run my app, all my classes that tried to access the import com.somelib.somepackage.SomeClass import statement threw an error that this package didnt exist.
I changed back to compile and I was able to build and run my app.
I want to comply to the new rules since compile is deprecated and will be removed with the next Gradle release, so how do I go about doing that?
If you are trying to access classes from the .jar that is included in the library project from the app project, you will have to use api instead of implementation otherwise the classes will only be accessible in the library project:
implementation files('libs/com.somelib.somepackage.jar')
should be
api files('libs/com.somelib/somepackage.jar')
As said by the documentation:
... When a module includes an api dependency, it's letting Gradle know
that the module wants to transitively export that dependency to other
modules, so that it's available to them at both runtime and compile
time ...
Reference:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations
I've created an Android library that I want to be able to use in other Android applications. I've created a small test app in the same project as the library and everything works.
To test integration with other apps, I created a new application, and followed this guide to import my library to that new application by creating a new module from the library's AAR file.
build.gradle file:
dependencies {
...
compile project(':my-sdk')
...
}
settings.gradle file:
include ':app', ':my-sdk'
Compilation succeeded and I can use the library's API inside the test app, but when running it I get a runtime exception
java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/MediaType;
I assume it's a dependencies issue, since the library uses okhttp as a dependency, which is probably not being packed in the AAR file. I just couldn't find anywhere how to compile this dependency so it would work in other projects.
I fix it adding jar files instead of using maven into my module, then assemble release aar, and in the app project disabling "Instant Run".
I have a library which itself has a few dependencies, namely Realm, Retrofit, as well as a native library. The native library is on github and I can successfully pull it into my project via jitpack.
I have a sample app which im using to test this library. In my sample apps 'app' module build.gradle i my dependencies block looks somewhat like the following:
dependencies {
compile project(':sdk')
...
//compile realm,rx,retrofit, etc..
}
When doing this, my sample app works correctly.
Now lets say I either:
Grab the sdk's generated .jar file and put it in my sample apps /libs
Get the .aar and do the same as above
Put the repo on jitpack and try to download it via compile 'xxxx'
Trying to include the sdk in my sample app any of these other ways does not seem to work and spits out unhelpful errors.
What could possibly be the issue? I got a hint that it may be an issue with 'transitive dependencies' but am not sure where to start.
Ideas?
I have an issue that dependencies of a project that is referenced as a dependency module in my android app seem not to be included into the .apk file of my android application.
Project Setup
Android App (Android studio & gradle)
Java desktop application (IntelliJ/maven/gradle project)
Java model (classes & features used by both android & desktop app)
The "Java model" is added as a dependency to both Android App and Java Desktop application.
When I run the desktop application, the dependencies of the JavaModel are resolved via maven, incuded into the application and everything runs smoothly.
From an Android Studio point of view, I have imported the JavaModel as a module into the android project and gradle is used to resolve the dependencies. I have set up the following gradle files:
Android App "settings.gradle"
include ':app'
include ':JavaModel'
project(':JavaModel').projectDir=new File('../../JavaModel')
Android App "build.gradle"
dependencies {
compile project(':JavaModel')
// and more...
}
Java Model "build.gradle"
dependencies {
compile(
'org.apache.httpcomponents:httpclient:4.4.1'
// and more ...
)
}
Everything compiles just fine without any errors and a .apk can be created and runs on my test device. However, as soon as I access features within the app that are provided by the "Java model" (in this example, I am using the HttpClient class from the org.apache.httpcomponents:httpclient:4.4.1 dependency), I get the following exception:
java.lang.ClassNotFoundException: Didn't find class
"org.apache.http.impl.client.HttpClients"
Plese note that this is just an example case and the issue also occurs with all other dependencies that are only referenced in the "JavaModel", but not in the Android app itself.
It seems to me like the dependencies of the "JavaModel" work just fine at compile time, since everything executes just fine, but are then not included into the .apk file and therefore cause this exeption.
The question is how can I (correctly) make sure that even dependencies of a dependency project are included into the .apk file?
Apache http client conflicts with android one, if you want to use recent one, you need to use android port https://hc.apache.org/httpcomponents-client-4.3.x/android-port.html
Regarding "JavaModel". If dependencies of JavaModel are compile dependencies it all must work fine (assuming dependency does not have fancy code like classloaders)
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.