How to use one module with FCM for 2 apps? - android

All!
I am building 2 android apps: one for each type of user and want to use as much shared code as possible. So I made 3 modules. One library module with shared code com.example and 2 application modules: com.example.one and com.example.two. Both application modules depend on the library module.
I want to put FCM into the library module. Is that possible? Will it work with only one google-services.json file? How should I configure my project in Firebase console?

it is possible ,
1)first step to create project in firebase
2) then you can see add app button in that firebase project.
3) then add app for your first app then
4) again add app for to add your second app
5) now generate service.json file ,which file used for your both apps you can copy that file in your both app directory.

Assume we have two executable module (A and B) and one library module which is C.
We want to A and B depended on C. If C has another dependencies like FCM, A and B also will depend on them over C. This is called transitive dependency.
You need add lines given below to your A's and B's gradle files.
dependencies {
...
compile project(':your_module_name(C in this example)')
}
If want to run separate modules on A & B (assume they dont depend each other, like a tablet and phone application because why not?) You need extra source sets configuration on gradle.
Good luck
Emre

Related

How to access layouts of a sub-module from another sub-module?

I have two modules in my android project. And I want both of these modules to access the layout files of other module and perform operations like Intent().
But, Using compile project (':x ') is not working for this.
Edit:
Both the modules are successfully added into the project and gradle was synced properly and manifests are merged. I want to how to access res/layout files of one module from another module's java file.For context, I have activityA.xml in module 1 which I need in module two so that when a button is clicked in module 2, it transits to ActivityA.xml with intent.
ok so to use resources from another module you must do a few things.
Determine how you will package and use your module. Is the module "just" for this app or is it used in others.
If it is used by more than one project then I recommend packaging it as an AAR and hosting in a Maven Repo of your own and pulling via Gradle to avoid project dependencies on the code base itself.
If it is only used by this project then you must do a couple things as well.
1) Ensure that the settings.gradle has the module included. You can do this with simple :moduleName if it is local to the project directory. Otherwise you can do it with
include 'myLib'
project(':myLib').projectDir = new File('../../../workspace/libs/myLib')
Once you have done this confirm Gradle Sync pulls the module into the project. And not just an empty folder with an IML. If it is an empty folder confirm your path and try again.
Step 2) So you successfully imported your module and you can see it in your directory. However, you can't have a module depend on the app and the app depend on the module. It is called circular dependency, so you must only have the app depend on the module. Go to your app's module the one where it has
apply plugin: 'com.android.application'
and go to the dependency section and include
compile ':myLib'
Now you need to confirm your compile myLib is working (myLib is the name of your lib). To confirm you can do an assembleRelease from the terminal and see if it has any issue or you can do a Gradlesync as well.
If it worked you can now import resources or files into any Activity of the parent app module by simply including the import statement at the top.
Now you can do things like HelperClass.doSomething (where helperClass lives in myLibs).
So let's take it one step further. Maybe you need HelperClass to be able to respond to the app module. Then you must supply an interface inside the myLib of IMyCallBack with whatever methods you would call out to and have the parent module calling activity implement the interface for calling back.
Does this answer your question or are you having another issue. A there should be no issue accessing the content of your child module if your dependency is setup correctly.

What is the right way to insert an activity in another project?

I have an Android Studio project
which consists of a login activity with relative style, manifest, IntentService and other stuff.
I want to insert this little project in many other apps, what is the best way to proceed ?
Is Module the right way ?
The ultimate goal is still to easy maintenance, such as if one day the server should change URL, I would not have to make changes in any application that uses this login activity :-)
You need to extract these components in a separate module:
A module is a discrete unit of functionality which you can compile,
run, test and debug independently.
Modules contain everything that is required for their specific tasks:
source code, build scripts, unit tests, deployment descriptors, and
documentation. However, modules exist and are functional only in the
context of a project.
Then, include that module in all projects using it.
In fact, you can create the module in an independent "library" project of its own. And add it as a dependency for all projects using it.
Going a step further, you can publish the output of an open source library project as .aar or .jar on maven central, jcenter and other public repositories. Then other people will also be able to use your module.
Some important points to remember when creating android library projects:
The resources (strings, layouts, xmls, images) of a library will be merged with those of final project on build. Still your module's R class will stay under your module's package name.
Some attributes from manifest file of library might be merged to that of final project (like that of <application> ). So, a library module should have a minimal manifest file, with at most, the package name.
You may include an example application project inside a library module, but do not redestribute the example app project with library. It will cause trouble for someone building an application using your library.
I think that what you need to do is to export your original project first:
File>>Export
Then go to your new project and import the original one.
Dont forget to amend the setContentView() method to point to your original activity(the one you imported)
and finally dont forget your intent method!
if you have any issues let me know and i will create a detailed answer for you but i think that you will be ok!

Can we have multiple apps in one Android Studio project?

I am using Android Studio for developing Android apps. But I have heard in Android Studio it is better to have only one app in a single (one project per app) if that is right, then it will be very wasteful to open many frames for many projects. But when I searched I found that
Android Studio project = Eclipse workspace
Android Studio module = Eclipse project
Now, if this is true, it means that Android Studio also can support a multi-app project. If yes, then, is every app in Android Studio independent like in Eclipse (i.e. they do not disturb each other by sharing any file or setting)? Or can we have many apps in a single project? If we can then is there any point to take care of?
Thanks!
Yes, you have two options:
Option 1: Create an addition app module
First create your standard Phone & Tablet Android project, including the auto-generated app module.
Add a new app module: File > New > New Module ... > Phone & Tablet Module
Complete the wizard and name your Application app2 for instance.
Now you'll have both app and app2 in the same project.
To actually run app2 you first need to select it in the pull-down menu in the top toolbar of Android Studio, next to the Start and Debug icons. You can also do this though Run Configurations: Run > Run... > Edit Configurations... and modifying Module.
Option 2: Create an addition library module
This is ideal for creating a separate library that is isolated from the app, and can be shared across more apps (or other projects):
Add a new library module: File > New > New Module ... > Java Library.
Complete the wizard and give your library a good name, like libgoodstuff.
Now libgoodstuff and app will reside in the same project.
To make app sources depend on libgoodstuff, you first have to add the library module to the project settings.gradle to look something like this:
include ':app', ':libgoodstuff'
Then in app/build.gradle you have to depend on the library module like this:
apply plugin: 'com.android.application'
···
dependencies {
···
implementation project(path: ':libgoodstuff')
···
}
···
Yes you can. Inside your project if you want to create a new app do the following:
Create a new module by right clicking on your project -> new -> module
Select phone and tablet module
Now you will be able to run either app. This is an excellent way to share code between two apps as it allows you to keep and develop your libraries in one location.
You can definitely have multiple app modules in the same Android Studio project. Having said that, I've yet to find a reason to define multiple app modules in a project.
If you need different version of the same app, Gradle's build variant is powerful enough to satisfy perhaps 99% of the use-cases (I have a project with a dozen variants, each with its own custom code/res).
If you are writing different apps then it's better to make each its own project so apps don't inadvertently change each other's behaviour.
Not sure what you mean by "is every app in Android Studio independent as Eclipse", but each module is its own world by default unless dependencies to other modules are explicitly defined.
Adding this as an answer since I don't have enough reputation for comments yet.
For the answer to your question - Check this question that I have raised. Is this the same boat you were in ?
TL;DR
I was able to have multiple apps in the same Android Studio Project, build and run them without any issues. Another member
corroborated my claims in the comments on the Question.
#Android Studio Pros : Please check the above link and add your insights. This seems to be a confusing aspect.
My Take
I think I agree with #Kai's answer. But there are instances where we want multiple apps to have common library dependencies and don't want to duplicate the library dependencies. Wouldn't multiple apps be fine as long as the common library dependencies have ONLY common code and nothing else. The separate modules hold the individual app related code and that's where the differentiation is.
Yes, it is possible. As the existing answers showed, it’s quite straightforward to create additional application module in the same Android Studio project.
So I’ll try to answer underlying question why anyone might need it.
It’s certainly not worth it to put multiple completely independent apps in one project.
However, if you app is big enough, you might benefit from putting separate features into separate modules. You can also create a separate executable app module for each feature, so that you can:
launch/debug each feature separately
save some time on every compilation/dexing/putting everything into a single apk
encourage teams/developers to work independently, and even in separate repositories.
The main app module can be used only to combine existing features together.
I’ve recently created an article demonstrating this approach where I tried to explain everything in more details: https://medium.com/#domplebump/multiple-application-modules-in-one-android-project-36e86ceb8a9

How to build own android sdk and use across my apps?

Actually We've two apps.
Both the apps are almost same functionality.So, I want to reuse the code in both the apps by creating some common modules .
Can you please suggest the way?
Build one Project i.e. Project A and add modules in this project which are common to both projects
1.
Now, when you create Project B and Project C which have same
funtionality, add Project A's JAR file in B and C. This way you will
able to re-use your code Project A
2.
Another way is to create library Project A and include library to B
and C
I will recommend 1 to use because you will be reusing java code mostly which is implemented in Project A

Multiple Android Application Modules Under One Project

I have two 'android application modules' in one project and they create independent apks.
But I want to combine these two modules into one hence creating dependencies between the modules so that one apk can be generated and one 'android application module' can invoke activities from another 'android application modules'.
Most of the examples suggests me to convert one 'android application module' into a 'library module' but I don't want to do that.
Document here suggests that there can exist more than one 'android application module' in one project but never could I find an example that does that.
Please suggest some ideas.
So guys I figured the right way to handle this problem.
So I created a placeholder library module under the project that had 2 android application modules and modified the build.gradle(i.e. I introduced android SourceSet objects) of that library module to point to sources(src and res folders) of the other application module that I wanted to merge into other application module.
In this way both the application module may coexist under the same project and you never have to touch your application module(that you wanted to convert to a library module).
The only difference would be that the manifest file of this library module will not be having a 'LAUNCHER' intent filter for any of its activity since an application module can not have more than one LAUNCH activities.
So this way you can still have 2 independent apks and continue to develop the applications independently and if you want to include one in the other then use a placeholder library module to point sources of the app modules.
Each application module creates a separate APK. What you may want to do is to create a library module to contain code common to both APKs and then add a dependency between them.

Categories

Resources