I have to add a 3rd party library to my project.
Let's say the library is called XLib and it is made by vendor A.
I already have in my project a library with same name (XLib) but it is made by vendor B and they are 2 different things, I need them both.
how do I handle this scenario in Android Studio?
Thanks,
how are you importing these libraries? are they library modules or gradle dependencies?
if they are library modules, and you have the source, i would recommend renaming the modules XLib-VendorA and XLib-VendorB. Assuming they don't also have overlapping package structures, that may be all you need.
if they are gradle dependencies, then they should originate from different group names, so you would only be concerned about package collisions at that point.
Related
I have read some sample codes, I find that many project use library module structure, you can see Image A.
Could you tell me the benefit to use library module in Android Studio ?
What code do I need to place it in library ?
And More, both app and lib module use the same namesapce in the sample code, I don't know if it's suitable, could you tell me ?
Image A
Library module gives you two options to create library Android and Java.
Android library module-> Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module . It allows you to add android specific components like resources and manifest file, which allows you to bundle in shared resources like layouts and drawables in addition to Java classes and methods.
Java Library -> It builds a JAR file. JAR file is useful for many projects especially when you want to share code with other platforms. But it does not allow you to include Android resources or manifest files, which is very useful for code reuse in Android projects. So when you do not need any android specific resources in library you should create a java library.
Library modules are beneficial for projects :-
When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.
Quoted from developer.android.com
Other than that same namespace is not problematic unless you have same package name inside App and libraries . You should use a different namespace for libraries.
PS-> If you are familiar with Clean Architecture, The idea behind most of the software design pattern is Separation of concern . In Clean architecture a project is divided into multiple modules. When you implement clean architecture in android you'll see that some of the module you can create as Java library like domain module. Creating module is really useful to follow re-usability and SOLID principles and Inversion of control.
Firstly, don't look into the package name declared in the java directory. Look into the manifest file. You can see that these modules have different package name. It means that all modules in a project must have different package name.
Regarding to your question, what are the benefit of naming library module as lib?
There's no benefit at all. Some people are comfort with lib name, so they can differentiate the demo and library module easily. However, using lib as library's module name requires you to add additional configuration in the lib/build.gradle, i.e. archiveBaseName. This Gradle attribute will rename the JAR/AAR from lib.aar to work-runtime.aar, so people can use it like this:
implementation "androidx.work:work-runtime:$work_version"
If archiveBaseName is not set, people will use it like this:
implementation "androidx.work:lib:$work_version"
In real case, let's take my open source library as the example, MaterialPreference. I used to use lib name on this project, but now I think lib is not a good module name. Using materialpreference as module name will remove additional configuration archiveBaseName. So I feel it is more simple.
I have an old eclipse android workspace which has the following structure:
one library project which has all the code and default graphics and resources
other projects which depend on the library project, but with different package names, different strings.xml and different graphic resources.
Basically what this means is that all my code is in the Library Project, and all other projects are really just different apps, based on the same code, but looking differently with different names
All these were in one Eclipse workspace and now I need to update the project, so upgrading it to Android Studio sounds like the best way to go.
The easy way is to hope that export works for your case. I battled with Eclipse->Studio migration for weeks. I had exactly that situation - one common project and other apps on top of it. Studio will just convert your common project into an Android library module, and other 'app' modules will be converted into Android app modules. Check out this answer: https://stackoverflow.com/a/22797387/2102748
When you're migrated, you should look to move your source files from the ant-style hierarchy to the gradle-style hierarchy. There is a lot of content for that here: http://tools.android.com/tech-docs/new-build-system/user-guide
Android Studio will also ask you to replace .jar libraries it knows about (like appcompat, google-play-services, etc) with gradle dependencies - do it. Life becomes so much easier.
I have a library module in my Android Studio project. Because I would like to publish a free version and a paid version of my app, I've added two application modules that depends on this library project.
If I would like to toast e.g. "FREE" for the free application module and "PAID" for the paid application module, how do I tell the difference of which application module that is active at run-time?
I've had a look at this question but I believe that it contradicts the reason for implementing the library module in the first place.
Initially I thought that I would add two different MainActivity.java in each of the application modules, each with a different Toast message. This did not work, since the library modules MainActivity.java was still shown.
So my questions are; Am I on the right path here, is this how you utilize library modules in Android Studio? If so, how do I add code that is specific to the application module, thus not implemented in the library module, "overriding the default" library module code?
Rather than going with libraries you may want to investigate application flavours under the gradle documentation:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-BuildType-and-Product-Flavor-property-reference
All the following are Android Library Projects.
'thirdPartyLibrary'
'module1'
'module2'
With the assumption of 'Android Library thirdPartyLibrary' having a dependency on 'Android Library module1', how do you allow access to submodule functions?
After exporting 'Android Library thirdPartyLibrary' as a .jar for a third party library, public classes of 'Android Library module1' are not available to them. How do you design the jar exported to provide access to members of the submodule?
Providing similar package names across Android Libraries allows for classes to communicate with one another with 'default' or 'protected' status. Even though they are in different projects. I had not known that this would work across Android libraries.
So, in order to obscure what the user should or shouldn't see within the library when exporting a third party SDK, while still keeping separate functional libraries and providing access across them, the package names just need to be the same. This required a little bit of re-factoring but in the end 'module1' can share its protected classes with 'thirdPartyLibrary'.
'module1' is a dependency for 'thirdPartyLibrary' directly as a Library Project in the settings.
'module2' is a dependency for 'thirdPartyLibrary' directly as a Library Project in the settings.
'thirdPartyLibrary' gets exported as a jar library and passed to the end-user.
I'm developing a software layer that I would like to reuse several time for building my Android applications. Basically I want that, once installed, the software layer any other apps can use it (like a system library).
I was wondering what is the best solution for doing this, when I found that recently Android supports library projects (http://developer.android.com/guide/developing/projects/projects-eclipse.html#SettingUpLibraryProject).
So I decided to create my software layer as a library project, making the code it contains re-usable by the other applications I want to realize.
My software layer depends on a set of external jar, which are correctly located in the lib folder of the library project.
The problem is that when I create a new project referencing the library project I'm not able to see the classes defined into the external jars of the library project: i.e. it seems that they are not part of the classpath.
So when referring to a library project is possible to re-use only the source code defined there? If my library project have some other libraries I have to import these libraries also in the other ones (I want to avoid this!)?
I'm also interested to know if there are other ways for doing this, but searching around I haven't found other ways for realizing Android libraries/shared code.
Thanks.
Android library projects definitely incorporate any JARs you have in the library project's libs/ directory. However, if you are using Eclipse, you probably have to somehow manually add those to your build path of the host project (the one reusing the library).
Ok I finally figured out that for solving this is sufficient to add the jars to the host project build-path (no need to re-import them, you can just choose the path from the library project). However it is weird that they are not automatically exported in the host project classpath.