I'm creating a new module in android studio, and I want some of the classes to be hidden to outside of the module, I mean, that the classes could just be used internally in the module, but not externally. Is it possible? How could I achieve that?
Thanks in advance!
EDIT: I doubt it's possible to have module-visibility, but the closest you can use is package-visibility, for which you do the following:
Don't make the classes you intend to hide 'public'. Keep the default visibility, which is only seen within classes of the same package. Other public classes within this same package can act as your external interface to your module.
class PrivateToPackageInModule {
}
public class InterfaceOfModule {
private PrivateToPackageInModule ptpim;
}
For anyone that happens to stumble upon this post, there is now a keyword called internal which offers exactly the functionality that OP was looking for.
Documentation link
Related
I mean, imports generally don't effect code unless you use something that is unknown in the current file without giving the full qualified identifier, but this one seems weird to me. It's in a few files and is generally unused.
Can "import org.parceler.Generated" be removed savely? Is there any reason to keep it?
The part that stumps me is the word "Generated" here. It seems like this has to be or atleast should be kept, but I don't know why.
I suppose it's autoincluded when using some autogeneration tool, potentially even build into android-studio. But why is the "import org.parceler.Generated" line generated if the import is unused ?
If you're using Parceler, then it should be generating classes that look like the following:
#Generated(value = "org.parceler.ParcelAnnotationProcessor", date = "2016-09-03T09:53-0600")
#SuppressWarnings({
"unchecked",
"deprecation"
})
public class ParcelerTestModel$$Parcelable
implements Parcelable, ParcelWrapper<org.parceler.test.ParcelerTestModel>
{
...
Notice the #Generated annotation. This requires the import you mentioned if the generated class it outside of the org.parceler package.
The #Generated annotation doesn't do much here. The intention behind this annotation is to demarcate generated code from user written code, following the JSR269 standard.
If you're taking the generated code out of the purview of the annotation processor and managing it yourself then you're free to remove this annotation. I wouldn't recommend this approach, however, as it's simply more boilerplate to manage which defeats the purpose of using a boilerplate-reducing solution like Parceler.
I have multiple activities. For organization purposes I want to to put them in folders without affecting the project. How can this be done?
Just create a new folder.
In fact, you can organize your project and separate the class as you want. This way, you keep your project organized and you can hide some methods and let them to be used only for classes in same package, for example.
I use do:
...\app\src\main\java\com\example\myapp\activities
...\app\src\main\java\com\example\myapp\service
...\app\src\main\java\com\example\myapp\dataprovider
...\app\src\main\java\com\example\myapp\adapters
So, your activities classes would start with:
package com.example.myapp.activities;
And your services would start with:
package com.example.myapp.service;
Also, you may need to adjust your imports:
import com.example.myapp.dataprovider.Class1;
import com.example.myapp.dataprovider.Class2;
However, this should be done automatically by Android Studio.
Just remember that doing that, you are creating a different packages. This way, you have to take a special attention with method accessbility (private, protected, public)
However, since we usually use private and public modifiers, we should not have any problem.
I'm making a paid/free version of my app so have a 'Library Project' that the two apps use.
I'm trying to use Android Annotations to clean up my code:
http://code.google.com/p/androidannotations/
Unfortunately when I use this in my shared library project, one of my projects gets the error in Eclipse:
The type xActivity_ is already defined xActivity_.java /ProjectName/.apt_generated/lib/activities/
Because Android Annotations automatically creates a new activity with an extra '_' in the folder .apt_generated one of the apps is allowed to create this file, but the other gets the error "already defined".
Is there a way in Eclipse to resolve this? Or is it a problem with the Android Annotations?
This seems to be an AndroidAnnotations bug, and should be reported on the dedicated bug tracker.
AndroidAnnotations wasn't designed with this use case in mind, but this is still a very valid use case. The problem seems to be that the activity is generated in the shared library project, when it should be generated in each depending project, am I right ?
(please answer in the bug tracker)
This question is quite old, but I thought that I should mention android annotations now supports being used in libaries:
https://github.com/excilys/androidannotations/wiki/Library-projects
One caveat is that due to the way android library projects generate the R class, you cannot reference resouces directly inside the annotations. Eg, you cant do this:
#EActivity(R.layout.myLayout)
public class MyActivity extends Activity {
#Click(R.id.myButton1, R.id.myButton2})
public void someButtonClicked() {
}
}
Instead you must do this:
#EActivity(resName="myLayout")
public class MyActivity extends Activity {
#Click(resName={"myButton1", "myButton2"})
public void someButtonClicked() {
}
}
I just knew AndroidAnnotations (which seems a great tool!) but I think that if you do this using different projects (sharing the same library) your problem should be solved.
I have a common library where I've put classes that are used between multiple Android projects. However, now I encountered a situation where I have to make minor changes to the functionality of the class in one project. How should I organize the classes, keeping in mind easy readability of code and future extension possibilities?
Should I
Extend the class (MyClass) with modifications that are special to the subproject (MyClassSub extends MyClass)? What about if I have references to MyClass in the library classes, but in this special subproject MyClassSub should be called?
Have switch OR if clauses for each special part in the class file? And then pass some variable to the class?
Some other option?
This is probably a trivial question, but I am quite new to java and can't quite figure it out.
Definately 1.
Create a library (jar) containing the base class, and then use that jar in the projects that need its base functionality. Each project should provide the specialized class that extends the base.
If the new functionality is specific to the one project, I would avoid putting the functionality in the library. Go with a subclass or a replacement class. If later you find that this extended behavior is more widely usable, you can migrate it to the library (perhaps creating an entire new version of the library, much in the same way that the Java API evolves.)
Unfortunately the answer is "it depends."
Specifically, a class hierarchy should be designed such that the behavior of the base class holds for all subclasses of the class. One way to look at this is to say that the subclass may expand the behavior of the base class. The corollary is that a subclass should not restrict the behavior of the base class. So a Square IS NOT A Rectangle.
Also consider "favor composition over inheritance" unless the base class is specifically designed for inheritance, as a change to the base class might BREAK the subclass.
Have fun!
I'm trying to convert a Java Program to Android. I created a new xml Interface and most of the core logic is still running but since Swing is not present in Android i'm missing the class javax.swing.tree.DefaultMutableTreeNode in particular.
Is there an easy way to replace the class with something equivalent in Android?
Thanks in advance!
Taber
There is no Tree view on Android, so there is no directly comparable class.
But, if you need to use a tree-like data structure to display it's data in UI, for example in ExpandableListView, then you can use CursorTreeAdapter.