Android Wear: how to share code between the wearable and handheld modules? - android

I am making an Android app with Wear capabilities.
I want to share some code between the wearable and handheld modules. Specifically, I want to share communication code that uses Google Play Services classes, e.g. com.google.android.gms.common.api.GoogleApiClient.
The obvious way to do this is to have a module (I called it common) and add a dependency to it in both the handheld and the wearable modules.
Since this common module uses Play Services, I need to make it depend on com.google.android.gms:play-services.
I was not sure what to put for the version number - the official documentation here says to use 5.0.77, but that does not work, as the latest SDK does not have this version anywhere, instead it comes with 5.0.89 and 5.2.08.
If I use 5.0.89, the Wearable app does not work, with this error: Google Play services out of date. Requires 5089000 but found 5077534. The version on the watch is older than the one I used to compile.
Instead of depending on com.google.android.gms:play-services the common module could depend on com.google.android.gms:play-services-wearable but then there is a conflict when building because the handheld module depends on com.google.android.gms:play-services, and these two artefacts use the same package name (com.google.android.gms), and so the gradle build fails.
What's the solution?
.
EDIT after discussing a bit and to make my question clearer.
To be able to use communication APIs in my common module I have two choices:
Make common depend on com.google.android.gms:play-services
Make common depend on com.google.android.gms:play-services-wear
⇒ Solution 1 does not work because the version available (5.0.89) for development is more recent than the one on the watch (5.0.77).
⇒ Solution 2 does not work because the handheld module already depends on com.google.android.gms:play-services, which conflicts with com.google.android.gms:play-services-wear.

I bumped into the same problem a few days ago. My shared module depended on com.google.android.gms:play-services as well, so Gradle refused to build and kept nagging at me:
Error: more than one library with package name 'com.google.android.gms
I added this line to my mobile project's gradle file and the error disappeared magically:
compile(project(':sharedModule')) {
transitive = false
}

Take a look here: https://github.com/tajchert/SWear_Weather
I had created common project that is shared between mobile and wear, and contains my constants. Remember to set there dummy manifest file and:
apply plugin: 'com.android.library' in build.gradle file.
I had also encountered problem with play-services version - I had solved it by using
compile 'com.google.android.gms:play-services-wearable:+'
compile 'com.google.android.support:wearable:+'
instead of specifying particular version - to be honest it should be separete question - as it is out of scope of previous (sharing code between projects).
It is possible to need invalidate cache/restart after changing - you can/should remove build paths in your projects to get rid of all other versions.

Related

Dependency missing but exists in APK, how?

I have a MyLocationService library, which has dependency from huawei_location_service.Inside I have HMSLocationService class which is the only one using huawei_location_service classes and I use relfection to access that class. Meaning is, if we run app on Huawei and if there is dependency from huawei_location_service, I will get location, otherwise will not. And application should run perfectly on non-hauwei devices without dependency from huawei_location_service.
So when I build MyLocationService.aar I removed huawei_location_service dependency from it's pom file. After that I created a new application and added dependency from MyLocationService.aar. When I check dependencies with command gradlew app:dependencies I don't see any dependency from huawei, but when I create an apk and analyze it, in classes.dex there are classes from huawei_location_service.
Question: How it is possible? And is there any other way to achieve what I want?
P.S. I analyzed also MyLocationService.aar, didn't find any huawei dependency. Is there another way to check dependencies of *.aar files instead of pom or analyzing tool of android studio?
So if someone will be mistaken as me, this answer will help.
The repositories and classes I saw in classes.dex were not coming from hms libraries. As I have imports in my custom classes, that imports' texts were the reason I was seeing huawei folder in classes.dex. Also take attention on the size, and you can see that they are kind of 20 bytes.
So I removed the imports, generate my library again, created apk and analyzed it and woala, no huawei folder is visible.
P.S. *.aars doesn't contain any library if you not put transitive=true. And you need to add dependencies required by your lib in your own applicaiton.
P.S.S. If you have locally or globally publishing your library, maven(Gradle uses maven) creates metadata, so called POM file, as a helper to identify all dependencies that the library needs.

how to create library ( jar ) in android studio ? [duplicate]

This question already has answers here:
How to make a .jar out from an Android Studio project
(11 answers)
Closed 5 years ago.
I want to create some library that i will use in the future beside my current project.
I can't find a way to create library in android studio.
How to do it on android ?
A library module is useful in the following situations:
When you're building multiple apps that use some of the same components, such as activities, services, or UI layouts.
When you're building an app that exists in multiple APK variations, such as a free and paid version and you need the same core components in both.
In either case, simply move the files you want to reuse into a library module then add the library as a dependency for each app module.
To create a new library module in your project, proceed as follows:
Click File > New > New Module.
In the Create New Module window that appears, click Android Library, then click Next.
There's also an option to create a Java Library, which builds a traditional JAR file.
Give your library a name and select a minimum SDK version for the code in the library, then click Finish.
Once the Gradle project sync completes, the library module appears in the Project panel on the left.
If you don't see the new module folder, make sure it's displaying the Android view.
Visit https://developer.android.com/studio/projects/android-library.html
As introduction I would suggest you to peek into this conceptually simple tutorial. Basically you can start your own library module when you chose your project, without adding any Activity. Then you create your Java Class, usually with a View. When your library is ready, with all its business logic, you can glue everything inserting in the top level build gradle the instruction you are using a library, basically before you remove the following line, that is not needed for a library:
applicationId
(This line in your gradle file is a unique application ID that looks like a Java package name, that identifies your app to the device you are running and in google play)
and then you change this line:
apply plugin: 'com.android.application'
to:
apply plugin: 'com.android.name_library'
When you have a more structured project you can follow the official documentation
As it is well explained the difference between a normal Application and a Library is:
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.
I would not encourage you to use solutions like web services that do it on your behalf, namely just copying/pasting your existing code. In fact a library often needs specific architectural choices, so is important to consider and learn different factors, is not just writing some business logic is quite complex to explain, but you can imagine that also the choice of what the user can see and modify can be crucial. Also should be as much as possible bug free, because once it is adopted could cause problems to the users. I remand you to a famous post, superbly written where you can find some solution to this aspect.
you can use https://jitpack.io/ is very easy publish an android library. just upload your code to github/bitbucket and paste the repository link on jitpack website. that's all

How to configure Gradle dependencies when converting an app to a library for use in another app

For a number of inane reasons, I have to split an existing app into phone and tablet versions with multiple APKs (each one has nearly 100Mb of different static resources--yes, I'm mad at the guy who was in charge of requirements).
To achieve this, I am converting the original app into an Android library using com.android.library in the original package (and removing applicationId from build.gradle).
To import the library into my new app (myProjectTablet), I added the following to settings.gradle:
include ':myProjectLib'
project (':myProjectLib').projectDir = new File('../myProjectRoot/myProjectLib')
To compile it, I added the following to build.gradle:
compile project (':myProjectLib')
For now, I need two apps based on this library so the directory structure is:
myProjectRoot
myProjectLib
myProjectPhone
myProjectTablet
And to finally ask my question: I receive some variant of the following Gradle error no matter how I tweak paths and such:
Error:Project :tablet declares a dependency from configuration 'compile' to configuration 'default' which is not declared in the descriptor for project :lib.
From my searching, it seems this usually has to do with a conflict in using the default keyword in the context of certain libraries and tools, but I don't believe my apps have anything like that. If anybody has any insight into this error, I would appreciate it.
And after two days of messing this up (and accidentally trying to put this in a comment), I finally realized that:
project (':myProjectLib').projectDir = new File('../myProjectRoot/myProjectLib')
needs to be:
`project (':myProjectLib').projectDir = new File('../myProjectLib')`
Gradle simply wasn't finding the project it seems.

Create an Android Library that uses google play services

I need to create an Android Library that uses google's gcm and location services. Initially I did it inside the application's module and everything went fine.
Now I need to create an Android Library to be used by more than one app, and I'm having a lot of problems:
The first one is that my lib doesn't have a google-services.json file, since it should use the app's module to configure itself. So google play services plugin is not working on the libs' build.gradle file.
The second one is that it seems that google play services plugin detects that I'm not calling apply plugin inside the lib's build.gradle and uses by default the version 9.0.0 of the library, even when I'm configuring it to use 10.0.1. So I'm also having a strange problem to run my app, Unable to execute dex: Multiple dex files define ...
What's the right way to create an Android library that uses google-play-services which can also coexist with an Android Application that also uses google-play-services?
First of all in your library, in the build.gradle you have to remove the line
//apply plugin: 'com.google.gms.google-services'
Since you can't have a google-services.json file, you can't use the plugin.
You have to add the dependencies needed, and it is enough to compile to library.
Instead in the projects which will use the library, you have to add the plugin and the google-services.json file.
I have faced a similar situation and concluded that:
1- Android Library should have it
google-services.json
file and should also declare dependencies and google services plugin.
2- App using the library can have their own
google-services.json
file and dependencies declared.
The point to be noted here is that we should use the same versions of dependencies and google services plugin.
Also while adding library dependency,
transitive = true
is added.
I found in the internet that this error of yours is usually caused by multiple copies of library(duplicate) that you are using in your project. Go into the Project Structure -> modules, then check in the target or the apk-libs folder if there are more than one copy. After this, do a clean and build the project.
For more solution about this issue, check this related SO question.
Now, for your question about creating an Android library that uses google-play-services, try to check this tutorial if it can help you. It provides screenshot that you can follow.
For more information, check these threads:
Set Up Google Play Services
Android Studio with Google Play Services
Import Google Play Services library in Android Studio
Adding Google Play Services Library to Your Android App
Even after invalidating caches and restarting Android Studio. The only thing that solved my problem was restarting the computer. Don't know how, but suddenly everything was correct again.

Can we have multiple apps in one Android Studio project?

I am using Android Studio for developing Android apps. But I have heard in Android Studio it is better to have only one app in a single (one project per app) if that is right, then it will be very wasteful to open many frames for many projects. But when I searched I found that
Android Studio project = Eclipse workspace
Android Studio module = Eclipse project
Now, if this is true, it means that Android Studio also can support a multi-app project. If yes, then, is every app in Android Studio independent like in Eclipse (i.e. they do not disturb each other by sharing any file or setting)? Or can we have many apps in a single project? If we can then is there any point to take care of?
Thanks!
Yes, you have two options:
Option 1: Create an addition app module
First create your standard Phone & Tablet Android project, including the auto-generated app module.
Add a new app module: File > New > New Module ... > Phone & Tablet Module
Complete the wizard and name your Application app2 for instance.
Now you'll have both app and app2 in the same project.
To actually run app2 you first need to select it in the pull-down menu in the top toolbar of Android Studio, next to the Start and Debug icons. You can also do this though Run Configurations: Run > Run... > Edit Configurations... and modifying Module.
Option 2: Create an addition library module
This is ideal for creating a separate library that is isolated from the app, and can be shared across more apps (or other projects):
Add a new library module: File > New > New Module ... > Java Library.
Complete the wizard and give your library a good name, like libgoodstuff.
Now libgoodstuff and app will reside in the same project.
To make app sources depend on libgoodstuff, you first have to add the library module to the project settings.gradle to look something like this:
include ':app', ':libgoodstuff'
Then in app/build.gradle you have to depend on the library module like this:
apply plugin: 'com.android.application'
···
dependencies {
···
implementation project(path: ':libgoodstuff')
···
}
···
Yes you can. Inside your project if you want to create a new app do the following:
Create a new module by right clicking on your project -> new -> module
Select phone and tablet module
Now you will be able to run either app. This is an excellent way to share code between two apps as it allows you to keep and develop your libraries in one location.
You can definitely have multiple app modules in the same Android Studio project. Having said that, I've yet to find a reason to define multiple app modules in a project.
If you need different version of the same app, Gradle's build variant is powerful enough to satisfy perhaps 99% of the use-cases (I have a project with a dozen variants, each with its own custom code/res).
If you are writing different apps then it's better to make each its own project so apps don't inadvertently change each other's behaviour.
Not sure what you mean by "is every app in Android Studio independent as Eclipse", but each module is its own world by default unless dependencies to other modules are explicitly defined.
Adding this as an answer since I don't have enough reputation for comments yet.
For the answer to your question - Check this question that I have raised. Is this the same boat you were in ?
TL;DR
I was able to have multiple apps in the same Android Studio Project, build and run them without any issues. Another member
corroborated my claims in the comments on the Question.
#Android Studio Pros : Please check the above link and add your insights. This seems to be a confusing aspect.
My Take
I think I agree with #Kai's answer. But there are instances where we want multiple apps to have common library dependencies and don't want to duplicate the library dependencies. Wouldn't multiple apps be fine as long as the common library dependencies have ONLY common code and nothing else. The separate modules hold the individual app related code and that's where the differentiation is.
Yes, it is possible. As the existing answers showed, it’s quite straightforward to create additional application module in the same Android Studio project.
So I’ll try to answer underlying question why anyone might need it.
It’s certainly not worth it to put multiple completely independent apps in one project.
However, if you app is big enough, you might benefit from putting separate features into separate modules. You can also create a separate executable app module for each feature, so that you can:
launch/debug each feature separately
save some time on every compilation/dexing/putting everything into a single apk
encourage teams/developers to work independently, and even in separate repositories.
The main app module can be used only to combine existing features together.
I’ve recently created an article demonstrating this approach where I tried to explain everything in more details: https://medium.com/#domplebump/multiple-application-modules-in-one-android-project-36e86ceb8a9

Categories

Resources