Remove library project from apk using gradle - android

I have a library project used by my project. Also my project has several build flavors. In code I reference some classes from that library. I need that library to be removed from my .apk for some of the flavors. I know that this will lead to NoClassDefFoundError but it's ok for me.
I tried to use flavorCompiletask instead of compile but this were leading to errors during compilation of my project (as expected). So how can I make that libeaey project present during compilation but removed from .apk?
P.S. I know that I can remove it during compilation AND alter some of the code files which are using the classes from that library but for me NoClassDefFoundError is ok so I'd like to not have different versions of the same .java files per flavor.
EDIT1
I've found this article but Gradle version seems to be older then current one. Also now I know that I'm looking fot ability to add compile time dependencies.

you could add flavour specific compile block in dependencies, so ex. if you have flavour free and pay
use
dependencies {
payCompile '<something>'
}

you might use provided:
dependencies {
provided 'optional-lib'
}

Related

Multiple APKs packaging the same library can cause runtime errors with only one dynamic module on AGP 4.0.1?

I have only one dynamic feature module in my project called search, But when I try to build project, I get that Error:
[:search, :search] all package the same library [androidx.recyclerview:recyclerview].
Multiple APKs packaging the same library can cause runtime errors.
Placing each of the above libraries in its own dynamic feature and adding that
feature as a dependency of modules requiring it will resolve this issue.
Libraries that are always used together can be combined into a single feature
module to be imported by their dependents. If a library is required by all
feature modules it can be added to the base module instead.
Of Course, the first thing I did is to research about people who had the same problem And I found:
1- This Question
2- This medium article
Both Introduce the same solution (Use Android Gradle Plugin 4.0) and my project uses AGP 4.0.1, But the problem is that I have only one dynamic module called search. I don't have any other dynamic modules, even further I don't have the dependency of RecyclerView: androidx.recyclerview:recyclerview in my search gradle file, So this is maybe a transitive dependency.
Also, you can find that duplicated dependency in one of two ways:
1-Navigate to: PROJECT_NAME/module_name(In my case: search)/build/intermediates/
and then search for "deps.txt" file in that directory, Open the file and you will see all your module dependencies direct and transitive ones
2- run ./gradlew :module_name:dependencies task
If you tried to remove that duplicated line: androidx.recyclerview:recyclerview from "deps.txt" file, it gets generated again after each build.
That being said, I need some rule in my packagingOptions {} like exclude to prevent that conflict between search.aar and and any other search.* format
Can anyone help, please?
In my case, I removed the below from one of the modules
Note: not from the base module
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

How to get a compile error if a JAR dependency is missing?

I have an Android project with the following dependencies:
-- Android App
---> MySDK.Jar
------> 'org.apache.commons:commons-lang3:3.5'
This is MySDK.jar that has a dependency on commons-lang3.
I'm working on Android Studio and I'm thus using Gradle.
Here is my problem:
I have shared "MySDK.Jar" to someone and he has built his own Android App on top of it.
It works but we have seen that the compiler doesn't notice the missing dependency on 'org.apache.commons:commons-lang3:3.5'. At run-time there will be a crash if the code using 'org.apache.commons:commons-lang3:3.5' is called. One may not notice the problem if he doesn't call the code using this library.
I know that we can solve this issue by adding the following line to Android App build.gradle file:
compile 'org.apache.commons:commons-lang3:3.5'
I'm wondering if there is a way to get a compile error indicating such missing dependencies? It is indeed better to see the dependency problem at compilation time rather than at runtime.
What are the recommended good practices for this?
Thanks!
commons-lang3 is a transitive dependency of Android App. As such, it is often not needed for compilation - there are exceptions, especially regarding multiple levels of inheritance. So at compile time you (usually) do not know whether you miss a transitive dependency that you need at runtime.
This is where Gradle comes in. Gradle can (as Maven) resolve dependencies transitively from a Maven repository (as MavenCentral). If you put MySDK into a Maven repository (like Nexus or Artifactory, which have open source versions), everyone using MySDK will automatically draw commons-lang3 so you will not miss anything at runtime.
If you are just adding the jar file in your project you can't warning about the missing dependencies.
To do it you have to publish the jar file in a maven repo.
In this way you have a pom file which describes the dependencies that gradle has to download.
Provide a method like MySDK.init() int your MySDK.jar,call a method whe is belong to org.apache.commons:commons-lang3:3.5' in the MySDK.init() method, then put init() into onCreate() of your Application,
Another way is,putorg.apache.commons:commons-lang3:3.5 into MySDK.jar,
Hope it helps you :)

Does an Android .aar built with Gradle contain dependencies?

Not sure why I can't find any answers on this. If I convert my library project into an .aar using Gradle in Android Studio, does it retain all the dependencies of that module?
I'm asking because I'm trying to use a Gradle generated .aar locally, but it looks like only some of the original dependencies have been packaged. Namely, it complains that I'm missing 'OkHttp', but if I add it to the main project I get duplicate class errors.
Usually a library does not directly contain its dependencies. This does not matter whether it is an aar or a jar. Instead, the library declares its dependencies in the Gradle build file and they are resolved when someone uses the library.

What does "#aar" means in gradle 'compile' task

I'm new in gradle and cannot find any documentation about this feature.
All I noticed is that
compile 'com.github.asne:asne-vk:0.2.1' caused some manifest merging problems (some grunting abot different minSdkVersions) and that compile 'com.github.asne:asne-vk:0.2.1#aar' works fine.
The AAR format
is the binary distribution of an Android Library Project.
As described here in the official Android Tools documentation.
In your case, when adding a compile dependency in an Android Gradle project, adding "#aar" means that you would like to fetch the #aar file and not a regular JAR file.
Actually that is not a classifier. Classifier (the term coming from Maven) refers to something else.
See http://www.gradle.org/docs/current/userguide/dependency_management.html#sec:how_to_declare_your_dependencies
This is an optional classifier which indicates which artifact should be downloaded. In this case aar instead of jar is chosen. Here You can find the docs. Search for classifier.

Android/Gradle: Where is this dependency embedded?

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.

Categories

Resources