How to exclude a specific library from Android module (Library) - android

I built an Android library and published it on Jitpack.io, in this library I'm using some other libraries for debugging purposes such as Facebook Flipper and Hyperion but I don't want to export these libraries, meaning, I don't want these libraries' files to make its way to anyone who is going to use my library, How can I do that ?

Looks like you want to define gradle dependencies based on build or flavor. For example :
debugImplementation "example.debug:dependency"
Now when you build your artifact, you just build a non debug variant and the dependency will not be included.
You can read more here: https://developer.android.com/studio/build/dependencies#dependency_configurations

Related

Can I convert an app configured in AOSP to an app using gradle in Android Studio?

I have my legacy version System App which consists of mk files from AOSP.
However, debugging or modifying this app is time-consuming and tedious because you have to port it after building the entire platform.
I just want to modify this app and add features within android studio like the apps I've developed in the past.
Most of the dependencies written in the existing mk file could be configured using gradle script, but androidstudio fails to build because of the part that uses system api or the part where the framework itself is customized.
is there a way for me?
I would split it into two major parts:
For dependencies on Android Application component based Java code:
Try to make a list of all the dependencies you have there and add
them via imports if they're a part of the public Android SDK, and /
or add them to your app via the dependencies block in the module's
build.gradle as an implementation. Make sure to add google() and
mavenCentral() in the repositories. (Plenty of guides on how to add
a dependency of a third party library). And then gradle sync.
For customized framework dependency:
I think the best way to add any custom framework dependency is to add it as a compileOnly file / or implementation / api in the build.gradle of whatever module you have in the app source code on your project as an AAR or a JAR file. To generate this custom framework AAR / JAR (like a CustomServiceManager.java), build that specific module in AOSP environment if it can be built independently or via make framework / make services.
Ref: https://medium.com/#chauyan/import-aosp-framework-jar-file-f0c2ac979a8a

How to add direct local aar dependency for androidTest?

I know the way using 'flatDir'.
But android studio warn do not use flatDir.
My project is android library.
I add jniLib in sourcet and use implementation files.
Then, I could add direct aar dependency to my lib and app which use my lib.
But I can't add dependency to androidTest in my android library project.
I want to use androidTestImplementation but my project is multi flavor build and there is no API flavor[Debug|Release]AndroidTestImplementation with files arguments.
I tried to search the way, but failed.
Now, I use flatDir and ignore warning.
How can I solve this problem?

AAR dependencies - bundle or not?

I am developing my own SDK, which in turn depends on some 3rd party SDKS. For example - OkHttp.
Should I add OkHttp to my build.gradle, or let the users of my SDK include that? In this scenario, they will probably "anyway" use it, so its safe to say they already have it.
Another point to add - not all paths of my SDK needs "OkHttp", so, in theory, some user of my SDK could use those parts only, and have not OkHttp on his APK.
Another thing I am contemplating:
If I do embed OkHttp on by build.gradle - how can users of my SDK use that OkHttp library, instead of consuming another replica?
Should I add OkHttp to my build.gradle, or let the users of my SDK include that?
Adding the dependencies in build.gradle doesn't mean packaging the dependencies inside the aar file.
The aar file doesn't contain the transitive dependencies and doesn't have a pom file which describes the dependencies used by the library.
Uploading the artifact in a maven repository you will have your aar and a pom file which will contains the dependencies list.
In this way gradle will automatically download all the dependencies tree and you can configure gradle to exclude same libraries.
Use implementation and package it - the consumer can still exclude it.
One cannot depend on something and then not package it; this won't build.
In the application package, it can/must only exists once ...so what's the point?
I will answer your questions in a reverse order
Another thing I am contemplating: If I do embed OkHttp on by build.gradle - how can users of my SDK use that OkHttp library, instead of consuming another replica?
How Gradle build system works is suppose, In my project I use your library and I'm using v2 of OkHttp and your library is using V1 of OkHttp, then the gradle will automatically use the latest version. You can read about it here
Another point to add - not all paths of my SDK needs "OkHttp", so, in theory, some user of my SDK could use those parts only, and have not OkHttp on his APK.
In my project I use your library and it uses OkHttp, whereas I don't use it in my project also, I'm not using the part of your library where you are using OkHttp but still my APK will include OKHttp in it. This can be avoided either by splitting your library into two separate libraries or me using proguard in my Project.
Should I add OkHttp to my build.gradle, or let the users of my SDK include that? In this scenario, they will probably "anyway" use it, so its safe to say they already have it.
You should not bundle it in your library you just use implementation and let the user of your library decide if he wants to exclude it or not.
You need knows about api and implementation in the gradle
The link will be helpful
Api:
Role: Declaring ,API, dependencies
Consumable? no
Resolveable? no
Description: This is where you should declare dependencies which are transitively exported to consumers, for compile.
Implemetation:
Role: Declaring, implementation, dependencies
Consumable? no
Resolveable? no
Description: This is where you should declare dependencies which are purely internal and not meant to be exposed to consumers.

What are dependencies in android?

What are dependencies ?
Why do we add dependencies ?
I searched a lot but could not find the answers to above questions.
In Android Studio, dependencies allows us to include external library or local jar files or other library modules in our Android project.
For example: Suppose I want to show some images in ImageView. But I'm using Glide Library to enhance the smoothness of application. So I have to add a dependency in the build.gradle(Module App) as:
compile 'com.github.bumptech.glide:glide:3.7.0'
So Now I can use Glide library :) and show my images.
Note: Glide library is the bumptech's library but still I can use it in my project from 1 line of code of dependency.
Whenever you add a dependency to your gradle file, it will download the added libraries, and add them to your project so that is available in your project. It makes it easy to manage external libraries in your project.
To study more , visit : https://developer.android.com/studio/build/dependencies.html
if you want to use external libraries or modules in your android
project you have to add the dependancies so that you may be given the
authority to use that particular library otherwise that will not be accessible to you inside the project.
so, its concluded that:
The Gradle build system in Android Studio makes it easy to include external binaries or other library modules to your build as dependencies. The dependencies can be located on your machine or in a remote repository, and any transitive dependencies they declare are automatically included as well
for further you can visit this link:
https://developer.android.com/studio/build/dependencies

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