I would like to use third party classes for which I have no source code. How do I instantiate these classes (which are in an APK on the phone) if I have no source code. Is it possible to do in Java? If not, how can it be done?
What exactly are you trying to do? If you can root your phone I guess you could pull the APK off and try to analyze what the classes are doing using something like Apktool..
How do I instantiate these classes (which are in an APK on the phone) if I have no source code.
If the classes are in your project (as a JAR or source code), you use them normally, like any other class. If you do not know how to do that, contact whoever wrote the code and obtain documentation for them.
If the classes are not in your project, you cannot use them.
Related
When I make an app with package name com.example.app, src/com/example/app/MainActivity.java is created automatically. I am new to Java and I don't understand
why it uses so many folders inside folders? Why isn't it just src/MainActivity.java?
In order to avoid namespace collisions and conflicts, it's a common best practice in Java nest source code within a folder structure that is the reverse of the internet site associated with it. If everyone created jar library files in the root /src directory, eventually you'd have a collision and the code wouldn't be usable.
For instance, if I have some fancy Android library and I provided a class called Button, in a Button.java class, and you also at times wanted to use some other library that also had a Button.java in /src, your project would not compile.
Thus, in order to let everyone have their own unique Button class, the convention that was adopted was for everyone to use their reverse domain name, followed often by the project name. So the Facebook SDK, fo instance, has /src/com/facebook/android/Util.java while my own project has /src/com/myapp/misc/Util.java and I can use and reference both in my source code.
I have been working for one of the project where I am required to do the following.
Current Status: I have developed an Android app for which we are currently distributing apk files to the users. But the problem is, if users want to alter any designs etc, we have to take that pain, instead, we want to make sure the view files are available to them, but the core logic is in the jar file. Now, creating jar file is not an issue, but we still want to secure the source code. So, I found ProGuard http://proguard.sourceforge.net/index.html#manual/introduction.html
Now, the examples there shows that apk file which will be generate will have obfuscated codes. But I just want my classes under the jar files to be obfuscated.
Any help will be appreciation.
You can limit which classes should be left original and which ones should be obfuscated.
For example you may not want to obfuscate Activities or Services or Android will not be able to find them anymore ;-)
Reference: http://developer.android.com/tools/help/proguard.html#configuring
You can also use proguard on its own and pre-obfuscate your jar file before using it in the app itself.
I am refactoring my code, and I am organizing my classes into packages. But I find that my program doesn't run when I create my own subpackages (Java, not android, packages). Is there anything special I need to do if I have several internal packages in my code?
The only Android-specific change you should have to make is updating your AndroidManifest to use the full class names (e.g. com.example.ClassName). Post your errors if this doesn't fit it.
(Of course, you will also need to change the imports just like in any other Java application.)
I would like to build a library and be able to distribute it as a jar without having to give the source. In the library, layouts are used for specifying the UI, however android doesn't seem to facilitate easily bundling a jar and distributing it, as it doesn't properly scope the resources (anything in '/res/*') in this jar file, the references made with R.xxxx within the jar don't work.
I can give the xml layouts and other resources to the client and ask them to put them into their resources directory, thus their R.java would have these references, now, how can the client pass this R.java to the library when invoking a method in the library?
Guess, answer to part of the question would be through answer to 'How to pass class in java?"
Yes, I am new to android and java too.
Thanks,
Krishna
If you have just simple layouts you could also create them in Java and not define them in XML.
It's not so nice but you don't have to distribute some other files.
I have developed some reusable android component which is basically a class . This class has some resource dependencies e.g. some png drawables, some xml layouts etc. So this class referenced the auto-generated R file.I would like to distribute this code in a single package like jar file to other developers for use in their applications.
I have read that the only possible solution is to distribute code together with all my resources, which others have to copy to their "res" folder (source).
So I created a jar file having the class file (say MyClass which is in the package com.xyz.android.app) and resources and tried to use this in my new application.
So I added the jar file to my new applications build path using add external jars option in eclipse and copied all the resources to my new application's res folder. (The activity class say MainActivity of my new application is in com.abc.myapplication package, just for the case if it may helpful)
But when I run this new application there is java.lang.ClassCastException in the MyClass class. I tried to debug the application and then I found that in the MyClass class, there is "R cannot be resolved" problem.
Then I changed MainActivity's package to com.xyz.android.app (which is not the way, other developers will be happy to do), But again the same problem.
But When I just copy the source java file such that both MainActivity.java and MyClass.java are in com.xyz.android.app package then application runs fine.
So if I need to distribute such that other users need not to bother these package naming things,
how can I accomplish this? Please help !!
Edit
In android.jar, there are also some resources. How are they referenced in a project? How are they used in android classes? There is also android.R file?
Is it not possible to do the same thing i.e. to make jar file like android.jar for my reusable code?
As Nic Strong noted, there is nothing much built into the project to manage this, and current indications are that the upcoming changes to the SDK tools may only help a bit.
I am organizing some other tools to help deal with this problem. I hope to have developer documentation published in a few days.
This is not so easy to do at the moment. The problem is the project using your jar does not know to look in there for drawables etc.
The good news is it should soon be possible (hopefully with SDK 2.2 which is rumoured to be released at IO next week). See this blog post http://mobilebytes.wordpress.com/2010/04/27/android-tools-version6-coming/
I've been playing with Mark Murphy's ParcelHelper class at http://github.com/commonsguy/cwac-parcel. In a nutshell, it's a collection of methods that let you access the components of 'R' by name without needing to import 'R' into your code.
If I understand your question right, this is exactly what you want. You may still need to copy some resources, such as styleables, into your project.