Google Play Services with Maven? - android

I'm some kind of confused. I want to install Google Play Services for Android and found this question: How can i use google play services in a maven project?
There is a reference to https://github.com/mosabua/maven-android-sdk-deployer/ which provides a project to install a lot of mavenized APIs. So I downloaded and built it to get one of the latest Google Play Services API but when I add
<dependency>
<groupId>com.google.android.gms</groupId>
<artifactId>play-services</artifactId>
<version>5.0.89</version>
<type>aar</type>
</dependency>
to my project, I get a "support-v4-19.1.0.jar" as dependency which includes neither any Google Play Services classes nor do I have any of those layout files supplied with Google Services API. For example: When I look to https://developer.android.com/google/play-services/setup.html, and read the ProGuard part, then there must be at least a class called SafeParcelable, but this included project by Maven does not even have the common package where this class is included. I wonder what's going on here.
The project play-services-5.0.89.aar exists in the Maven repository. But how do I use this? Is there any additional task to do since it's aar or something?
[UPDATE]
Ok, it seems that's only an Eclipse problem, because com.google.android.gms.common.GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()) is compilable by Maven although Eclipse doesn't find it. That means that the Google Play Services API works within the project.
[UPDATE 2]
Meanwhile I figured out that aar is not supported by Eclipse. But I use a little trick to use apklib and aar (which is supported by android-maven-plugin) with Eclipse. I unpack the package (in Maven repository) and import it as library into Eclipse. Then I add the library reference into the main project's properties. Eclipse finds the classes of these libraries then, so no erroneous Eclipse project anymore (even content assist works) and you can even build it by Maven when you use the android-maven-plugin. In other words: You only need to import aar and apklib as libraries into Eclipse due to code writing to not have an erroneous project. For the build process all this doesn't matter, since Maven does all your build handling.

Related

Google Play Services aar download for above 11.0.4

From where to download the google-play-services aar files for version 11.6.0 OR above 11.0.4
I know just by adding the dependency in apps build.gradle adds the library in it.
compile 'com.google.android.gms:play-services-location:11.6.0'
compile 'com.google.android.gms:play-services-ads:11.6.0'
This is fair in case of Android Studio.
But as a Unity Developer, I have downloaded the Google Mobile Ads Unity Plugin which after import gives the play services ads, lite, basement, gass of version 11.6.0
Now we have some location dependency in our project. So I have downloaded the Android SDK and its stored in this path.
/Users/USERNAME/Library/Android/sdk
After navigating to /Users/USERNAME/Library/Android/sdk/extras/google/m2repository/com/google/android/gms,
I got the latest play services library of version 11.0.4 only and if I tried to create a Android Studio project by adding GPS dependency of 11.6.0, its getting added properly.
So my question is,
where google play services library of version 11.6.0 is downloaded. So that I can go to that location and get all the aar file needed for my Unity Project
Following are the details from Android developer documentation:
Programmatic access:
For programmatic access to Google's Maven artifacts, you can get an XML list of artifact groups from maven.google.com/master-index.xml (link) which gives the group path
Then, for any group, you can view its library names and versions at:
maven.google.com/group_path/group-index.xml
For example, libraries in the android.arch.lifecycle group are listed at maven.google.com/android/arch/lifecycle/group-index.xml (link).
You can also download the POM and JAR files at:
maven.google.com/group_path/library/version/library-version.ext
For example:
maven.google.com/android/arch/lifecycle/compiler/1.0.0/compiler-1.0.0.pom. (link)
For more details please refer:
https://developer.android.com/studio/build/dependencies.html#gmaven-access
For complete list of Google Play Services libraries, refer below xml:
https://dl.google.com/dl/android/maven2/com/google/android/gms/group-index.xml
Eg: To download Google Play Services Location library AAR file, use the following link:
https://dl.google.com/dl/android/maven2/com/google/android/gms/play-services-location/11.6.0/play-services-location-11.6.0.aar
You can replace group name, version, file, extension to download other files.
Note: Do check the pom file for each module, it may have dependencies on other Google Play Services libraries. If so, you will need to download all dependent files.
[Update]:
You can use Google's Unity Jar Resolver for this:
https://github.com/googlesamples/unity-jar-resolver
Check the links below:
play-services-ads-11.6.0
play-services-location-11.6.0
You can find and download all versions of the aar libraries at the MVN repository.
Also:
I came across a github project for Titanium/Appcelerator/AxWay. It has a script that downloads the latest play-services libraries.
The project is at: https://github.com/appcelerator-modules/ti.playservices
The relevant script is: updater/index.js
Hopefully the script will not go away because it is very useful.

APK Expansion Files: Specifying library path in Android Studio (Windows)

I am on Android Studio (Windows 10). I have been trying to follow the steps in https://developer.android.com/google/play/expansion-files.html but am stuck at the section "Preparing to use the Downloader Library". I have downloaded and installed Google Play License Library and Google Play Downloader Library. I have created the corresponding modules. But when I go to Module Settings and specify the path to the Library Repository, I get errors and when I open up Module Settings again, the Library Repository field remains blank. The error I get are :
10:23:57 PM IncorrectOperationException: Error while applying changes: cannot create file from text: C:\Users\n56\AppData\Local\Android\sdk\extras\google\market_apk_expansion\downloader_library\
10:36:23 PM IncorrectOperationException: Error while applying changes: cannot create file from text: C:\Users\n56\AppData\Local\Android\sdk\extras\google\market_licensing\library\
And the library repository paths I specified are :
C:\Users\n56\AppData\Local\Android\sdk\extras\google\market_apk_expansion\downloader_library
C:\Users\n56\AppData\Local\Android\sdk\extras\google\market_licensing\library\
I have tried various things like escaping the backslashes, importing the modules instead of clicking New Modules but to no available.
Would be grateful for any help. TIA.
The documentation of this tutorial miss the reference of the Android Manifes.xml file on the path for each android-library.
The correct path for each library are:
Google Play License Library
<sdk>/extras/google/play_licensing/library/AndroidManifest.xml
Google Play Downloader Library
<sdk>/extras/google/play_apk_expansion/downloader_library/AndroidManifest.xml
I noticed that on other tutorial from Google where use the same library for setting up the Licensing Verification Library
ANSWER UPDATED:
These previous method only reference one file of the whole library.
Actually I revert all this steps to use a library from gradle whit all we need to use Google Play License Library and Google Play Downloader Library there is the link for the repository
I tried and made it work with below Tags in individual Module's build.gradle . flatDir is the key to solution. and for Windows based Android Studio, the '\' needs to be escaped using double '\' backslashes.
repositories {
flatDir {
dirs "C:\\Users\\<username>\\AppData\\Local\\Android\\sdk\\extras\\google\\market_licensing"
}
}
I am still learning, but I think it has something to do with flatDirs container which should be child of repositories container.
it is now showing up a bit different but the Gradle Sync just works.

Why do Google recommend copying libraries into your tree?

Google's instructions for using the Play Service API (for example) say:
Copy the /extras/google/google_play_services/libproject/google-play-services_lib library project into the source tree where you maintain your Android app projects.
Note: You should be referencing a copy of the library that you copied to your source tree—you should not reference the library from the Android SDK directory.
This seems ugly to me - why not reference it from the SDK directory? Is there some technical reason for this? Or is it so that you have explicit control over when it gets upgraded?
I'd like to point out that this is entirely a limitation of Eclipse, and it is indeed ugly.
The problem is that this library contains resources in addition to source code. Eclipse can only deal with libraries packaged as jar files, which, for the purposes of Android development, cannot contain resources.
So, in order for the library's resource to be compiled into the application, the library's source code, with the resources, must be added to your project.
If you move your build to Maven, and use an IDE that 'understands' Maven, then you can compile a library that contains resources as an 'apklib', and treat it as an external library, in a manner similar to a jar file.
The new Gradle-based build system is built on Maven primitives, but uses a different format for this, 'aar'. Hopefully, it will eventually also support apklib so that Maven builds and Gradle builds can inter-operate.
I just went through the exercise of converting an Android application to a Maven build, including the use of some apklibs. I can tell you that Eclipse with the m2eclipse plugin does not handle apklibs properly. Both IntelliJ and the new Google Android Studio (based on IntelliJ) do handle apklibs with no issues.
It's not about "Play Services Library" specifically. Just like any other libraries that the project makes use of, this library should be referenced from project's source tree.
In this case the external library is in the Android SDK directory and referencing from there is not a good practice too. So yes, it can be called "a technical reason".
Used libraries (Play Services library in this case) shouldn't be referenced from anywhere other than the project's source tree.

Adding Google API Java Client Library and Calendar API Java Library to Maven dependencies

I have an Android project using Google Calendar v3 API.
I'm using Google API Client and Calendar API Java Libraries to perform queries on Google Calendar.
I have problems setting up maven dependencies. I've added those libraries to my pom.xml using ids from documentation, but some of them were outdated, so I've ended up with this pom.xml
I had to put <scope>provided</scope> to google-api-client, google-http-client-jackson and google-http-client-android libraries because maven was failing when trying to dex those libs.
Failed to execute goal com.jayway.maven.plugins.android.generation2:android-maven-plugin:3.4.1:dex (default-dex)
Now this pom.xml successfully creates .apk and installs it on device. But when I trying to make queries to Calendar API using provided libraries, an app fails, saying that it cannot find some classes.
12-19 17:32:17.247: E/dalvikvm(15119): Could not find class 'com.google.api.services.calendar.model.TimePeriod', referenced from method
When I'm building an app from Eclipse (all .jars are in the /libs folder) everything is ok.
So I wonder if there is a correct way to add those dependencies to Maven script.
Google has a lot of an outdated documentation with non-working links or samples.
Otherwise I'd have to get rid of Google libraries and write Calendar API queries by myself.
Any help will be highly appreciated.
ANSWER:
xpp3.jar was causing the problem because it has classes from javax package. I had to manually exclude it from all Google libraries:
<exclusion>
<artifactId>xpp3</artifactId>
<groupId>xpp3</groupId>
</exclusion>
Artem,
I see next error and advice how to fix it:
[INFO] trouble processing "javax/xml/namespace/QName.class":
[INFO]
[INFO] Ill-advised or mistaken usage of a core class (java.* or javax.*)
[INFO] when not building a core library.
This is Java core file. Try to figure out what dependencies are using it with (mvn dependency:tree).
You can not use scope provided since these libraries are NOT part of the Android stack and therefore need to be included into your apk. What are the failures you are getting when including them with default scope?
Update:
As suspected you are trying to include a javax namespace project. Try to find out what the dependency is that is causing it with
mvn dependency:tree
and then potentially either exclude it (assuming that same class is in android already) or shade it into a different namespace (and update the library to use it) or find a better suited library..

Adding an Eclipse "Android Library" project and building via Maven

I am trying to include cocos2d into a preexisting app. I've done things the Eclipse way, such as setting "isLibrary" and adding the library project to the build path in Eclipse, and I have the following dependency in my app's pom.xml:
<dependency>
<groupId>cocos2d_android</groupId>
<artifactId>cocos2d_android</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
I thought this would've taken care of the issue, but when I build, the library seems not to be included. I know this because when I start an Activity, SimpleGame, referencing one of the classes in the cocos2d source, the Activity dies and I get this stacktrace in DDMS:
E/AndroidRuntime(10621): FATAL EXCEPTION: main
E/AndroidRuntime(10621): java.lang.NoClassDefFoundError: org.cocos2d.opengl.CCGLSurfaceView
E/AndroidRuntime(10621): at com.xyz.game.SimpleGame.onCreate(SimpleGame.java:22)
...
I'm looking for two things:
1) a reliable way to see if a certain class/jar/whatever was packaged up into my apk, as the steps to get to this point in my app are long and complicated right now
2) Something in the Manifest or pom.xml for either the main app or the library project seems to be missing - something needed to signal to Maven to pick up this other project - what is it?
I'm using Maven 3.0.4 and 3.0.0-alpha-13 of the plugin, building for level 8 and up.
1) a reliable way to see if a certain class/jar/whatever was packaged up into my apk, as the steps to get to this point in my app are long and complicated right now
Build project from command-line (i.e. mvn clean install), maven will output verbose logs during every single phase/goal of build, in the dex goal, you can see something like this:
[INFO] --- android-maven-plugin:3.1.1:dex (default-dex) # myproject ---
[INFO] C:\Program Files\Java\jdk1.6.0_21\jre\bin\java [-Xmx1024M, -jar, C:\Progr
am Files\Android\android-sdk-r16\platform-tools\lib\dx.jar, --dex, --output=C:\workspace\myproject\target\classes.dex, C:\workspace\myproject\target\classes, C:\maven\repository\cocos2d_android\cocos2d_android\1.0.0-SNAPSHOT\cocos2d_android-1.0.0-SNAPSHOT.apklib, C:\maven\repository\com\google\code\gson\gson\1.7.1\gson-1.7.1.jar, ... ...]
The Android Library Project is actually dex-ed as cocos2d_android-1.0.0-SNAPSHOT.apklib with some other regular jar library archives.
2) Something in the Manifest or pom.xml for either the main app or the library project seems to be missing - something needed to signal to Maven to pick up this other project - what is it?
It is ApkLib, which is simply a zip archive of the Android Library Project (src/, res/, AndroidManifest.xml and etc.). We usually create/implement our own Android Library Project with the dependant Android Project as multi-module maven project, however, this is not necessary in case if you need reference Android Library Project written by other developers. Thanks to the developer, android-maven-plugin has already supported non-Maven Android Library Projects, check out Compatible with non-Maven Android Library Projects:
The generated .apklib file will have the layout of a standard Android/Eclipse library project. This means that regardless of your Maven layout, the layout inside the .apklib file will be that source code is in "src/" instead of "src/main/java/", but still interpreted correctly when used in an Android/Maven application project. This is to be compatible with non-Maven developers' library projects, which is important to grow the Android developer community.
Use other non-Maven developers' libraries
It also means we can take any external Android library project zip file (from non-Maven developers) and do mvn install:install-file ... on it and simply start using it as a dependency.
Share your .apklib with non-Maven developers
To share your .apklib file with a non-Maven developer, they will probably feel more comfortable if you rename it to .zip. They can then simply unpack it in a directory and use it from there.
So the workaround is:
Pack your Android Library Project into zip archive properly, then rename it to something.apklib.
Install something.apklib into your Maven local repository by using mvn install:install-file.
In any Android Project that requires Library dependency, simply using:
<dependency>
<groupId>cocos2d_android</groupId>
<artifactId>cocos2d_android</artifactId>
<version>1.0.0-SNAPSHOT</version>
<type>apklib</type>
</dependency>
Check out Samples on android-maven-plugin website to see how to use apklib properly.
Scope shouldn't be provided. Provided means:
provided - this is much like compile, but indicates you expect the JDK
or a container to provide it at runtime. It is only available on the
compilation and test classpath, and is not transitive.
Use compile instead.
Extra tip: Since compile is the default, you can remove the <scope>provided</scope> line.

Categories

Resources