Android Tutorial Google Play Services - Installation & Tips

Status
Not open for further replies.

Erel

Administrator
Staff member
Licensed User
This tutorial is not relevant for developers using B4A v6+.

More and more services rely on Google Play Services library.
For example the following libraries depend on it: AdMob, GoogleMap, Google Play Game Services, Google Analytics and others.

Installing Google Play Services

- Open Android SDK Manager and download the latest version:



- Copy google-play-services.jar from <android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\libs to the additional libraries folder. Make sure to repeat this step whenever you update Google Play Services.

- Some libraries (such as Google Maps) depend on resources that are part of Google Play Services.
The library tutorial should explicitly mention this requirement.
In this case you need to add the following attribute to the main module:
B4X:
#AdditionalRes: <android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms
Make sure to update <android sdk> with the correct path.

- google-play-services.jar is a large library. It will add about 3mb to the apk size. It will also slow down the compilation process.
You can make the optimized dexer step much quicker by increasing the memory that the dexer can use.
In order to do this you need to close the IDE, go to %APPDATA%\Anywhere Software\Basic4android in Windows explorer.
Edit b4xV5.ini with a text editor and increase the value of MaxRamForDex from 512 to 1024 or more.

- #ExcludeClasses - This attribute, which was added in B4A v5.80, allows you to removed classes that are not required from your app. It will make compilation faster and the APK size will be smaller.
https://www.b4x.com/android/forum/threads/new-feature-three-birds-with-one-stone.63127/

Note that the latest version of Google Play Services requires Java 7 (set with Tools - Configure Paths) and requires B4A v3.82+.
 
Last edited:

pierrem

Member
Licensed User
Hi,

I followed this tuto (and seen additional comments on here) but ...

still unable ton find the google-play-services.xml file
I've been looking everywhere in ...\android-sdk\extras\google\google_play_services
and this lib doesn't appear in the lib panel
(a copy of the .jar is well in my B4A other-lib directory)


Where am I wrong ?
Should I specifically download/update this.XML file through the sdk manager ?
I dont an option for this in the sdk manager

(btw, i plan to use at least Auth ... updated tuto welcome !
:)
)
 

Mbm

Member
Licensed User
Hello,

I have an app that uses GoogleMapsExtras Library an it was working fine for months in all kind of devices but this week, something started to fail. In devices with version of google play services 6.7.76(I think that was released 23th of february) I get an error with the code:

B4X:
Dim MarkerOptions1 As MarkerOptions
MarkerOptions1.Initialize
MarkerOptions1.Position2(cont.Latitude,cont.Longitude).Snippet(line1).Title(str2).Visible(True)
The error is:

B4X:
** Activity (lastpositionmap) Resume **


map ready


java.lang.NullPointerException


    at com.google.k.a.cj.a(Unknown Source)
    at com.google.maps.api.android.lib6.c.dc.<init>(Unknown Source)
    at com.google.maps.api.android.lib6.c.db.a(Unknown Source)
    at com.google.maps.api.android.lib6.c.aj.<init>(Unknown Source)
    at com.google.maps.api.android.lib6.c.al.a(Unknown Source)
    at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
    at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
    at android.os.Binder.transact(Binder.java:361)
    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
    at uk.co.martinpearman.b4a.googlemapsextras.GoogleMapsExtras.AddMarker(GoogleMapsExtras.java:48)
    at com.mbm.distromel.incidencias.lastpositionmap._fillmap(lastpositionmap.java:543)
    at com.mbm.distromel.incidencias.lastpositionmap._map_ready(lastpositionmap.java:604)
    at java.lang.reflect.Method.invokeNative(Native Method)


    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment.onViewCreated(MapFragmentWrapper.java:191)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5335)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
I have been reading and I have realized that maybe I should update my google play services so I have tried to do it but I get this error:

B4X:
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4452000 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
I have copied google-play-services.jar from <android sdk>\extras\google\google_play_services\libproject\google-play-services_lib\libs to the additional libraries folder and i have added in my main module:

B4X:
#AdditionalRes: C:\Archivos de programa\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res,com.google.android.gms
but it doen´t work and I´m stuck with this problem.

I´ll be glad if someone could help me.

Thank you in advance.
 
Last edited:

Mbm

Member
Licensed User
i have added in my main module:

B4X:
#AdditionalRes: C:\Archivos de programa\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib\res,com.google.android.gms
I have included in my application editor (with my Api code) and all works fine for previous versions of google play services:


B4X:
AddManifestText( <permission
          android:name="$PACKAGE$.permission.MAPS_RECEIVE"
          android:protectionLevel="signature"/>
      <uses-feature android:glEsVersion="0x00020000" android:required="true"/>)
 
AddApplicationText(<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzaSyCzspmxxxxxxxxxxxxx"/>)
I don´t have clear if i should quit this code for my version (B4A 4.30), but if i quit the code how I pass the api key to the Google Maps Api?
 

Mbm

Member
Licensed User
I have replaced the application editor code for this:

B4X:
AddManifestText( <permission
          android:name="$PACKAGE$.permission.MAPS_RECEIVE"
          android:protectionLevel="signature"/>
      <uses-feature android:glEsVersion="0x00020000" android:required="true"/>)
 
AddApplicationText(<meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="AIzxxxxxxxxxxxx"/>
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"
    />)
Now I don´t get this error:

B4X:
Code:
java.lang.IllegalStateException: The meta-data tag in your app's AndroidManifest.xml does not have the right value.  Expected 4452000 but found 0.  You must have the following declaration within the <application> element:     <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />
But still get the error:

B4X:
** Activity (lastpositionmap) Create, isFirst = true **


** Activity (lastpositionmap) Resume **


map ready


java.lang.NullPointerException


    at com.google.k.a.cj.a(Unknown Source)
    at com.google.maps.api.android.lib6.c.dc.<init>(Unknown Source)
    at com.google.maps.api.android.lib6.c.db.a(Unknown Source)
    at com.google.maps.api.android.lib6.c.aj.<init>(Unknown Source)
    at com.google.maps.api.android.lib6.c.al.a(Unknown Source)
    at com.google.maps.api.android.lib6.c.el.a(Unknown Source)
    at com.google.android.gms.maps.internal.l.onTransact(SourceFile:167)
    at android.os.Binder.transact(Binder.java:361)
    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
    at uk.co.martinpearman.b4a.googlemapsextras.GoogleMapsExtras.AddMarker(GoogleMapsExtras.java:48)
    at com.mbm.distromel.incidencias.lastpositionmap._fillmap(lastpositionmap.java:543)
    at com.mbm.distromel.incidencias.lastpositionmap._map_ready(lastpositionmap.java:604)
    at java.lang.reflect.Method.invokeNative(Native Method)


    at java.lang.reflect.Method.invoke(Method.java:515)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:175)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:171)
    at anywheresoftware.b4a.objects.MapFragmentWrapper$MyMapFragment.onViewCreated(MapFragmentWrapper.java:191)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
    at android.app.BackStackRecord.run(BackStackRecord.java:684)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:157)
    at android.app.ActivityThread.main(ActivityThread.java:5335)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
    at dalvik.system.NativeStart.main(Native Method)
java.lang.NullPointerException
** Service (newinst2) Start **


-- AppUpdating.NewInst2: processing service_start
Maybe is a problem with the googlemapsextras library? Have I updated well my google play services? I have installed version 22
 

eurojam

Well-Known Member
Licensed User
you can directly add the version code like
B4X:
<meta-data android:name="com.google.android.gms.version" android:value="4452000" />
version code could be found in google-play-services_lib>res>values>version.xml
 

Mbm

Member
Licensed User
But with this expression works too, doesn´t it?

B4X:
android:value="@integer/google_play_services_version"
 

Erel

Administrator
Staff member
Licensed User
you can directly add the version code like
Don't do it. Will only cause problems.

You need to make sure to copy the latest google-play-services.jar to the additional libraries folder and reference the latest res folder with #AdditionalRes.

The second error is not related to this thread.
 

Mbm

Member
Licensed User
Ok thank you Erel,now I think that I have updated the google play services in the correct way, I have posted the other issue in the googlemapsextras library thread, if you have any idea about the problem i will be waiting for your tip
 

henrywood

Active Member
Licensed User
I cannot compile my app when InAppBilling is added as a Library
I have changed the INI file as suggested

Here is the error I am getting:


B4A version 4.30
Parsing code. 0.41
Compiling code. 1.98
Compiling layouts code. 0.79
Generating R file. 0.42
Compiling debugger engine code. 7.47
Compiling generated Java code. 8.11
Convert byte code - optimized dex. Error
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lcom/google/android/gms/common/internal/safeparcel/SafeParcelable;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:122)
at com.android.dx.dex.file.DexFile.add(DexFile.java:161)
at com.android.dx.command.dexer.Main.processClass(Main.java:685)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:634)
at com.android.dx.command.dexer.Main.access$600(Main.java:78)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:572)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
at com.android.dx.command.dexer.Main.processOne(Main.java:596)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:498)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:264)
at com.android.dx.command.dexer.Main.run(Main.java:230)
at com.android.dx.command.dexer.Main.main(Main.java:199)
at com.android.dx.command.Main.main(Main.java:103)
1 error; aborting
Optimized dexer failed. Switching to Standard dexer.



And these are my #AdditionalRes and #AdditionalJar directives for the app:

' Google Play Services / GoogleMaps / InApp Billing
#AdditionalRes: C:\adt-bundle-windows-x86_64-20140702\sdk\extras\google\google_play_services\libproject\google-play-services_lib\res, com.google.android.gms

'FACEBOOK related directives
#AdditionalRes: ..\AppConfig
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\facebook\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\facebook\sdk\facebooksdk.jar

'GOOGLE+ related directives
'#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\googleplus\sdk\res
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\googleplus\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\googleplus\sdk\gplussdk.jar

'Twitter related directives
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\twitter\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\twitter\sdk\twittersdk.jar

'Instagram related directives
#AdditionalRes: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\instagram\sdk\res
#AdditionalJar: C:\Users\Henrik\Desktop\STEEN PROJECT BUTIKKER\APP_ANDROID\beta1\socialapi\instagram\sdk\instagramsdk.jar



Any suggestions ?
 
Last edited:

mr23

Active Member
Licensed User
Tonight I updated the android SDK via the manager, and just accepted the available updates.
I copied the new jar in, and updated the jar path in the B4A IDE.
The plan was to try an interstitial ad. I wasn't expecting it to take a lot longer to compile with the full services, it was much quicker with just a banner ad on the earlier version.
The compile/F5 output includes:
Convert byte code - optimized dex. (136.64s)
Optimized dexer failed. Switching to Standard dexer.
I tried bumping MaxRamForDex from 512 to 2048 (in increments of 512); the above value was the approx the same to this point.
I bumped it to 3072 and it dropped to
Convert byte code - optimized dex. (25.23s)
A second try at the compile/F5, without any other changes resulted in:
Convert byte code - optimized dex. ( 9.25s)
Quite a drop.
Next F5 cycle:
Convert byte code - optimized dex. ( 8.26s)

Is there a known optimal MaxRamForDex for this scenario (so I don't have to keep testing to find one)?
I sometimes have 2-3 instances of B4A running (to update/extend a library, while working on an app, with separate projects for them).
Non-GooglePlayServices apps still build as fast as before. I'm using an i3 laptop with 6GB, which has been acceptable for B4A; it's only using about 25% of the cpu with a few bursts to 50%.
 
Last edited:

mr23

Active Member
Licensed User
More memory will not make it slower.

The optimized dexer works in the background. If you haven't waited long enough when you tested it then the results will not be meaningful.
To clarify, I increased memory until it was able to complete. With <=2048 the opt dexer would fail.
It was then able to run to completion. When recompiled the time dropped (something cached).
If the IDE was exited and restarted, it went back up to a large time value, but completed, then on retests would again drop.
 
Status
Not open for further replies.
Top