How to get reference of application project into library project? - android

I am going to make application with library project. My application project referred a library project.
When we build the dependent application project, library projects are compiled and merged with the application project, so application project has all the resources which library have.
But my requirement is slightly reverse from above. I want application project resources reference into library project. I want to access R.java of application project into reference library project.
Is there any way to get reference of dependent project into library project?
Thanks in advanced.

No, there is no way as far as I know. And it is also not the idea of "Lib" projects that they depend on a App Project. It is only made for a vice versa dependency.

Related

Resources in Android Library

I have problems with accessing resources in my android library. I have created library project with some resources (com.library) and then I imported module into application project (com.app). So I have Android Studio project with library and application.
When I want to access some library resource (com.library.R.string.label) I get error during compilation
package com.library.R does not exist
When I want to run some library method from application which contains/uses R.string.label I get
java.lang.NoClassDefFoundError: com.library.R$string
I added library to application gradle file using
compile project(':Library')
and from IDE perspective looks everything fine and R.java is created with references to resources. I want same usage as I'm using for example android.R.string.cancel or similar libraries in my application project.Where I'm doing mistake? Thank you for help.
PS: In the future I want to have my library project as aar package.
As soon as you add a library to your project all resources will be "copied" to the R-file of your app. So if you want to access a string from your library you don't do something like getString(com.library.R.string.some_string) you simply call getString(R.string.some_string) instead.
In terms of the library method: Would you mind sharing some code with us? Currently I can't imagine what's going wrong.
Problem was in wrong package name in gradle configuration files (after renaming).

Adding library with and without resources in android

I'm new to android development. I wanted to implement Navigation Drawer feature in my application, which requires android.support.v4.jar file to be included in my project. I'm using Android Studio to develop my application so I included the compile "com.android.support:support-v4:18.0.+" in my build.gradle file as mentioned in https://developer.android.com/tools/support-library/setup.html. My project is working fine, I was able to successfully include my library. I did adding library without resources.
What I couldn't figure out is the difference between adding library without resources and adding library with resources. Does adding library with resources mean including some sample project?
Thanks
For Android Studio projects using Gradle (which is what your project is), there isn't an important distinction between libraries with resources and without resources. The Gradle build system's support for Android is more advanced than anything available to Eclipse and can support libraries with and without resources the same way (you just add the compile statement to your dependencies as you have; you can also do it more easily via the Project Structure dialog).
In Eclipse, if you want to include a library with resources, you have to add it as a project (similar to an Android Studio module) to ensure the resources get included directly; there you can't include resourceful libraries as simple jar files.

Android Studio - Common Codebase

So I recently migrated to Android Studio from Eclipse. For the most part, it's better, but I haven't found a good way to maintain a shared codebase between multiple projects.
What I want to do is be able to share some code between several of my applications. Each application is in its own project. From what I've seen, most people add it as a library module in the application's project. The problem with that is the module is accessible from only one project. The other projects within which my other applications reside can't access the library.
It seems to me like there should be a mechanism for creating another library project and then allow each of the application projects to access that code. This worked in Eclipse, where I would create another project in my workspace, mark it as a library, and then have the other projects reference it. I would be able to change the code in the library and then all of the projects referencing it would automatically build with the updated code.
Is this something I could do in Android Studio?
Yes this is possible:
Create the project you would like to have as a shared library - we'll refer to it as sharedProject.
Now in the project that you want to use this library open settings.gradle and paste the following:
include '..:sharedProject:app'
Open your build.gradle and paste the following under the dependencies element:
compile project(':..:sharedProject:app')
You can use this technique for as many projects as you'd like to refer to your common codebase in sharedProject. Note that this assumes your project and your sharedProject directories are in a common workspace directory (which is almost always the case).

How to test an android library?

I am writing an android library project and trying to test it. I found testing a library project
more difficult than it has to be using my current method.
Right now, I am exporting the library project into jar file. Then I put it into the libs folder of the test project and the test target project. Then I add it to build path of both projects and run the test.
Every time I make a change to the library I have to do this again. Is there easier way to do this?
There is some reference to this in the google docs. Check out http://developer.android.com/tools/projects/index.html#testing
Basically, the easiest way is to write an application that uses the library project and then add unittests to that application.
When creating the tests via eclipse, you can choose to test modules from within your library project. This way, changes to the library project are automatically applied.

multiple dependent android projects in eclipse

I just started to play with android dev and java+eclipse is pretty new to me. I managed to create simple project and run it on my device. Now I want to create simple game (more of them actually) and I would love to use shared code base for all of them (game loop, initialization, etc..).
Problem is that I have no idea how to correctly do this. I created android project called engine with all basic stuff that I need and made it work on device. Now I tried to create another project in same workspace called mygame. Main class (activity) of mygame is MyGameApp which inherits from EngineApp (main activity of my engine project) which inherits from Activity.
I added engine project into required projects in mygame build path tab in properties. Problem is that when I try to run this project it crashes on ClassNotFoundException trying to find my MyGameApp class.
Any help (or pointer to some articles that explain how this is done) is greatly appreciated. few hours of googling didn't help much :/
You need to set up an Android Library Project
An Android library project is a development project that holds shared Android source code and resources. Other Android application projects can reference the library project and, at build time, include its compiled sources in their .apk files. Multiple application projects can reference the same library project and any single application project can reference multiple library projects.
The docs go on to say how to convert an existing project to a library project:
You can also convert an existing application project into a library. To do so, simply open the Properties for the project and select the "is Library" checkbox. Other application projects can now reference the existing project as a library project.

Categories

Resources