Android Studio make my test module depend of application module - android

I want to make in Android Studio my test module to be dependent of application module, but can not achieve this, have mistake
Error:Dependency AndroidApp:app:unspecified on project TestsRobotium resolves to an APK archive which is not supported as a compilation dependency. File: D:\android\MEWE\AndroidApp\app\build\apk\MeWe.apk
In my test.gradle I added provided project(':app') in dependency tag (it is done by Android studio automatically)
How would I resolve this? I need dependency on src, res folders of my main app and need to know R file

This is now possible using com.android.test plugin. Works fine for Java and resources stuff.
An example by Google can be found here.
Basically you create a separate module for testing and define which module and flavor should be tested. I've just checked it and it has some issues with working with the IDE, but runs fine with gradle command line.

Related

Android Studio 3.0.0 Beta 4 doesn't complain at compile time but fail the build while running assemble for R file

I have multiple feature modules in my instant app project structure. All of my resources which are being used in the multiple features are residing in my core module.
Assume my core module package name is com.andorid.myapp.core.
And I have a feature1 module with package name com.android.myapp.feature1.
Now If I am using any resources from core module into the feature1 module, I am referencing it with simply R.string.something_from_core and studio doesn't complain anything even I have imported com.andorid.myapp.feature1.R not com.android.myapp.core.R it just works fine. But when I run ./gradlew assemble, It doesn't build successfully and throws an error which says the R.string.something_from_core doesn't exist.
Now my 100s of my project files are using resources from core and I am not able to find which resources are coming from core and which are from feature1 because studio is not complaining anything while writing code it just works. And also the build is only breaking when it tries to run build variants like assmbleFlavourFeature, rest everything is working fine.
So confused with no solution, would be great if someone can help.
Seems there is a problem with cache.
Try my answer here :
My answer
Hope this helps.

Phone and tablet Module vs Android Library

I want to make a separate module so I can share it between projects. The module is going to have android components. So upon creating a new module the options are Phone & Tablet module and Android Library. I wanted to know what's the difference between the two. thanks
What is Module? From the Add a Module for a New Device documentation:
Modules provide a container for your app's source code, resource
files, and app level settings, such as the module-level build file and
Android manifest file. Each module can be independently built, tested,
and debugged.
What is Android Library? From Create an Android Library documentation:
An Android library is structurally the same as an Android app module.
It can include everything needed to build an app, including source
code, resource files, and an Android manifest. However, instead of
compiling into an APK that runs on a device, an Android library
compiles into an Android Archive (AAR) file that you can use as a
dependency for an Android app module. Unlike JAR files, AAR files can
contain Android resources and a manifest file, which allows you to
bundle in shared resources like layouts and drawables in addition to
Java classes and methods.
You can't access the components from one module to another module.
So, if you need to share components between projects, you need to create Android library. But please remember, you can't run the library. You only can access and use it from the module. Furthermore, you can use Gradle Android Maven plugin to install your library locally.
Check the official doc
Resume:
Modules provide a container for your app's source code, resource files, and app level settings, such as the module-level build file and Android manifest file. Each module can be independently built, tested, and debugged.
Android Studio uses modules to make it easy to add new devices to your project. By following a few simple steps in Android Studio, you can create a module to contain code that's specific to a device type, such as Android Wear or Android TV. Android Studio automatically creates module directories, such as source and resource directories, and a default build.gradle file appropriate for the device type. Also, Android Studio creates device modules with recommended build configurations, such as using the Leanback library for Android TV modules.

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).

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.

Self-contained test library project cannot find the library classes

According to this SDK guide, unit-testing a Library project can be achieved by creating a standard application project, reference the Library project and then instrument the application for unit testing.
However, when I do this and launch the test application I get the message
No tests found with test runner 'JUnit 3'.
I'm using Eclipse and the Android ADT plugin, all latest versions.
Note: the projects compile just fine. The test project also installs fine to the emulator. But in the console I can see that it looks for <library>.apk, which of course doesn't exist since I'm compiling this as a library into the test project.
Anyone got this to work? And if so, what is the trickery here?
Update: after discovering and fixing a problem, which was actually including the test classes (!), the test runner now can find all tests. But, all the tests fail with the following exceptions:
java.lang.NoClassDefFoundError: <nameOfClassInLibraryProject>
nameOfClassInLibraryProject are classes defined in the library project. These classes should be compiled into the test project, and indeed, everything compiles just fine. But when running the test project, the runtime doesn't seem to find the library classes.
After much fiddling and wasted time in Eclipse I have managed to get Android Library projects to work.
According to the Working with Library Projects article:
Instead, you must compile the library indirectly, by referencing the library from a dependent application's build path, then building that application.
The problem was that I interpreted this to mean that the library project should be added to the Projects tab in Java Build Path. Doing this makes the test project compile since the library code is obviously available to the compiler. But since the library is not compiled into a .jar or .apk in itself, the library classes are never deployed to the device.
The solution is to not add the library project to Projects, rather on the Source tab, add the library /src folder using the Link Source... button. And yes, it is the library src folder, not the library project root, that must be linked into the test project.

Categories

Resources