How to break up one android package into several packages - java

I have a legacy app with 168 modules in it and I would like to break it up into several java packages from:
com.mycompany.ediary
to
com.mycompany.ediary.util
com.mycompany.ediary.data
com.mycompany.ediary.services
...etc.
This would be to better organize the code. I read where it is ideal to have less than 30 files per package and as you can see, this is way over this. What is the best way to do this? I am currently running Android Studio 1.5

As #Antoniossss says "Just do it!".
Following a slight explanation...
In the older Eclipse days there was a link between the Java packages, and the Android manifest package (now referred to as the "application ID") in the app. This was enforced by Eclipse (almost) requiring that you put your code into a Java package that was named the same as the application ID.
Changing the root (Java) package of your code in Eclipse got a bit tricky, and generally you would want to do that by using the Android Tools menu. This made sure that the R class was correctly imported after the change.
Android Studio makes it much easier to separate the 2 concepts. Plus the terminology makes it easier now, since the accepted term "Android manifest package" is now the "application ID".
This gets set in your gradle build file:
applicationId "mobi.glowworm.demo"
How you organise your Java code does not depend on this value at all anymore. You can use any package structure you want to.
So, yes, "Just do it!"

It wasn't as easy as I thought in Android Studio, yet easier than I thought at the same time.
Adding new packages is basically adding new directories to your project structure, by adding new "packages" and letting Android Studio do the work of refactoring it all in your code. I did have some collisions with the manifest-merger-release-report.txt file, but after manually fixing those and updating my manifest as well, I was able to break up the files into a more organized format. I didn't have to touch the gradle files at all.
Right click on the java folder in your app, select New -> package - then enter in the full package name: com.mycompany.ediary.utils. Then cut and paste the files you want to add to the new directory/package, when prompted for reformatting, hit "reformat", then allow Android studio to do its magic.

Related

Android app dependencies ClassNotFoundException

I'm new in Android development. Not very new in ordinary EE Java and Gradle.
So I am trying to build a REST consumer app for Android. I have a (1) .jar file I will need (contains DTOs and api consumers). That (1) .jar has dependencies on other (2) jars from Nexus repo. These (2) do depend on other (3) jars (e.g. apache commons lang3, etc.) from maven repos.
The funny thing is that... The app compiles and install just fine. But on runtime it occasionaly tends to lose some classes from (1) (at least (1) -- dunno about (2) and (3) yet).
I know SO does not like pics in questions, but in this case I believe pics = clarity.
See below. Same runtime, same jar. Except one of the classes is not loaded, and another one, sitting right next to it, is available.
Why is that so? What did I do wrong?
One more thing... If I attempt to instantiate one of the missing classes (there's more than one) in main activity (inside onCreate()), sometimes that class appears in the classpath and creates an object just fine. But as ridiculous as it may sound some other class goes missing! It feels like Classloader has limited number of slots for classes it could possibly handle.
EDIT:
Okay, it gets better. It appears that for some reason gradle decided not to include some files from (1).jar into generated .apk...
Any ideas why?
Gradle 4.1. Linux Mint 18. Android Studio 3.0 Canary 8
Left - decompiled APK structure; right - original jar
Probably, you have ProGuard activated. ProGuard removes from the final .apk the unused classes. ProGuard

How to rebuild Android app in release mode with another package name by command line interface?

Hello im making an generic app that is configurable by xmls and files on res. Im trying to make differents apks from the same source of code. The problem is that im triying to do this by command line interface and i still dont find a way to change the package name and then build it in release mode.
I created my custom build.xml ant file and I tryed this solution,it generates 2 apks: MainActivity-debug.apk and MainActivity-debug-unaligned.apk but didnt work for me since the apk just built on debug mode and didnt even ran on emulator throwing [ INSTALL_FAILED_INVALID_APK].
So is there a way of doing this? The main problem isnt about debug or release mode since i think i just will have to use ant release command, is about creating an apk with different packages names so i can have both signed and running in same device.
Thank you!
If you set your project as a Library project, you can use a facade style Project with it's own AndroidManifest.xml and overriding /res values to create a "custom" set of projects which derive from your main "Library" project.
Facade projects created in this way act the same in terms of functionality but can have different styles, package names, internal id's, colours, images (or even expose different intents or receivers).
As commonsWare proposed i used Gradle for this solutions.
I used this tutorial for Gradle with android.
I used this answer for building apps in release mode
I used flavors also as commonsWare said, read this
Thanks a lot!

jar and package won't work

I'm building a small game application. I've build the whole engine using java and seperated the engine from the logic, and it worked perfectly fine as a console and a swing-UI application.
now I want to migrate it into android and for some reason my emulator crashes each time I try to create any instance from the package I've build.
I added it to the project as a jar file (and added it to my path). afterwards I added the *.java package to the android project and it still crashes.
your help would be much appreciated.
There may be a million reasons for this. Most likely reasons are:
You compile your code to Java 7 (you have different class format)
You use a dependency that is not present in the Android environment
Try to add your source to the source folder of your Android project, and let the Android environment compile it to you. Do not use the jar file. That will work for sure.

What is the best way to include another (non-android) project in an android project under eclipse

I'm new to android programming. I'm trying to include code from a non-android project in an android app. This contains shared code used by a lot of my other non-android apps.
I was wondering what the best way to do this is from a code maintenance perspective. The shared code that I want to include does get modified from time to time and I want to keep the process of updating any apps I write as simple as possible (automatic if at all possible).
Am I better to build a .jar file containing the shared code and copy this to my app (eg: using an ant script) or is there a more streamlined approach.
I specifically want to avoid turning my shared code project into any kind of android project.
Make a jar and add it to the android project's build path. Whenever there is an update to jar, you would need to update it in your project and update (increase) version of your app to automatically allow users to download and update the update (android market would take care of that for you).
Its the most widely and maintainable way.
Hope this helps.
Once you have your .jar, you simply have to add it to your project (or update with the new .jar file if this library gets updated).
Once it is in you project (let's say under the /lib folder,
right click on the lib folder -> select build -> add to build path.
Unfortunately, I'm pretty sure there's no way to make an automatic update.
In the projects' properties go to "Project References". It will should you the other open projects and you can click whichever you want.
I haven't tried it, but if you change the non-Android code and run the Android project, I would expect the non-Android project to be recompiled automatically.

How to import android email source code into eclipse project?

I downloaded the android Email app source code [version 2.2] from here. I deleted the .classpath and .project file so that I could import an existing android project. Everything seems to have worked. However, I get quite a few recurring errors such as:
Attendees cannot be resolved to a
variable
android.provider.Calendar cannot be
resolved to a variable
Calendar cannot be resolved to a
variable
I have set the correct android version 2.2, what am I missing?
Thanks all for any help
I faced the same issue with Email. I fixed the issue, by adding the following jars into the build path and thus, all the errors were resolved.
../out/target/common/obj/APPS/Email_intermediates/classes.jar
../out/target/common/obj/JAVA_LIBRARIES/framework_intermediates/classes.jar
But you need to make the Application build using the platform.
Many of the included applications cannot(or at least could not) be built with the SDK, as they depend(ed) on non-exported functionality that is only "visible" during the system build. I'm not sure if Email is still in this category, but it was in the cupcake days.
There are three potential workarounds. Best is to rewrite those parts of the application to instead use functionality which is exported in the SDK, and thus fairly stable.
Next up is to use reflection to pick up the missing pieces at runtime.
Last is to make do-nothing dummy classes for the needed non-public platform pieces, within your project. These are enough to let the program compile, and at runtime they get rejected due to name collisions with the hidden system functionality, so you end up utilizing the real classes rather than the dummies.
The proper thing to do is to build the application with the platform build system, rather than the SDK.

Categories

Resources