How to use a project in another project as a module? - java

I'm hoping to modularize an Android project so it will be easy to maintain in future use. I want to know whether its possible or not, to make an Android project used as a module within another Android project?
For example, let's say in my main android project I have three buttons for login, signup, forget password. and I have three separate projects for login, signup and forget password with their own views, activities and libraries. How can I use these separate android projects in the main Android project so when I click any of the three options in the main activity view so it will load the relevant view from those three module projects? (click the login button in the main view and load login view from the separate login project activities). I have tried to fiddle with build files and library module importing but couldn't do it. help me with saying if this approach is possible or not? if possible how to achieve it?

settings.gradle in root project include each project.
include ':loginApp'
include ':signupApp'
include ':resetpasswordApp'
project(':loginApp').projectDir = file('path/to/LoginApp')
project(':signupApp').projectDir = file('path/to/SignupApp')
project(':resetpasswordApp').projectDir = file('path/to/ResetpasswordApp')
build.gradle Of main module
implementation project(':loginApp')
implementation project(':signupApp')
implementation project(':resetpasswordApp')

Yes, it's possible.
Your current project is basically a module too. See the app folder.
An easy way is to create a new module (android library) with a different name from the current one which by default is app. Let's call it bigapp. This module will have all properties just like your app and you can select to run it from the debug configuration too.
Go to settings.gradle and ensure the file reads include ':app', ':bigapp'
To use the bigapp and call its functions, import it in your dependency. This will be the build.gradle file of the app module.
dependencies {
implementation project(":bigapp")
}
Lets now start the MainActivity from our app module
startActivity(new Intent(this, com.lucemanb.bigapp.MainActivity.class));
You can also import it at the top and simply call the MainActivity
import com.lucemanb.bigapp.MainActivity;
---
private void startActivity(){
startActivity(new Intent(this, MainActivity.class));
}

Related

Custom view from module not showing in android studio palette

I have created android studio module that is supposed to be a android ui framework. Framework has few components like custom buttons, custom layouts etc. and if I include it into app i can see them in palette under "Project"
Next steps in development is to enable user to import framework using gradle and I managed to to that using jfrog but when you import into your project you are not able to see components in palette.
I tried importing just .jar and .aar files but nothing happened. And if you write xml manually components are shown in preview window normally
here is git and jfrog
P.S.: keep in mind that project is still in development so git documentations is not full!
Thanks in advance, Leo
EDIT:
If you open new project you can import framework using gradle.
Just add:
maven {
url "https://cmykui.bintray.com/maven"
}
to your root build.gradle file and
implementation 'hr.foi.air2018.cmykui:cmykui:0.3.6'
to your app build.gradle
if you want to try out a component you can add
<hr.foi.air.cmykui.component.ButtonComponent
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="50dp" />
to see if framework is imported.
I think this is an Android Studio problem. It doesn't show View subclasses in imported modules to the Project tab of Design panel of XML.
Reason 1.
Android Studio doesn't show View subclasses in dependencies to the Project tab of Design panel.
For example, even if implementation 'com.android.support:percent:28.0.0' exists in module(app) build.gradle, android.support.percent.PercentFrameLayout doesn't show up in the Project tab of design panel. As auto-complete prompts it in Text tab of the xml, indexer can find them. So, Android Stuidio should check them if it is a public View class or not and add to Project tab, but it doesn't.
Reason 2.
I also tried with a quite simple module which contains a View subclass. It is shown in development stage (having source codes in that module), but if once built and imported as aar, it isn't.
I've tried with this.
public class CustomView extends View
{
public CustomView(final Context context, #Nullable final AttributeSet attrs)
{
super(context, attrs);
}
}
I also checked with cmykui module in the question, but the result is same. If imported from Git with source code, it appears. If imported as aar , it doesn't.

How To Upload Android Library For Private Use Only?

I have an Android app but require to create two Different copies of it with different package names. The reason for wanting two package names is that I want to upload them to two different Stores which requires the developer to provide a different package name.
To simplify the update process and fixing the issues I thought of dividing my code to different libraries.
I have now created an android library and now want to upload it so that I can simply use the library in my projects by simply adding the line compile com.adc.aaa...
But I do not want those library to be accessible to other developers either directly of maybe through google Search. Is it possible to achieve this?
These are few links which I have visited regarding this matter
1) https://inthecheesefactory.com/blog/how-to-upload-library-to-jcenter-maven-central-as-dependency/en
2) https://mayojava.github.io/android/library/creating-and-distributing-your-own-android-library/
Can anyone please help.
Suposing your module in android studio is called "library" you can simply add the module to your app project locally without the need to upload it to bitray.
You can add a local module as a depedency to your project using AS or manually:
1) Using AS:
Go to File -> Project structure. Click on the app module and then depedencies, click on the plus sign and then choose 3 - Module Dependency. If you library is in the same folder as your app it'll show up there. Just select it and it will be added as a depedency like the other compile dependencies.
2) Manually
In your settings.gradle file add:
include ':app', ':library'
In your app leve build.gradle:
implementation project(':library')
If you library module is somewhere else you can inport it to your project using:
File -> New-> Import Module

Android Module reference layout error

I'm not good at using the modules on Android
So I have a problem. When I called module activity it must refer the module layout but actually it's referring to the App layout.
I guess it might R.java problem, but I'm not sure.
Here is how it is setup in my project.
During the build process, any resources in a library module are ignored if the same resource appears in the main module. That's why androidmodule2.MainActivity is picking up the activity_main layout from the app module: the resource of the same name in the library module is being thrown away during the build process.
Change the name of the layout resource in one of the modules so there's no name collision and all should then be well. (Well, at least this particular problem should go away.)

Android Project Structure Changed After Gradle Sync

I am working with android studio attempting to implement the following github within my own application
https://github.com/jhansireddy/AndroidScannerDemo
When following the instructions I add the following line to the gradle
compile project(':scanlibrary')
After adding the above i then sync the gradle and the whole project structure changes, to the point where i no longer recognise it
Original Project Structure
New Project Structure
If you havnt gathered i am fairly new to the whole android programming thing
One thought i had was that the git project uses OpenCV and it was related to this, however i have not manually installed OpenCV
You have to import scan library to bring project structure to normal. Follow these steps:
1 - Download zip from this link.
2 - Extract it and copy location of scanlibrary project present inside ScanDemoExample. In my case it is like (D:\Amrit\AndroidScannerDemo-master\AndroidScannerDemo-master\ScanDemoExample\scanlibrary).
3 - Now come back to Android Studio, select
File -> New -> Import Module
and paste copied location path from previous step in source directory which will result in module name :scanlibrary. Click on Finish to close the dialog.
4 - Now your project structure will look like image below. (You can view it by selecting File -> Project Structure).
5 - Don't forget to add dependency
compile project(':scanlibrary')
inside build.gradle file.
6 - Now your library is ready to use with your project. In case you are facing any errors try cleaning & rebuilding your project. And more often errors are import problem so try including these imports :
import com.scanlibrary.ScanActivity;
import com.scanlibrary.ScanConstants;
where ScanActivity & ScanConstants are classes present inside scanlibrary.
Let me know if you have any doubts. Happy coding!
add this depenedency in build.gradle
implementation 'com.github.neutrinosplatform:scanlibrary:3.3'
and add maven in repositories
maven { url 'https://jitpack.io' }

ClassNotFoundException when using library projects

I have a problem when trying to reuse a project class and resources, I always get a ClassNotFoundException. I dont know what I'm doing wrong.
The easier steps for reproduce my problem is create this two projects:
MyLibrary project:
Creating a example launcher activity ("Activity1") and in properties/Android
section mark as librayy
MyProject project:
It is just an empty project (no classes), in Java Build Path\Projects tag add the
project "MyLibrary" and declare in the manifest that it MAIN & LAUNCHER activity
is "Activity1"
Press play and there you have the ClassNotFoundException.
My target is inherit a whole project for making a free version from a paid version and keeping the common code in a single place for keep the maintain easy
Thanks

Categories