Since my application is growing more and more dense each day. I thought of re-structuring various files.
By re-structuring, I mean to add folders seperately for screen activites, another folder for dialog xmls, screen xmls, for custom adapters, etc. In other words, I need to segregate files according to their significance.
Currently, all my java files are listed in src folder (screen activities, custom adapters, business logic, etc.) and all the xmls (screens, custom list view design, custom dialogs, etc.) are present in layout folder.
Is it feasible? What impact will it have on the existing project?
Folders are synonymous with packages -- that's how the IDE interprets them. So putting them in separate folders actually creates separate packages. That's the downside, they won't have the same scope/permissions as if they were in the same package.
Packages in Java
What you're talking about is most likely packages.
Packages makes structuring of bigger projects and navigating through it much easier.
However you can't without writing your own gradle extension put your XML files outside /res/ folder, but codewise, wrapping your classes in packages makes working in bigger codebase much easier
You define the package you class is in before defining the imports in following way:
package com.ruuhkis.test;
import android.content.Context;
class Test {
}
then the Test class is put in folder /src/com/ruuhkis/test/ folder to be found by the compiler
Related
I am creating three apps that are almost the same except some changes in the assets and raw files between them.
So I wanted to create a library from the first one, that will contain all the logic and activity and all the layouts and resources, and then import this library to the other two apps, and only change the assets and raw files.
the problem is that when the layout from the library loads, it is loading the assets from the library itself, and not the assets of the main app.
I am sure that my design here is wrong then I would also appreciate if someone can explain to me what is the right way to share activities and layouts as libraries in android.
NOTE: I don't want to use flavors. I want to build it as a completely different projects.
cheers
I have an app which is going to have multiple instances. The instances will differ in some resources, which can be divided into two groups:
differs only in (un)commented code, whole code is present
colors
strings
small differences in layouts (e.g. in one instance some components will be gone, in another instance they will be visible, but code remains)
complete substitution (name, files)
package name
drawables
certificates in res/raw (all certs mustn't be stored there, only for particular instance)
What is the best approach to deal with these requirements? Use different git branches?
I use git (smartgit client). I guess that all requirements could be met when I use different branches for different instances. But I am afraid that this approach won't work for second group - complete substitution (name, files). Any suggestions?
PS: I understand that When adding new features I need to merge from some master branch into all other branches.
After all git approach wasn't that right. Building multiple app instances (product flavours) with Gradle was just the right thing to do:
1) Specific files and resources per the instance of app
colors and strings are located in resource files: Project/module/src/instance_name/res/values/instance_name_cfg.xml.
layouts are located in: Project/module/src/instance_name/res/layout/instance_name_cfg.xml
Java code is located in: Project/module/src/instance_name/java/package.name/MyClass.java
drawables (PNG images) are located in: Project/module/src/instance_name/res/drawable/, ic_launcher images are in the same path only in different folders (drawable-hdpi,...)
Common files and resources are located in Project/module/src/main/ folder.
2) In the first picture there is the whole file structure.
Instances are in my case cities (Dammam, Jeddah, Prague, Reading). Screenshots are taken from Android Studio, where you can switch build variants (flavours) - at the bottom of the picture.
3) In the second picture there is a fragment of the build.gradle file.
Mixing app (instance) resources is quite powerful. As you can see in the picture below, resources, java files and even manifest files could be combined together in directive sourceSets. For example Dammam and Jeddah have the same java files, some common resources and specific resources per instance. Using name of instance (e.g. dammam) in sourceSets must precede declaration in directive productFlavours.
I have an app on the iPhone and need to port it to android. For this I would like to group screen related files like classes and xml per screen in one "screen group" per screen somehow, ideally also strings and other value files
if I use folders I can only group res files separately and src files separately.
what would be the best way?
Thanks very much!
EDIT:
If that should not be possible, how to best then solve this issue? Do you create a subfolder in the src and another in the res for each screen?
The way you group files for the iphone is not possible for an android project. Android has pre determined folders which hold specific files, if you break this structure, your building process will fail. Its not ideal but that just how it it.
When it comes to source java files, they follow the concept of packages which are basically folders. The 'src' folder is the part where you can create sub folders as you desire. If you are adamant about keeping the files related to a screen in one place, you should create the layouts with java code and not use layout xml files.
But using xml layout files make development much easier and faster. Consider that as the presentation and java files as the logic+data. So group java files as you want and leave xml files in the layout folder with easy to identify names.
android uses certain directory layout for project structures (i.e. convention over configuration). Basically you will want to put your XML layout files in res/layout directory. Please read http://developer.android.com/guide/developing/projects/index.html#ApplicationProjects for further information.
Unfortunately, there's no easy way to do this in Eclipse. You can't create custom directories in your Android app's /res directory, you can only use permitted dir-names. E.g. you can't have a /res/layout-myscreen1 and /res/layout-myscreen2. You also must put your resources in /res, and your code files in packages, so they're at separate places in your project.
You can use Working Sets to group related files together however, but they're quite painful to use IMHO. Check the eclipse docs and tutorials out on them.
My current project is getting awfully large. I have dozens of activities, adapters, fragments, layout xmls, and other resources.
In my (smaller) previous projects I organized stuff with a 1 package / 1 category style. So I had com.stuff.xy.adapter, com.stuff.xy.activity, and so on. Now these packages contain too many items, and I find myself wasting considerable amounts of time searching for a specific class in the package hierarchy.
I use Eclipse, and there are some shortcuts one can use (go to class definition e.g.), but those tend to be situational (I can't use that to quickly jump to a layout definiton xml).
Could you share some tips on organizing large scale projects efficiently? Or some plugins for this perhaps? (It might help for example if I could group together source files that deal with a specific application screen - adapters, layouts, activity and fragment code - so I can quickly open them)
EDIT: After many months developing large projects
First I tried to go with working sets with Eclipse. It didn't really cut it for me, my problem was that our single Android project was simply too big, containing many resources, classes, interfaces, etc. Messing around with working sets in the context of a single project just took too much time, I think that they're mainly useful to organize projects in a single workspace.
On the long run we separated our huge single project into many smaller android-library projects and a single "main application" project that depended on all these smaller ones. This way we could split the resources among these library projects (there were many layouts, values, styles that were only used in certain parts in the application) and code of course. I also created a Base library, that all other libraries depended upon, and contained resources and (base)classes that every part of the application needed.
For all my android projects I prefer to sort code in the following structure:
com.company.projectname is the package of the application.
Underlying packages:
model - all my business-objects
logic - services and objects implementing business logic
screens - all the activities of the project. If activities require adapters and so on, then each activity is placed in a separate package under screens package and the related stuff is placed to the same project.
tools - package with Utility class. SettingsUtil and so on.
In the root of the package I usually have Constants.java interface with constants.
In Eclipse, you can use Working Sets to filter your source/layout/resource files in the Project Explorer view. This is a bit more powerful than packages, since it operates on all files including layout and image assets, not just java source files.
For example, you could create a Home working set which contains HomeActivity.java, HomeAdapter.java, res/layout/home.xml, res/drawable/home_icon.png, etc.
Just another tip.
Use Ctrl-Shift-R to quickly open a resource (you get an autocomplete drop down) and Ctrl-Shift-T to quickly open a java class. The list will also auto-populate using the most recent opened files.
Maybe a tip: to quickly go to a declaration in Eclipse
Hold Ctrl while hovering over a class or method. After 1 sec you get a popup with open declaration / open implementation.
Very useful in large project.
For the rest i recommend just making it intuitive and sort all Activities in a package aswell as all calculations e.g.
for your concern "I can't use that to quickly jump to a layout definiton xml", you can click the name of the layout xml, then ctrl+shift+R will lead you to that definition page.
I'm just getting into my first android application and just wondering what the convention is here?
Is it more organised to separate my code into various packages? For example.
com.myfirstapp.activity;
com.myfirstapp.database;
I was thinking of doing this as a way of organising my code with database helper files for example kept it .database package.
I have just noticed that data is stored in /data/data/YOUR_PACKAGE/ does this mean that when on a device I would end up having data stored all over the place if I use different packages?
If this is not right what is a better way of organising code in Eclipse much like you do in Xcode?
Yes, please separate your code into packages. That's a beautiful concept of Java.
The file name your app is stored under is determined by manifest package element (which will be com.myfirstapp in your case).
Also have a look at Declaring class names section.
All your source file are stored int the src folder of your project. In the apk that is generated all your classes seem to be merged to a classes.dex file.
Keep your classes in the res folder and organize them into as many packages as you think you need. I normally have a data package that contains my data classes, packages like sound, database, location... that contain classes/controller for this various topics and then I have a ui package that holds several sub packages for the main branches in my User Interface.