How can I install Android Support Library to deploy a Gluon Mobile application to Android?
It has been asked here and here, but since the answers are in different contexts, maybe it's worthy having a summary here.
Up until version 1.2.0, the jfxmobile plugin has been working with the obsolete Android Support Library.
Now this library is not only obsolete but not available any more for download.
The current version of the jfxmobile plugin is 1.3.2, and it includes the required changes to work without that library, and use the multidex-1.0.1.aar
library instead.
To make it work, just follow these steps:
Open the Android SDK Manager and make sure you have installed
Extras/Google Repository
andExtras/Android Support Repository
.Make sure you have this library:
ANDROID_HOME/extras/android/m2repository/com/android/support/multidex/1.0.1/multidex-1.0.1.aar
.
Now on your project:
Update the
build.gradle
file of your project to use the jfxmobile plugin current 1.3.2 versionIf you add to the android configuration the android SDK path, you should move it to a properties file. For that, just create a properties file under
C:\Users\<user>\.gradle\gradle.properties
, and add theANDROID_HOME
variable:ANDROID_HOME=C:/<path.to.Android>/sdk
. And remove theandroidSdk
from the android block if it was added.Save and reload your project so the new configuration takes place (On the projects tab, root of the project, right click, Reload Project).
EDIT
If you are on Windows, make sure that Android SDK is installed under a location where gradle will have enough permissions to create folders and extract the .aar file.
Can we build Gluon Mobile for Android (APK) from Windows without installing Linux?
For now, it's not possible to build an Android package (APK) from Windows. But we can use WSL (Windows Subsystem for Linux) and run all the commands from the Linux command. You can get WSL from
Microsoft Store and it's really easy to install. So we can code and run on Windows to see the output of the app and use Linux ONLY when we want to deploy on Android.
javafxports 'android' gradle task requires Android Support Library which is retired from Android SDK Manager...what now?
So far (current release version 1.2.0), the jfxmobile plugin has been working with the obsolete Android Support Library.
As now it is not only obsolete but not available any more for download, as Joeri Sykora commented, there is a snapshot available with a fix to use the new Support Repository.
Until release version 1.3.0 is out, you can use it providing you refer the repository to download this snapshot, so there is no need to download, build or install manually the plugin:
buildscript {
repositories {
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.3.0-SNAPSHOT'
}
}
This will work if you have installed with the Android SDK Manager Extras/Google Repository: check your Android sdk path for this folder: ANDROID_HOME/extras/google/m2repository
, and the Extras/Android Support Repository: check for ANDROID_HOME/extras/android/m2repository/com/android/support/multidex/1.0.1/multidex-1.0.1.aar
And don't forget to apply the changes in the build script to your gradle project (Sync button on top-left of Gradle window in IntelliJ).
Can't build a gluon project for mobile using gradle
After updating Eclipse Mars I can reproduce your issue, and the solution to get all the tasks back is easy:
Based on this thread, it seems there are public and private tasks.
So only the public tasks (those added to any kind of group) are visible by default:
Click on the down arrow icon on the right of the Tasks View:
Select Show All Tasks
:
And you will have a new group: other
, with the missing tasks like android
, launchIOSDevice
:
Gluon mobile cross-platform adsView
Yes, there is a way, for both Android and iOS.
This answer already contains a snippet of how it could be done on Android. But I'll update it here using and extending the Charm Down library. I won't include the iOS solution, though, as it requires modifications in the jfxmobile plugin, as I'll explain later.
Create a new project with the Gluon plugin, and modify the following. You'll have to keep the package names I mention, but you can name your project (and package) as you want.
build.gradle
script
You need to include the Google Play Services library:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.3.4'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
maven {
url 'http://nexus.gluonhq.com/nexus/content/repositories/releases'
}
}
mainClassName = 'com.gluonhq.adview.GluonAdView' // this can be changed
dependencies {
compile 'com.gluonhq:charm:4.3.2'
androidCompile 'com.google.android.gms:play-services-ads:9.4.0'
}
jfxmobile {
downConfig {
version '3.2.4'
plugins 'display', 'lifecycle', 'statusbar', 'storage'
}
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
forceLinkClasses = [
'com.gluonhq.**.*',
'javax.annotations.**.*',
'javax.inject.**.*',
'javax.json.**.*',
'org.glassfish.json.**.*'
]
}
}
project.afterEvaluate {
explodeAarDependencies(project.configurations.androidCompile)
}
Note the last task: it explodes the aar
files on the android/google local repositories, to extract the jars (in this case the Google Play Services jar and all its dependencies).
Note also that after modifying the build file, you need to reload the project (to sync the project and manage the new dependencies).
- Source Packages/Java
Besides the project files, you need to add this package: com.gluonhq.charm.down.plugins
, and these classes:
AdViewService
interface
public interface AdViewService {
void setAdUnit(String unitId, String testDeviceId, boolean test);
}
AdViewServiceFactory
class
public class AdViewServiceFactory extends DefaultServiceFactory<AdViewService> {
public AdViewServiceFactory() {
super(AdViewService.class);
}
}
- Android/Java Packages
In the Android/Java package, add this package: com.gluonhq.charm.down.plugins.android
, and this class:
AndroidAdViewService
class
import android.view.Gravity;
import android.widget.LinearLayout;
import com.gluonhq.charm.down.Services;
import com.gluonhq.charm.down.plugins.LifecycleEvent;
import com.gluonhq.charm.down.plugins.LifecycleService;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import javafxports.android.FXActivity;
import com.gluonhq.charm.down.plugins.AdViewService;
public class AndroidAdViewService implements AdViewService {
private AdView adView;
@Override
public void setAdUnit(String unitId, String testDeviceId, boolean test) {
FXActivity.getInstance().runOnUiThread(() -> {
LinearLayout layout = new LinearLayout(FXActivity.getInstance());
layout.setVerticalGravity(Gravity.BOTTOM);
layout.setOrientation(LinearLayout.VERTICAL);
adView = new AdView(FXActivity.getInstance());
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(unitId);
AdRequest adRequest;
if (test) {
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR) // All emulators
.addTestDevice(testDeviceId) // from logcat!
.build();
} else {
adRequest = new AdRequest.Builder().build();
}
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
}
});
layout.addView(adView);
FXActivity.getViewGroup().addView(layout);
});
Services.get(LifecycleService.class).ifPresent(service -> {
service.addListener(LifecycleEvent.RESUME, () -> FXActivity.getInstance().runOnUiThread(() -> adView.resume()));
service.addListener(LifecycleEvent.PAUSE, () -> FXActivity.getInstance().runOnUiThread(() -> adView.pause()));
});
}
}
It makes use of com.google.android.gms.ads.AdView
. If your Android dependencies already include Google Play Services, you won't have any problem with the imports. Otherwise, go back to step 1.
Note that I'm setting a layout where the banner will go to the bottom of the screen. Change this at your convenience.
AndroidManifest.xml
You need to add this to the manifest:
<?xml version="1.0" encoding="UTF-8"?>
<manifest ...>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application ...>
<activity .../>
<meta-data android:name="com.google.android.gms.version" android:value="9452000" />
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent"/>
</application>
</manifest>
Note the Google Play Services value corresponds with the 9.4.0 version.
- Sample
Now you can add the service to your project. For that you'll need an Ad unit Id. You can create an account on Google AdMob to add your app, and get the App ID for the banner unit.
To get the test device id, you'll have to run the app first, and find in the console the id with adb logcat
.
public BasicView(String name) {
super(name);
Services.get(AdViewService.class).ifPresent(ads -> {
ads.setAdUnit("ca-app-pub-17652XXXXXXXXXX/83XXXXXXXX", "0283A9A0758XXXXXXXXXXXXXXXX", true);
});
Label label = new Label("Hello JavaFX World!");
...
}
- Run the sample on your Android device
You should see the ads at the bottom, and some logs at the console as well.
04-02 12:42:45.352 25520 25520 I Ads : Starting ad request.
04-02 12:42:47.844 25520 25520 I Ads : Scheduling ad refresh 60000 milliseconds from now.
04-02 12:42:47.889 25520 25520 I Ads : Ad finished loading.
- iOS
For iOS it is possible as well. The problem is that it requires downloading the Google Mobile Ads SDK, and adding the GoogleMobileAds.framework
to the list of frameworks, so when compiling the native code it finds it.
This should be done in the jfxmobile-plugin
, so it is out of scope in this question for now.
Example project doesn't work
If you have a look at the list of prerequisites for deploying on Android, you need:
- the Android SDK
- the Build-tools
- And the Android Support Library
Open Android SDK manager (from /tools, run ./android
), and make sure you install the Android Support Library from Extras
:
Related Topics
Httpurlconnection.Getresponsecode() Returns -1 on Second Invocation
Android Gives Error "Cannot Fit Requested Classes in a Single Dex File"
Problems Importing Project into Android Studio Regarding Actionbarsherlock
Android Intent for Sending Email with Attachment
Listview Viewholder Checkbox State
Why Can't I Use Resources.Getsystem() Without a Runtime Error
Double Tap: Zoom on Android Mapview
How to Get Selected Xls File Path from Uri for Sdk 17 or Below for Android
Android App Crashes After Sdk-Tools Update Version (Noclassdeffound, Tool Version 22)
How to Do an Https Post from Android
Click Is Not Working on the Listitem Listview Android
Eclipse: Jvm Terminated. Exit Code=2
Why Menuitemcompat.Getactionprovider Returns Null