I followed this "tutorial" here and loved the idea of splitting up an Android project into modules. But now I stumble upon problems I wouldn't have otherwise, e.g. that I can't use SQLiteOpenHelper in the model-module as it's plain Java and not Android.
Is there a way to use Android-Classes (e.g. SQLiteOpenHelper) there nevertheless or is it recommended not to split up the app at all?
When you create a new Module in Android Studio you can choose which type on Module you want to create. Just choose Android Library and you will have access to the Android classes
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.
This question already has answers here:
How to make a .jar out from an Android Studio project
(11 answers)
Closed 5 years ago.
I want to create some library that i will use in the future beside my current project.
I can't find a way to create library in android studio.
How to do it on android ?
A library module is useful in the following situations:
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.
In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module.
To create a new library module in your project, proceed as follows:
Click File > New > New Module.
In the Create New Module window that appears, click Android Library, then click Next.
There's also an option to create a Java Library, which builds a traditional JAR file.
Give your library a name and select a minimum SDK version for the code in the library, then click Finish.
Once the Gradle project sync completes, the library module appears in the Project panel on the left.
If you don't see the new module folder, make sure it's displaying the Android view.
Visit https://developer.android.com/studio/projects/android-library.html
As introduction I would suggest you to peek into this conceptually simple tutorial. Basically you can start your own library module when you chose your project, without adding any Activity. Then you create your Java Class, usually with a View. When your library is ready, with all its business logic, you can glue everything inserting in the top level build gradle the instruction you are using a library, basically before you remove the following line, that is not needed for a library:
applicationId
(This line in your gradle file is a unique application ID that looks like a Java package name, that identifies your app to the device you are running and in google play)
and then you change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.name_library'
When you have a more structured project you can follow the official documentation
As it is well explained the difference between a normal Application and a Library is:
An Android library is structurally the same as an Android app module. It can include everything needed to build an app, including source code, resource files, and an Android manifest. However, instead of compiling into an APK that runs on a device, an Android library compiles into an Android Archive (AAR) file that you can use as a dependency for an Android app module.
I would not encourage you to use solutions like web services that do it on your behalf, namely just copying/pasting your existing code. In fact a library often needs specific architectural choices, so is important to consider and learn different factors, is not just writing some business logic is quite complex to explain, but you can imagine that also the choice of what the user can see and modify can be crucial. Also should be as much as possible bug free, because once it is adopted could cause problems to the users. I remand you to a famous post, superbly written where you can find some solution to this aspect.
you can use https://jitpack.io/ is very easy publish an android library. just upload your code to github/bitbucket and paste the repository link on jitpack website. that's all
I have a C++ codebase that is currently set up in Visual Studio (to run on Windows), with multiple Projects with inter-dependencies. I'm trying to bring it over to Android Studio, to get it running on Android.
I'm familiar with Visual Studio and C++, but quite new to Android Studio's Gradle and CMake.
My (possibly wrong) expectation is to try and treat Android Studio Projects like Visual Studio Solutions, and Android Studio Modules like Visual Studio Projects. Given that my codebase uses multiple Projects in Visual Studio, I am trying to create multiple Modules in Android Studio -- each one with their own build.gradle and CMakeLists.txt files.
The issue is that I cannot get one section of code (AS Module) to link with the other. I am compiling these different sections as STATIC using add_library() (I plan to have one Module that creates a SHARED library, to load into Java).
I can easily get the includes to work via include_directories("../OtherModule/src/"). However, I cannot get it to link. I cannot find the .so (or similar) file to link to (via target_link_libraries() or equivalent). When I extract the .arr file from a given Module, I do not see any .so or anything.
I realize that I could simply put the entire codebase under one Module (using one build.gradle and one CMakeLists.txt -- or network of CMakeLists.txt's using add_subdirectory()). I don't know if this is fine, or if it would take more/less time to build.
I'm sure that there could be multiple ways to set this up, and it could just be a matter of preference. All research that I've done thus far has only found strictly adding native code to the same module with Java code -- doing basic JNI native bridge stuff. I haven't been able to find a single article about multiple native Modules linking together.
I'm hoping that someone with more experience with native development on Android could help me out. Thanks!
TL;DR: Simplified scenario: (Without being concerned with the JNI native bridge) I have two Modules in Android Studio, both with only native code. I would like to have each Module have its own build.gradle and CMakeLists.txt, creating its own STATIC libraries. One Module depends on the other and must set the correct include and link directories. How do?! Is this even correct (or should there ever be only one Module with native code)?
I asked a related question here. It seems to me that AS...
...does not actually link the final module-library unless it's SHARED (it does allow static 'sub-libraries' within the module); consider making the final library shared - you will have to System.loadLibrary() it specifically in Java though.
...does not allow you to install files to other places (e.g., from your native module to your Android app). I work around this by fetching the library through set_target_properties( jniwrapper PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/../libnative/build/intermediates/cmake/${BUILD_TYPE}/obj/${ANDROID_ABI}/libnative.so ) and setting BUILD_TYPE in build.gradle. Not overly elegant though.
Overall, this does not seem to be an encouraged use-case in AS...
I am working on a Android app in which I would like to add plug and play module functionality ,Say I have Two android project
[A] An app for capturing a image using camera and storing it in memory
[B]Enabling map and locate current location .
now I want to add add this functionality in my Another Android app.While going through android developer link and Android Library Projects - Tutorial I figure out that to use plug and play module I have to make my above project as library project so that I can use it in my new app ,now my questions are
1 Is this only way to use library project to add plug and play module
functionally in my app or there are another way also?
2 what are the pros and cons of using library project in order to add
plug and play module functionally?
basically I am researching on how to add plug and play service in my android app and trying to find best solution ,so any clarity on this topic will be extremaly helpful !!!
thanks in advance !!!
An alternative would be to use a linked source folder. In Eclipse, you can set this up in the project settings in the Java Build Path section. You can choose some source code directory outside your project that will virtually be in your project's space (and other projects that use it will do the same). I find this to be handy just from an IDE UI standpoint when I'm co-developing a module along with an app or two. What I don't like about library projects is that they sometimes don't update correctly within your project so you have to rebuild it and your project or clean your project to continue. Also, the module is simpler because it's just a directory of source code files, not an entire Eclipse project.
One other downside of libraries is that they can introduce conflicts that can be a headache to fight. For example, your library might have a different version of the Android compatibility library in it than your main project, and therefore give you compile time errors. Or there are sometimes errors with duplicate libraries, and you have to go fool with the Order and Export settings of your project.
If your module is going to rely on String, layout, and image resources, etc. that are common to all apps that implement the module, then a library project will be easier to work with so you don't have to pass all your resources in through your module's class constructors and duplicate them in each project.
A third option is Gradle, although as far as I know, that would essentially just help automate one of the above two options. I'm not very familiar with Gradle.
I have an Android code base which uses APIs with settings to get different data for several apps. All apps use the same code base but with one or two design tweaks. So how do I re-use the main code base without having to copy the whole Android project each time?
iPhone uses multiple targets in the same project which works well. If android cant do this do I need to compile binaries of the code base in one project and then import into each new app project? If so how? I'm using Eclipse and am an intermediate Java developer.
Any help much appreciated!
Doug
Check out "Working With Library Projects" from the Android documentation. This should be just what you're looking for: http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
The current way to approach this issue if you are using Android Studio with Gradle is by using Gradle, Build Type + Product Flavor
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
Build Variants
One goal of the new build system is to enable creating different versions of the same application.
There are two main use cases:
Different versions of the same application
For instance, a free/demo version vs the “pro” paid application.
Same application packaged differently for multi-apk in Google Play Store.
This new concept is designed to help when the differences are very minimum. If the answer to “Is this the same application?” is yes, then this is probably the way to go over Library Projects.
Note: This answer is basically obsolete now that one can create .aar libraries with resources. It still works, though, and there may be times when the portability of a .jar is desirable, so I'm leaving it here.
Blumer's answer is a good one, and you should definitely look into Android's idea of library projects. However, there is another alternative. If you have a module that contains only Java code, but no resources of any kind (images, layouts, etc.), you can compile it to a .jar file separately and use the resulting .jar in multiple application projects. It works like this:
Create a new Java project in Eclipse (not an Android project) and move the source code of your module there.
If your module references any classes from the SDK, you'll need to add the Android SDK .jar to the project's classpath (Project > Properties > Java Build Path > Libraries > Add JAR).
When your module is ready to use, bundle up its .class files into a .jar. You can do this manually, or you can look around to figure out how to get Eclipse to do it for you.
Copy your module .jar file into the "libs" directory of your app's main project.
Add the module .jar to the project's classpath (again, Project > Properties > Java Build Path > Libraries > Add JAR).
Now you should be able to build multiple apps using the same .jar, while maintaining only one copy of the module's source code.
Depending on your particular situation, this may or may not work any better for you than the standard Android library mechanism. But it's worth considering as an alternative.
The Android documentation recommends another approach if there aren't too many "different APIs" used.
The idea is to use reflection instead of making direction references to the code. Make sure to use optimized reflection instead of lookups every time.
References
http://developer.android.com/training/multiple-apks/api.html
http://developer.android.com/google/play/publishing/multiple-apks.html#ApiLevelOptions
You might want to consider using a source control system like Subversion (or GIT). Keep your most feature complete version in the trunk, and make branches for your separate versions that use different data sources or require minor layout changes, etc.