How can I share a Module's androidTest "Robots" with another module? - android

I am a big fan of the Robot pattern for keeping UI tests clean. But how can I reuse Robot classes across multiple modules?
:lib-with-ui . // has Robot classes in `src/androidTest`
:app
I fully test my lib-with-ui with the Robot classes, however I want to reuse those same Robot classes in the :app module for some quick smoke testing.
For why the smoke testing is useful, it's because we want to test the integration of all our components as well as catch any possible proguarding issue that may occur (we have a special flavor of our sample app that utilizes proguarded AARs of our libraries).
We've tried a variety of approaches to get this to work, but it has all lead to weird resource issues.

Robot classes are defined in module 'lib-with-ui' and you want to use this in module 'app'?
Add lib-with-ui android library module as dependency to app module. Any code and resources in the Android library is now accessible to your app module, and the library AAR file is bundled into your APK at build time.
https://developer.android.com/studio/projects/android-library.html#AddDependency

Related

Should I use Dagger2 in library project? Will it cause issues for application?

I am currently working on Android library project. Currently we have pure dependency injection(without any Frameworks). However, we expand our library functionality: providing more modules, separating code into new modules, dynamic delivery and etc. This is why our current DI will not suit our needs. We would need to invest tons of resources into that.
I did not used Dagger2 in the beginning, because I thought that library should have few dependencies. I am comfortable with Dagger2, so it won't create any issues.
However, what I fear is various issues, which my occur while integrating our library.
I wanted to ask, if it is possible to get Android gradle, or any other issues, if application does not use Dagger2 and library does?
Will integration issues occur if both application and library, uses different versions of dagger2?
Will integration issues occur if application uses another DI framework, like Coin for instance?
Thanks for answers!
Yes, you can use Dagger 2 for library project.
Dagger 2 is basically used to resolve dependencies into independent modules for better unit testing and various other optimisations of the project both library or user app.
No, integration problems doesn't occur if you can handle you Dex files. Also you may try using your library's files (external libraries) to avoid such errors.
No, integration error should not occur if two different external modules are used. But on must ensure thay if any common external module is being used by both DIs, both DIs must have same version of external module or well Dex managed.

What dependencies should one be putting in each module of an instant app?

I'm in the process of writing an instant app for others to learn how to write an instant app and hoping to get some ideas on how to best structure the app for dependencies.
Now reading the Android developer docs on project structure and referencing the diagram below:
I'm wondering with the new gradle 3.0 dependency configurations, what libraries should live in which modules?
Base Feature
I was thinking pretty much anything in the base feature module should be using the api gradle configuration since the base feature module essentially compiles down to an AAR library file. One question I might have for this module, if one was to use ROOM would this be the module to place it in?
Feature
Now in the feature modules, it is my understanding that everything should be utilizing the implementation gradle configuration since these modules should not leak there dependencies out to any other modules in order to truly make them independent from one another.
Just looking for some confirmation of my understanding and also any ideas to help with the project. Here is the github repo if you want to check out the code I got so far. It is really simple at the moment, but I was thinking about messing around with the Star Wars API using Retrofit.
Thanks for any help and gladly accept any contributions if you want to to try and make a pull request yourself for any other concepts in making an instant app that others should know.
Shared details in your question are correct. Consider some of the below suggestions which add to the points mentioned by TWL:
Adding certain libraries to specific feature module which should
be included in the feature module only, instead of being added in the
base APK.
For example, let's say you have an application that depends on
libraries X, Y, and Z. Initially, you may pack all the libraries in
the base module by placing all the dependencies in the base
gradle.build file. But if only the code in the feature module requires
library Z, it makes sense to move that dependency from the base module
to the feature module.This works as long as no other feature modules
depend on the same library. If multiple feature modules use the same
library it definitely makes sense to keep it in the base module.
Taking care of Transitive dependencies.
Transitive dependencies occur when the library your project relies
upon depends on another library, which in turn may depend on yet
another library. Sometimes those transitive dependencies may contain
unexpected surprises such as libraries you do not need at all (i.e. a
JSON processing library you never use in your code.)
I hope this adds some information to your query,
I'm wondering with the new gradle 3.0 dependency configurations, what libraries should live in which modules?
Some of these links can also be referred for additional data:
Android Instant Apps(best-practices)
AIA structure
As mentioned by keyboardsurfer, your dependency assumption is in the right direction.
Base is at the root and acts like a library shared by all the
non-base feature modules, so its shared dependencies should be set with
api so that modules that depend on it can also access them. (though, base doesn't have to act only like a library, it can
also be a feature APK itself)
Features, as an instant app, each one extends out to the end as its own APK, so there's no reason it should be leaking its dependencies to any other modules and therefore dependencies should be set with implementation here.
Within the Google Samples, the cookie-api and install-api are some samples that more clearly demonstrate the dependency configuration usage as how I explained above.

Android Studio 3 difference between library module and feature module

In Android Studio 3 there are at least two new module types. First is Instant app module and the second one is feature module. With Instant App module it's quite obvious but feature module from my perspective is the same as the library module. So what is the real difference between library and feature modules and when I should use library module and when feature module?
I would complete Marcin Orlowski scheme like this.
You could picture library module in the same way as dependencies of a given feature or base module.
Hence the library modules will not be packaged in Instant APP APK.
A feature module is a module that applies com.android.feature plugin.
This module type has a dual nature:
When consumed by an application (com.android.application) during build, it produces an aar and works just like a library
When consumed by an Instant App APK (com.android.instantapp), it generates an Instant App APK
Developers should write feature modules just like library modules. The tools provided are responsible for applying the correct nature when used during a build.
In the simplest case an Instant app can have a single feature module.
If there is more than one feature module, these feature-to-feature
dependencies can be defined through the api configuration. In any
case, there must only be a single base feature which is marked with a
baseFeature attribute.
Main source: https://codelabs.developers.google.com/codelabs/android-instant-apps/#3
This all for Instant Apps so you only need it if you are making your app supporting instant app feature
See https://developer.android.com/topic/instant-apps/getting-started/structure.html#basic-app
Android SDK is the core features and software tools that allow you to create an app for the Android Platform. An SDK contains lots of libraries and tools which you will use to develop your application.
A Library is a collection of pre-built compiled code which you can use to extend your application's features. For example, you may need to show some graphics in your application. Instead of creating this from scratch, you may choose to use a pre-built library someone else has developed which will give you the features you need thus saving you some time.
A module is a small part of the application which can be tested and debugged on its own without needing the whole application. This is same for any programming language. Suppose you are building an app with Login feature. To test if the login feature works, you don't need the whole app. Thus the Login part is a module of your application.
The app module builds an app. A library module builds a library.
An app is what a user uses. The output of an app module is an APK, the package of an Android application.
A library is a collection of code that represents something that you want to use in multiple applications or otherwise want to keep in a separate "container" than the rest of the app code. The output of a library module is an AAR And Jar.
Use Feature for linked feature of your instant app (to launch it with deeplink).
Use Library for code dependency in your app or in your Feature modules.

Adding application specific code to an application module that depends on a library module in Android Studio

I have a library module in my Android Studio project. Because I would like to publish a free version and a paid version of my app, I've added two application modules that depends on this library project.
If I would like to toast e.g. "FREE" for the free application module and "PAID" for the paid application module, how do I tell the difference of which application module that is active at run-time?
I've had a look at this question but I believe that it contradicts the reason for implementing the library module in the first place.
Initially I thought that I would add two different MainActivity.java in each of the application modules, each with a different Toast message. This did not work, since the library modules MainActivity.java was still shown.
So my questions are; Am I on the right path here, is this how you utilize library modules in Android Studio? If so, how do I add code that is specific to the application module, thus not implemented in the library module, "overriding the default" library module code?
Rather than going with libraries you may want to investigate application flavours under the gradle documentation:
http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-BuildType-and-Product-Flavor-property-reference

Android Studio Java Library Module vs. Android Library Module

My desired end result is to have a Project that can output different productflavors of Mobile, Tv, and Wear apps, that share the same code-base. So I would like the codebase to be a dependency for all three app-types, where each app module would only contain activities and interaction with the codebase.
My first idea was to add a "Android Library Module" to my newly created project, but I noticed that this is really just a new app module with its own resources and everything. I would like the codebase to function more like a "Java Library", but it needs access to packages like "android.graphics.Color".
So in short, is the correct way of achieving this result to use a java library that has a reference to an android sdk or am i just going about this the wrong way?
Continuation of this question at:Does an Android Library need a manifest ,app_name,Icon?
There's no in-between. If you want access to Android APIs, then the library needs to be an Android library so that the build system can properly link it in to dependent projects. It's true that Android Libraries have resources and other things you may not need, but you can ignore those bits and treat it essentially as a plain Java library if you wish. Even if you're not using resources, you may find useful the ability to specify AndroidManifest.xml attributes to be merged into the dependent app.
The Android Library project doesn't build a fully-fledged APK as its output; it generates an AAR, which is conceptually similar to a JAR archive, but has resources and meta-information useful to Android projects.
Supplemental answer defining terms
The Android Studio documentation defines a module as follows:
A module is a collection of source files and build settings that allow
you to divide your project into discrete units of functionality. Your
project can have one or many modules and one module may use another
module as a dependency. Each module can be independently built,
tested, and debugged.
So basically a module is a subproject in your bigger project.
Types of modules
Android app module - The app module is usually what you are working with in a normal project. When you compile it you get an APK file that will run on a device. Here are the different types of app modules that exist:
Phone & Tablet Module
Android Wear Module
Android TV Module
Glass Module
Library module - The purpose of a library is to share code. For example, you could have a project with a couple different app modules. The common code that they both use could be located in the library.
Android Library - In addition to Java code, this allows you to also include Android resource files and a manifest. If you are making an Android project and are wondering what kind of library to use, then choose the Android Library. When compiled it creates an AAR (Android Archive) file.
Java Library - This only allows you to include Java code files, no Android resource files. This is useful for cross-platform code sharing. When compiled it creates a JAR (Java Archive) file.
Google Cloud module - This type of module is the Google Cloud backend for communication with your client side app.
One additional point that I've not seen well documented: An android library module can have a dependency on another android library module or java library module, but a java library module cannot have a dependency on an android library module.

Categories

Resources