Using android library project, with multiple src / res folders? - android

Sorry for what is probably a rather basic question, but I can't find the answer...
I downloaded a project from github, and I'm having trouble setting it up. I have virtually nill experience using code repositories. The project is is flagged as an android library project, and has two sets of source folders and resource folders buried in sub directories. Each src folder is dependent on its own resource folder.
You can see the project layout in the pic below.
Android gives me error that there are no "src" or "res" folders in the main directory. I tried using "build path > use as source folder" options to make android recognize them, but this leads to errors about not being able to find the resource folders.
How am I suppose to set this up?

I have used that library before. the problem you are facing is due to wrongly importing the whole repository as 1 project.
it is actually 2 separate projects:
two-way-gridview. this is the library project. you should import this subfolder on its' own.
sample. this is a sample project with examples how to use two-way-grid correctly.
you need to import both separately for it to work.

Related

Android Studio import library from github where no jar file is provided

I want to use the API from Maps.Me. I've
downloaded the repository from github
extracted the files
copied the lib folder
created a libs folder as ...AndroidStudioProjects[MyApp]\app\libs
pasted the lib folder into the libs folder
The instructions on github say I should then "add the library project to [my] project". My problem is that all the advice I can find here and elsewhere online assumes that I will have a jar file which I will now see in Android Studio. I don't have a jar file and I can't see a libs folder anywhere in the Project tab on Android Studio.
(The libs folder that I have created contains the folders and files that I would expect to see in an Android Studio project folder, namely res and src folders and AndroidManifest.xml, build.gradle, build.xml and project.properties files)
What do I do next?
One thought is that I should import the downloaded library as a new project into Android Studio and then create a jar file from it. Would this work? Perhaps there is an easier way?
In case it helps anyone coming to this thread in future, I'll describe how I have worked around this issue.
As can be seen from the comments, I failed to import the library into my app in Android Studio, despite all the helpful guidance from CommonsWare. If I were more familiar with Android Studio, and Gradle specifically, I hope that I would have been able to succeed. However, I have followed a route which, for me with my limited experience of Android Studio, is more straightforward.
For each class in the downloaded repository I added a new Java class to my app with the same name.
I did not include one of the classes which included a dialog and relied on a layout and other resources, since I knew that I would not need this class
I copied all the code, except in each case for the package name, from each class in the depository and pasted it into the new classes.
I removed from the code that I did copy the one reference to the missing class
the result was code that worked exactly (so far as I know) as if I had imported the library.
Obviously, the result is inelegant, but for an app that is solely for my personal use it is good enough. It works.

How to create an android library project with no resources

I want to create a library project with just java/Android classes - but no resources. Basically it will contain helpers for IO etc. I have manually deleted the resources etc - the project is here - but I wonder if this can be done out of the box - or is my way not proper
NB: I need Android classes - so creating a regular java project is not an option (?)
Edit : if there is no out of the box way is there any catch in deleting the (edit: contents of the) res/ folder and the support library ? I would appreciate a 1-2-3 procedure
You need to keep project structure, incl. having res/ foder with drawable/ etc, but these folders can be empty. There's no requirement for your library to reference any drawable.
Others suggest it's mandatory for "app icon" but your library does not need any <application> entry in manifest, so it does not need any icon. The same applies to values/ and layout/ folders. Your library project have to have these folders (as required by build process), but having no file in them (so basically keeping them empty) is perfectly fine and valid and meets your requirements.
I believe you should keep all the required folders (including res) but you can leave them empty. Here is a guide for it.
http://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject
I believe you cannot delete the res folder completely since the app icon is contained in it. However you can delete the default layout if you do not need it and any other content that is required for the app to work.
If you would like to access the Android classes you will have to create an Android project.
I hope this helps.
I have created a git repository to illustrate - I created a vanilla eclipse android library project (screens) added a git ignore (bin/) and then proceeded to delete unneeded resources (all the contents of the res/ folder) along with libs/ and assets/ and edit the manifest. The changes are in this commit for reference. Cleaning the project removed R.java along.
Will be adding more (.gitattributes and eclipse settings for android) but this is for now.

Android SlidingMenu library installation

I am trying to follow this tutorial.
The aim is to integrate the SlidingMenu android library. As per the instructions in the tutorial:
For non-maven project : Copy
library/target/slidingmenu-1.3-SNAPSHOT.jar into the libs directory of
your project.
I don't know how to use Maven and I don't want to introduce another layer of complexity.
So, where can I locate the library/target/slidingmenu-1.3-SNAPSHOT.jar file?
Update
With the exception of the Maven section, I followed the tutorial completely but got the following single error:
slidingmenu cannot be resolved or is not a field
The error is caused by this line of code:
slidingMenu.setMenu(R.layout.slidingmenu);
I'm a beginner but I know that's an issue of a missing resource. What I don't know is if it's related to the Maven issue. How can I fix it?
So without implementing maven, all you need to do is download that library from your link on github (here). It is only 4.4 MB.
On that github page, there should be a button that says "Download ZIP".
Once you save the zip file, open the zip file, and navigate the contents... inside the library folder, you can find the source files of the SlidingMenu solution.
The QUICKEST way to get this into your project is to copy the contents of the libs, res, and src folders into the respective folders in your project.
The SlidingMenu project on github cannot merely be a .jar file, since it contains visual assets as well (ie. items in the /res folder are needed).
Another approach is to create a separate project on your computer named SlidingMenu (create it as a "library" project) and then include this project into your own project as a dependency. The manner in which to do this varies by IDE (Eclipse, IntelliJ, AndroidStudio, etc). Hopefully you're using one of those IDEs.

Android: Source path not being included

In my project I have added some folders that I want included in my project for source files. But no matter what I do, the source never gets recognized. I tried refreshing, clean up, restarting but nothing works. Here is the project's source folder:
I want the folders integration/android added. This is how it is showing up in Eclipse:
And here is what my build configuration looks like:
I could be wrong, but it looks like you have added them as a folder, but not actually added the folder to your project.
The folder should show up in your src folder in the solution explorer window under your other package.
Try creating the package name in your project, then move all the src files into it.
And when its in the src folder, in its correct package, you dont need to include any extra build paths.
(You can have multiple packages in a single application)
Importing someone elses project that is over 2 years old is almost always problematic. I found the easiest solution just to create a fresh project and add the files manually from the old project.

how to reference an asset in a library project

In a class belonging to a Library project I call:
webview.loadUrl("file:///android_asset/info.html", null);
Unfortunately, this only works if I duplicate the file info.html into the Application's project asset folder as well.
Is there a way to tell an Android library code: "look for this file in the library's assets folder, not in the application's assets folder" ?
This answer is out of date, the gradle build system and AAR files support assets.
From the Android Docs:
Library projects cannot include raw assets
The tools do not support the use of raw asset files (saved in the assets/ directory) in a library project. Any asset resources used by an application must be stored in the assets/ directory of the application project itself. However, resource files saved in the res/ directory are supported.
If you want to include files from a Library project, you'll need to put it in the resources instead of the assets. If you're trying to load HTML files from your library project into a WebView, this means that you'll have to go a more roundabout method than the usual asset URL. Instead you'll have to read the resource data and use something like loadData.
This is now possible using the Gradle build system.
Testing with Android Studio 0.5.0 and v0.9 of the Android Gradle plugin, I've found that files such as
MyLibProject/src/main/assets/test.html
are correctly packaged in the final application and can be accessed at runtime via the expected URL:
file:///android_asset/test.html
You can achieve this by creating a symbolic link in the project's asset folder that points to the directory in the library project.
Then you can access as below:
webview.loadUrl("file:///android_asset/folder_in_a_libary_project/info.html", null);
Okay. Ive been stressing out and losing sleep about this for a while. Im the type of person that loves API creation, and HATES complicated integration.
There arent many solutions around on the internet, so im quite proud of what Ive discovered with a bit of Eclipse Hackery.
It turns out that when you put a file in the Android Lib's /assets folder. The target apk will capture this and place it on the root of the APK archive. Thus, making general access fail.
This can be resolved by simply creating a Raw Java Library, and placing all assets in there, ie (JAVALIB)/assets/fileX.txt.
You can in turn then include this as a Java Build Path Folder Source in
Project > Properties > Java Build Path > Source > Link Source.
Link Source
Click on Variables. and Add New Variable, ie VAR_NAME_X. location : ../../(relative_path_to_assets_project)
Click Ok
Now, when you build and run your app, the assets folder in the APK will contain your (GLOBAL Library) files as you intended.
No need to reconfigure android internals or nothing. Its all capable within a few clicks of Eclipse.
I confirm that Daniel Grant's approach works for at least the following situation: target project does NOT have an asset folder (or the folder is empty, so you can safely delete it).
I did not setup any variable.
Simply setup a LinkSource as follows (just an example)
Linked folder location: /home/matthew/workspace_moonblink/assetsForAdvocacy/assets
Folder name : assets
The "assetsForAdvocacy" is a Java project, (created with New- Project - Java Project) with empty src folder, and a new folder named "assets", which now provides the entire assets folder for the target project.
This is a fairly straightforward way within Eclipse to provide assets re-use across many different projects IF they do not already have assets, good enough to get going with. I would probably want to enhance it to become a content provider in the long run, but that is a lot more development.
My project accesses the assets with the following code:
String advocacyFolderInAssets = "no_smoking/"; //a folder underneath assets/
String fn =advocacyFolderInAssets+imageFilename;
Bitmap pristineBitmapForAdvocacy = getBitmapFromAsset(context, fn);
I use Motodev Studio 3.1.0 on Ubuntu. It would not let me 'merge' a new assets folder in the new assets-only project onto an existing assets folder in the target project.
If you want to use a setup where multiple derivate products are created from one library you might consider using svn:externals or similar solution in your SCM system. This will also do the trick that static assets like online help may be versioned seperately from the android source code.
I found this older question, it might help you, too.
This is the official way Google uses to archive this (from the above post): Link

Categories

Resources