Android studio -- how to include resource file in Java package using gradle - android

I have an android project which requires to include a non-java resource file to included in the final package with Java class.
If you are using Eclipse, it is very easy, just put the non-Java resource file in the same package of Java classes, Eclipse will automatically copy the resource file to the destination.
I tried the same method in Android Studio, which uses Gradle for building, but it doesn't pack the resource file in the final Java class package. I have no control over how to read the resource file, it must use class.getResourceAsStream() to read the resource file.
Is there any way to pack resource file in Java package in the final product? Any suggestion is much appreciated.

First, Create a java resources folder.
Second, Create the same structure with a package name of a class.
for example, picture blow, you will have the same folder structure if you have a Tobee.class in a com.tobee package.
You can read a file in a Tobee class by calling a getResource or getResourceAsStream method.
getClass().getResourceAsStream("jar_properties");

Alright, I have solved the issue: pack everything in a jar including resource files then import this jar into the Android project.

Related

Can I make R file path independent from packageName?

For exampel my application has com.example.app1 packageName. When importing R file it hase com.example.app1.R path. If I change packageName to com.example.app2 it will have com.example.app2.R path. So it must be changed in all files (Eclipse does it atomatically) but I don't want to do it. So can I have path to R file independent from packageName?
No, you can't do that.
R is generated at compile time by the Android development platform as a class (and a collection of inner classes) belonging to the package your.app.package.name. As of today (API 20), no user preference or setting allows changing the way it is generated.
Can I make R file path independent from packageName? NO
Eclipse will immediately generate the resource file R.java related to your package name, if it changes the imports must chage since R.java file have all the resources IDs provided to resources related to your package.

how to change classes in android.jar file

I am new to android programming.
I have got the Google native contact code and imported it in the eclipse IDE .
After importing there are errors in each and every Java file and errors are like, some classes are importing the other classes that is not in the project not even in android.jar.
So I have downloaded android-apps-4.4_r1.jar file and added it to the project(external jar). By doing this most of the errors have been removed but still there are many errors to remove.
e.g.: one class has been imported android.provider.ContactsContract.ContactCounts
and there is a class file in android.jar folder named android.provider.ContactsContract but I think there is not any variable ContactCounts in that file, so I have searched this file (android.provider.ContactsContract) in the Internet and there I found it so my question is how to replace that class file (exist in android.jar folder) with the new file that I have found it on the Internet or there is some other way to solve this problem?
Check this url for references
http://docs.oracle.com/javase/tutorial/deployment/jar/update.html.
jar uf jar-file input-file(s)
Here 'u' means update.
ContactCounts is hidden from the public. Notice the #hide attribute in the source code of the class.
Source code: ContactsContract
You cannot use the hidden classes without extensive work and you will be vulnerable to the code being removed, not present, or changed in future or previous versions of the Android API.
You could try to copy the source code for the hidden class and use the local version of it in your project.
That means:
Create a new file called ContactsContract.java in your project
Copy the contents from the file linked above
Rename the package to match your project
Import your local class where the file is used

use resources from jar (Facebook SDK as jar)

I'm using Facebook SDK project on my own project by adding it as a library inside my workspace, like:
Project properties -> Android -> Add.. -> Facebook SDK
Like this i have access to classes and resources inside this project. For example in one of my layout files i have:
android:drawable="#drawable/com_facebook_loginbutton_blue_normal"
But if i want to change the way i use this Facebook SDK project, and simple export it as jar file, and add it to libs folder in my app, i have the error:
error: Error: No resource found that matches the given name (at 'drawable' with value '#drawable/com_facebook_loginbutton_blue_normal').
How to access the resources inside my jar file?
The short answer is, you can't. The Facebook SDK is an android library, and is being shipped as such, and should be used as such. Trying to import it as just a jar file will not work. Do you have a specific use case that requires you to only use jar libraries?
From what i can understand..
That,s because that drawable resource is in different package than yours, try accessing it with full package name
EDIT
What I meant was add
android:drawable="#packageName:drawable/com_facebook_loginbutton_blue_normal"
Like this:-
android:drawable="#com.google.android:drawable/com_facebook_loginbutton_blue_normal"
I don't know what package name is defined in Android SDk so change package name accordingly.
Hope this helps.

How to build an Android project to jar that included other project

How can I build one Android project to jar that is can included in other android project? When I use eclipse export an Android project to jar and use the jar to other android project ,The "R" file and the resource files will be error。
Android resource files (which are represented in your R.java files) cannot be put into Jars. That is why libraries such as ActionBarSherlock require to be opened as library projects, instead of simple Jars.
You can build Android Library Project to jar, but within library project you should prevent using R class, Instead of R class you can use reflection to load resources.
For example if you need to load string from strings.xml file
int id = context.getResources().getIdentifier("string_name", "string", context.getPackageName());
String value = context.getString(id);
But this is what they mentioned about getIdentifier method:
Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
You can build Android Project to jar file but don't use any Resource file.
B'coz when you will try to use that jar file in your application at run time Resource not Found exception will come.
So while creating jar file please unchecked res,gen,Manifest, etc.
You can Define your Base Activity class, Base Interface, Database Manager, GPS Manager
in your library project and use it in your our application it will surely work.
So see the below screen shots.
Thanks

Eclipse Import Custom Class from /src

Hey I'm having a pretty basic problem importing classes into my Eclipse based Android app. I have included the class directory into the /src directory where I figured the classes would be easily included, but Eclipse is saying it "cannot resolve the classes".
One thing I noticed is that the project folder that I included and the subfolders are just regular folder and not package folder icons. Is that a problem? If so, how do I change them to packages? This is an external git repo, so I'd rather not do anything beyond just including them in my project.
Here are some pics of how I set up the project to help:
The Activity where the error is:
Here is the package structure in the project, and the package called "android-utils" where the files I'm trying to import live:
Here is how I'm importing the files:
Btw, these classes are just some utils and I'll be improving and adding to them during the development process.
Let me know if you need any in more info to help me get these files imported the right way. Thanks!
Did you try dragging the .java files from Windows Explorer directly onto the package in Eclipse you want to put the new classes into? It should then ask to copy or link to files.
As your folder contains only java files Add them as Jar File in your Buildpath project, and your problem will be solved.
Also your jar file you can put it under the /libs folder
see this link to lean how you create jar file

Categories

Resources