Can I not have multiple packages in my android project - android

I am busy reading up of fully qualified names and packets etc,
In my project I have broken everything up into packages just to try and organize my project, but I have now realized this doesn't quite fit with the android manifest element as you can only provide one package?
Can I have multiple packages in my project like so?

You should specify a main package, and that package may have as many sub-packages as you like:
com.example.android could be a main package.
then you could specifiy the following:
com.example.android.activities
com.example.android.adapters
etc...
Of course, you may add classes to the 'root package' too without any hassle.
Check out Java package naming conventions for more info, Android follows these conventions as well.

You have to Specify a common package name for your application. eg:com.example.rhino68mobileapp and start every other packages by using this eg:com.example.rhino68mobileapp.utils

Yes you can use the multiple package in android, Only you have to play with access modifiers within your application, so that your one package class or object can communicate with other package class when desired.
Java provide different access modifiers:
Visible to the package. the default. No modifiers are needed.
Visible to the class only (private).
Visible to the world (public).
Visible to the package and all subclasses (protected).
For more read here
Edited:
You can define the main package(Application main package) and other package into your manifest like this:
<manifest package="com.example.mainPackage">
<application . . .>
<activity android:name=".packageOne.ActivityOne" . . . />
<activity android:name=".packageTwo.ActivityTwo" . . . />
<activity android:name=".packageThree.ActivityThree" . . . />
</application>
</manifest>

Yes you can.
The application package from the AndroidManifest.xml identifies your app.
But your Java code can be organized as you please.

Related

Android - How to extend application class mid-project for LeakCanary

TLDR:
I have to extend the application class but don't know where to start.
Ok... I know this is a dumb one but I have to ask it.
I need to install LeakCanary and I see in their documentation that it requires me to put code in the Application Class.
I understand how to make a java class that extends the application class, but I have some questions before I do this.
SITUATION:
I have 9 classes in my app already.
The entry point to my app is MainActivity.class (figures, right?)
I have not had to extend the Application Class yet.
App name is "CST-ALERT"
QUESTIONS:
Do I just create a new Java class in my java source folder and name it the same as my overall
package name? What do I name this new class?
Do I need to adjust the manifest now to have this as the entry point
instead of MainActivity? Which I imagine will change things quite a
bit for me.
Do I have to change anything else in the manifest? I assume I will
at least have to declare it in there.
Hopefully my situation and questions will give you an idea of what I am asking.
Do I just create a new Java class in my java source folder
Yes.
and name it the same as my overall package name?
If by "it", you mean the Java package, you already have a directory for your existing Java code. Put your subclass of Application in there, unless you have a clear reason not to.
In this trivial sample app, I put my Application subclass in the same directory as my launcher Activity, which is the directory for my Java package (com.commonsware.android.button).
What do I name this new class?
That is up to you. I called it CanaryApplication. If you think you leak a lot, call it CoalMine. So long as it is a legal Java class name, it doesn't matter.
Do I need to adjust the manifest now to have this as the entry point instead of MainActivity?
Um, well, I wouldn't describe MainActivity as the entry point. Regardless, MainActivity is left alone. You need to add an android:name attribute to the <application> element, identifying your Application subclass.
Do I have to change anything else in the manifest?
No, that should suffice.

Android Manifest: Does Activity package still need to be under the apk's main package?

I'm coming into a large preexisting Android project. The manifest looks something like this:
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.company.group1.package1">
<application ... >
<activity android:name="com.company.group1.MyActivity />
<service android:name="com.company.group2.blah.MyService" />
<provider android:name="com.company.group3.etc.MyProvider" />
... more fully-qualified activities ...
</application>
</manifest>
Basically the entities are all over the place, package-wise, and all fully-qualified. This breaks all kinds of convention of course but somehow it works just fine. I've tried to find a statement from Google that this is a bad idea but I have not found any official recommendation against it or what kind of problems it would cause.
Renaming the package com.company is not a good option because the company has multiple apps.
My question is: Is there any reason other than convention to organize the project in a more sensible way? I anticipate pushback on a massive renaming because of issues with source control history and whatnot.
No, an activity's name doesn't have to be under the apk's main package. It is possible, for example, to specify an Activity that is defined in an external jar that uses a totally different package name.
The actual requirement according to the AndroidManifest.xml documentation is that an activity name "should be a fully qualified class name".
However, as the doc continues to mention, in most cases, there is a shorthand where you don't have to specify the fully qualified class name all the time. And this is by preceding the name with a '.'. (ie. "<activity android:name=".MyActivity />")

Call the other package's activity in the Preference

I add new class in the android/packages/apps/phone/src and I want to call the class in the ohter application.
Assume that the new class is SS.java
I added
<activity android:name="SS"></activity>
in the AndroidManifest.xml file in the android/packages/apps/phone folder.
The application which want to call the SS class has a list and I put the code in the xml file;
<PreferenceScreen
android:title="#string/SS">
<intent
android:action="android.intent.action.MAIN"
android:targetPackage="com.android.phone"
android:targetClass="com.android.phone.SS" />
When I choose the menu in the list, the phone show Class Not Found Exception.
The message isUnable to find explicit activity class {com.android.phone/com/android.phone.SS}; ....
Please let mw know what is the problem
Are you talking about contributing to the AOSP or building custom ROMs? If you are, this is not the most relevant forum. Try http://groups.google.com/group/android-contrib?pli=1.
If you are not, then:
The android/packages/apps/phone is a part of special apps shipped with the OS. You can't add classes to this package. Even if you do and compile the AOSP code, no phone will have your classes, so no application can use your classes.

Android Manifest: Why sometimes ".<classname>" instead of just "<classname>"?

What does the dot mean?
most of the time I just write that:
<activity android:name="OneActivity" ...>...</activity>
But sometimes I see in the autogenerated file:
<activity android:name=".OtherActivity" ...>...</activity>
And also in the Docs for Service I see that they write
<manifest ... >
...
<application ... >
<service android:name=".ExampleService" />
...
</application>
</manifest>
But I never saw any differences in trying the one or the other.
If you take a look at above, there is package definition like
package="app.package.name"
The class name with the dot means that that class is under the defined package.
If you have another package like
app.package.name.another
and there is a class in that package, you have to defined class name like
<activity android:name=".another.activityname"
From the the Android Dev Guide < activity > reference
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the element. There is
no default. The name must be specified.
Credit: jaywon from Activity Name in android Manifest in Stack Overflow
You can find some differences if you create more than one package, android checks for the class in the default folder that you might have mentioned while creating project.
As for the service, it automatically appends "services" to the package name and looks for the service in it. so its more like relative and absolute paths, if you place your service in different package name, you will have to mention the whole package path with class name. It applies to receivers too..
For picking up any activity Android requires fully qualified name...
For that Our Manifest files has attribute (i.e. package="com.test")
So to make it fully qualified we put dot before activity name (i.e. android:name=".FirstActivity" )
If you do not want to use dot before every activity then simply put dot after your package attribute in manifest tag.(i.e. package="com.test. ") and write activity name without dot (i.e. android:name="FirstActivity" )
So that it can overall make a fully qualified name (i.e. com.test.FirstActivity)
The dot before the name means that it is a hidden file that will not be seen by others. You can see on youtube how to hide files by android cell.

What's the "dot" for when registering an Activity

I'm kind of a noob at programming for the Android OS. I noticed in the books I have been reading that the authors have placed a "dot" in front of the activity name when registering their activities in the manifest. I've looked around the Android developer site and I can't figure out why we need the "dot". Does the "dot" actually server a purpose? Do I need it? I have included an example below. Notice the "dot" before "NewActivity":
<activity android:name=".NewActivity"></activity>
As you have noticed the point is not necessary but it basically means: the activity class lives in the same package of the app. So, if your app package is: com.my.package then:
.YourActivity means that your class is inside com.my.package.
YourActivity means that your class is inside com.my.package (same as above).
.activities.YourActivity means that your class is inside com.my.package.activitites.
You can even do something like: com.my.package.activities.YourActivity which is useful when you want to have different versions of your app and use Ant to change the references to the package automatically.
http://developer.android.com/guide/topics/manifest/activity-element.html#nm
android:name
The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the <manifest>.
So given ApplicationManifest.xml:
<manifest
...
package="com.stackoverflow.android.geotask"
...>
<application ...>
<activity android:name=".view.TaskListListView" ...>
...
</application>
</manifest>
then since android:name=".view.TaskListListView" has a leading period, so it is interpreted as android:name="com.stackoverflow.android.geotask.view.TaskListListView".
That dot will append your package in your application manifest.
If your package name is com.app.demo.
<activity android:name=".HelloWorldActivity">
It means that Activity is lying inside demo package.
You can replace this with
<activity android:name="com.app.demo.HelloWorldActivity">

Categories

Resources