Preventing multiply output lines in Xamarin for Visual Studio on Android developement

I have used an very easy solution for this problem.
Whenever I need to do some output to the console (hint: you should only do this for you own purpose, later on you should Logging with respective logging levels in you app!) I do it the following way:

In the "App" class i have created a new method called "WriteLine" and there I use a dependency service call to execute some native java code on the android device:

public static void WriteLine(String line)

If you need help for using the DependencyService please look into further tutorials on that!
In the Implementation of the interface IOutput you just need the following code:

public void WriteLine(string line)

Like this you use the java "Console" class and you get the output at least one time less.


Use Locale in Android Application

I assume that your images are stored in the folder res-drawable. In Android Studio, location-specific behavior can be achieved by right-clicking on res and creating a new Android resource directory. In the following pop-up window, different qualifiers can be set, e.g. the Locale that allows will read out the language that is set on the mobile phone of the user and automatically uses the image from the corresponding directory (see the attached picture for details). You will notice that Android Studio creates the new directory with a specific suffix, e.g. "drawable-de" for german images. The general "drawable" folder will be used in case that the Locale of the user does not match any of your specified qualifiers and can therefore be seen as a default case.


Correctly change version code and name of Android Application

When you create your project with Android Studio, most of the files and settings are automatically created for you. You may therefore have missed that there is another file containing a versionCode and versionName, additional to the Manifest file. In your project directory, you will find a gradle file "app.gradle" that contains all the dependencies of your application. Further, in the defaultConfig section, you will see that there is a versionCode and a versionName that is probably identical to the code and name of your previous uploaded .apk file. Change these parameters and you are good to go for rolling out your file! The Play Store automatically notifies all the users of the old version about the changes and updates your product.


Use test ads in Android Application

What you are looking for are test ads. When you are using AdSense, you received a personal code that refers to your account. If somebody clicks on one of your ads, you receive the payment. Thus, in order to avoid this functionality, a test ad can be used that does not forward the action to your own account. Just replace the unit id of your ad that you have specified in the code with the following id: ca-app-pub-3940256099942544/6300978111. You will then see test ads in the correct size, but you cannot click on them.


Use a custom classloader and interfaces for such a task

This was a very tough issue - it was not allowed to copy the whole Swing application code into the Eclipse RCP because the maintenance effort would be doubled because of the doubled code base. So this was no option. Another problem would be the size of the deliverable - instead of shipping an Eclipse RCP application with 150 MB we would have to ship about 500 MB and we would have to roll out the 350 MB if there is one little bug in the Swing part.
The Swing application has to be standalone and can be started inside the Eclipse RCP application and outside of it but not at the same time. So we have to load the Swing application on demand at runtime. To achieve this we implemented a small library containing 2 interfaces - one interface was implemented on the Eclipse RCP side and the other one on the Swing application side. Now we just have to create a custom classloader which loads all the classes necessary for the Swing application and create an instance of the class which implements the interface.
This is just a way to integrate a legacy Swing application into an Eclipse RCP application - there are many more problem regarding SWT-AWT threading, different bugs in the SWT-AWT bridge etc.

Deactivate Apple Wireless Direct Link(AWDL)

To avoid the you have to deactivate the "Apple Wireless Direct Link" function of the OS X operating system. This can be accomplished by doing the following steps:

  • open a terminal window
  • enter the command "sudo ifconfig awdl0 down"
  • if you are asked for your password, enter it

If, for some reason you want to reactivate AWDL you have to do the following:

  • open a terminal window
  • enter the command "sudo ifconfig awdl0 up"
  • if you are asked for your password, enter it


Increase the VM memory size

- Find out the issue for the high memory consumption with debugging the program, try to fix or optimize that construct.
- Increase the max. heap size with a VM arg (the default is between 64MB to 1GB), for example use -Xmx2048m


Troubleshooting java applications - Using Java VisualVM

Using Java VisualVM (path: jdk/bin/jvisualvm.exe)

Each java application is shown under Applications > Local.
When an application is selected, some common properties of the application are shown (PID, host, ...) in the overview. In two subtabs ths JVM arguments and system properties are shown.
In the second tab, you can monitor the cpu usage, the heap size and the number of classes and threads.


PDF from Java Collection using JasperReports

  1. Add JasperReports depencency to pom.xml
  2. Create POJO
  3. Create JasperReports template students.jrxml with Jaspersoft Studio (see screenshot in the attachments)
    Compile the jrxml file to jasper file -> students.jasper file is created
  4. Run creation method createPDF("./students.jasper"); with compiled jasper file

public static void createPDF(String jasperFile){
List studentList = new ArrayList<>();
studentList.add(new Student("Lisa", "Mayer"));
studentList.add(new Student("Patrick", "Huber"));
studentList.add(new Student("Werner", "Reiter"));
studentList.add(new Student("Bettina", "Gruber"));
studentList.add(new Student("Dominik", "Berner"));

JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(studentList);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperFile, new HashMap(), beanColDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint, "students.pdf"); // see attachment students.pdf


@BatchSize explaination

first of all. your @BatchSize annotation must be placed on the Manufacturer-Entity. This annotation can only be placed on Collection-fields. assuming that you corrected that ...

if you take this jpql query

SELECT p FROM product as p where like :name

hibernate will execute 1 select for the products and as soon as you access the first manufacturer it will make a select for up to 50 different manufacturers.

select ... from manufacturere where ID in (?, ?, ?, ?, ...)

if your result has more than 50 different manufactuers and you iterate over it the next query with such an in clause will be executed as soon as you try to access a manufactorer that was not retrived in a previous batch-select.


Subscribe to java