Few cents about my commits

AltPods: pods updated - 1.10.0-SNAPSHOT

|

AltPods were updated to v1.10.0-SNAPSHOTS to sync with recent releases. Part of list didn’t receive any API update hovewer bindings were re-generated against recent version of frameworks. Update list look as bellow:

Updated pods

These pods were pushed to https://oss.sonatype.org/content/repositories/snapshots maven repo under 1.9.0-SNAPSHOTS version. Source code @github

Updates are not fully tested, please open issue if bug found.

NB: AltPods – are robopods kept at my personal standalone repo. This is done to prevent main robopods repo from turning into code graveyard. As these pods have low interest of community and low chances to be updated.

fix 509 + tutorial: how to generate icudt51l from source

|

issue 509 says that Collator api fails. Short investigations shows that the root case is missing files for collator/normalizer in icu.dat. Simple solution for user is to generate user files and use it as part of application. But icu data customizer is not available anymore online and python scripts for customization are not available for old v51.
icu.dat was built manualy and delivered as PR510. Steps how to do it described bellow.

Tutorial. Building icu.dat from sources

fix: JNI_OnLoad case

|

Subsurfer reported several issues related to JNI. Quick test exposed two issues:

Issue 1

JNI FindClass was not successful when was called from JNI_OnLoad (as result of System.loadLibrary for example) root case: top java class on the stack was java.lang.Runtime and its classLoader was NULL (corresponds to BootClassLoader). As result user class was looked in BC list and failed. fix: use systemClassLoader in case as top java class is BC class. issue is similar to robovm/robovm#352

Issue 2

If application uses a dynamic framework or library that exposes JNI_OnLoad it being called in context of application image. This happens as dyld loads all symbols from libraries referenced by LC_LOAD_DYLIB command. as result library JNI_OnLoad got called on early state while JNI is not initialized yet. This caused GPF due null pointer de-reference. fix: ignore JNI_OnLoad for application image

Fix was delivered as PR507.

AltPods: pods updated - 1.9.0-SNAPSHOT

|

AltPods were updated to v1.9.0-SNAPSHOTS to sync with recent releases. Part of list didn’t receive any API update hovewer bindings were re-generated against recent version of frameworks. Update list look as bellow:

New frameworks

Updated pods

Other changes

  • FIRCrashlytics.registerDefaultJavaUncaughtExceptionHandler that acts similar to same method in NSException but convert stack traces to FIRExceptionModel (check post for details)

These pods were pushed to https://oss.sonatype.org/content/repositories/snapshots maven repo under 1.9.0-SNAPSHOTS version. Source code @github

Updates are not fully tested, please open issue if bug found.

NB: AltPods – are robopods kept at my personal standalone repo. This is done to prevent main robopods repo from turning into code graveyard. As these pods have low interest of community and low chances to be updated.

Crashlytics: proper java stacktrace

|

This post continues tutorial for proper initialization of crash reporters t.
Option to track java exception was to use NSException.registerDefaultJavaUncaughtExceptionHandler(). It builds string presentation of exception creates NSException with this text as reason. At same time stack traces will be messed and will point to code where NSException is created.

FIRExceptionModel

Firebase Crashlytics provide special API FIRExceptionModel:

The Firebase Crashlytics Exception Model provides a way to report custom exceptions to Crashlytics that came from a runtime environment outside of the native platform Crashlytics is running in.

Exact case for RoboVM. To have it working its enough to setup Thread.setDefaultUncaughtExceptionHandler as bellow to convert exception stack traces into FIRExceptionModel elements:

Thread.setDefaultUncaughtExceptionHandler((thread, ex) -> {
    FIRExceptionModel model = new FIRExceptionModel(ex.getClass().getName(), ex.getMessage() != null ? ex.getMessage() : "");
    NSMutableArray<FIRStackFrame> modelFrames = new NSMutableArray<>();
    StackTraceElement[] stackTraces = ex.getStackTrace();
    if (stackTraces != null) {
        for (StackTraceElement st : stackTraces) {
            String symbol = st.getClassName() + '.' + st.getMethodName();
            String fileName;
            int lineNo = st.getLineNumber();
            if (lineNo < 0)
                lineNo = 0;
            if (st.isNativeMethod()) {
                fileName = "Native Method";
            } else {
                fileName = st.getFileName();
                if (fileName == null)
                    fileName = "Unknown Source";
            }

            FIRStackFrame frame = new FIRStackFrame(symbol, fileName, lineNo);
            modelFrames.add(frame);
        }
    }
    model.setStackTrace(modelFrames);
    FIRCrashlytics.crashlytics().recordExceptionModel(model);

    // kill app
    throw new ThreadDeath();
});

Note

Crashes will be reported as non-fatal !

Bonus

To get debug output from Crashlytics in simulator its not enought to provide -FIRDebugEnabled. Adding -FIRLoggerForceSTDERR helps.

tutorial: building a framework

|

Short guide how to get framework binary to use with RoboVM if it is provided by the vendor.

Building using Carthage

Carthage is intended to be the simplest way to add frameworks to your Cocoa application

Most github hosted projects provide a way to build artifacts using Carthage. Follow quick start guide for installation. Process is following:

  • create Cartfile and set its content up to project info, e.g.: github "Alamofire/Alamofire" ~> 4.7.2
  • run carthage update to build.

Result – dynamic framework.

Getting from CocoaPods

CocoaPods is a dependency manager for Swift and Objective-C Cocoa projects

CocoaPods project is highly intended to be used as part of Xcode project.
Often vendors provide instructions on how to include their artifacts into project dependencies using CocoaPods. Xcode project is required, follow step to create one and build the pod:

  • create new iOS - Single View App project in XCode and exit it;
  • create Podfile in same folder where ${projectname}.xcodeproj is located;
  • fill it with references from vendor (PersonalizedAdConsent here as example):
    platform :ios, '9.0'
    target 'pods_tutorial' do
    use_frameworks!
    use_modular_headers!
    pod 'PersonalizedAdConsent'
    end   
    
  • run pod install --repo-update to add dependencies to project;
  • cd Pods
  • build for simulator:
    xcodebuild -configuration Release -sdk iphonesimulator13.5 -scheme PersonalizedAdConsent build \
             CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO \
             CONFIGURATION_BUILD_DIR=artifacts_sim/
    
  • build for devices:
    xcodebuild -configuration Release -sdk iphoneos13.5 -scheme PersonalizedAdConsent build \
             CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO \
             CONFIGURATION_BUILD_DIR=artifacts_device/
    

Few moments here:

  • target 'pods_tutorial' do specifies the target as it is in your Xcode project;
  • -scheme PersonalizedAdConsent specifies the scheme added by pods, to list all schemes in project use xcodebuild -list;
  • -sdk iphoneos13.5 specifies SDK available in Xcode, use xcodebuild -showsdks to list all available;
  • CONFIGURATION_BUILD_DIR=artifacts_sim/ specifies the directory, where to put binaries;
  • use_frameworks! and use_modular_headers! command to build a dynamic framework, if removed it will result in static library.

Building out of sources

Clone/download Google Mobile Ads Consent SDK as an example:

  • Navigate to folder where PersonalizedAdConsent.xcodeproj is located;
  • use xcodebuild -list to get list of targets – here is PersonalizedAdConsent;
  • build same way as in case of cocoapods, simulator:
    xcodebuild -configuration Release -sdk iphonesimulator13.5 -scheme PersonalizedAdConsent build \
             CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO \
             CONFIGURATION_BUILD_DIR=artifacts_sim/
    
  • build for devices:
    xcodebuild -configuration Release -sdk iphoneos13.5 -scheme PersonalizedAdConsent build \
             CODE_SIGNING_REQUIRED=NO CODE_SIGNING_ALLOWED=NO \
             CONFIGURATION_BUILD_DIR=artifacts_device/
    

Note: this way static library/framework will be build.

AltPods: pods updated - 1.8.0-SNAPSHOT

|

AltPods were updated to v1.8.0-SNAPSHOTS to sync with recent releases. Part of list didn’t receive any API update hovewer bindings were re-generated against recent version of frameworks. Update list look as bellow:

New frameworks

Updated pods

These pods were pushed to https://oss.sonatype.org/content/repositories/snapshots maven repo under 1.8.0-SNAPSHOTS version. Source code @github

Updates are not fully tested, please open issue if bug found.

NB: AltPods – are robopods kept at my personal standalone repo. This is done to prevent main robopods repo from turning into code graveyard. As these pods have low interest of community and low chances to be updated.

Framework target: fixing back JNI support

|

Previous rework broke JNI. Rework added logic to start JVM automatically once framework is loaded to provide ObjectiveC classes from Java side available as soon as possible. A bad thing is that JVM structures required for JNI call were not exposed. A try to initialize(second) JVM with JNI_CreateJavaVM will fail.
Goal of the fix is to provide ability to have both JNI and ObjectiveC framework operational.

Disabling automatic JVM startup

This can be controlled with another key to robovm.xml. Instead, automatic JVM startup will be disabled if JNI_CreateJavaVM is present in the list of exported symbols:

<exportedSymbols>
    <symbol>JNI_CreateJavaVM</symbol>
</exportedSymbols>

Compiler will generate(only in case of Framework target) _bcFrameworkSkipJavaVMStartup symbol that will be recognized by native code and JVM start up will be skipped.

Interoperability

Once JVM is created it is a good idea(but subject for JNI code design) to ping back framework support code to pre-load all objective-c custom classes. In this case JNI based code should call following function:

void rvmInitializeFrameworkWithJVM(JavaVM* externalVm, JNIEnv *externalEnv);

Code

Code was delivered as PR497

okhttp 4x: failed to connect to cloudflare protected server

|

okhttp stopped working again with RoboVM: once project migrated to kotlin and refactored code old issue is back:

java.lang.NoClassDefFoundError: android/os/Build$VERSION

Workaround is same:

package android.os;
public class Build {
    public final static class VERSION {
        public final static int SDK_INT = 21;
    }
}

Worse thing is that okhttp unable to connect to cloudflare protected servers:

javax.net.ssl.SSLHandshakeException: javax.net.ssl.SSLProtocolException: SSL handshake terminated: ssl=0x7fae5b122560: Failure in SSL library, usually a protocol error
error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure

While old good URL u = new URL(url); u.openConnection(); works.

Root case

Usually handshake failed when parameter of connection failed to negotiate. Good tool for investigating such things is a sniffer like Wireshark. Longs story short – it fails as its not able to negotiate a cipher to be used for connection. Cloudflare supports following ones:

RoboVM compiler: out of memory -- huge class case

|

RoboVM compiler is quite memory hungry by its nature: creates and manipulates a bunch of strings, have to keep parsed classes structures while compiling etc. Also there is a bug opened #150. Usually its enough to give JVM bigger heap (-Xmx4g) to make everyone happy.
There was a report on gitter channel about OOM that happens on a single file that can’t be fixed by increasing heap size. File just contained about 8K static final string fields.

To reproduce the case dummy file with 8000 strings was generated:

public class DBkeys
{
    static public final String T0001 = "";
    static public final String T0002 = "";
    // lot of more these 
    static public final String T8000 = "";
}

Compilation using default heap settings just produce Out of memory exception but once JVM granted plenty of ram (-Xmx32g) it ends up with exception:

java.lang.OutOfMemoryError: Required array length too large

RoboVM went beyond of JVM limits trying to allocate byte array with size longer than JVM supported (Integer.MAX_VALUE - 2 gig). In other words – giving all memory you have to JVM heap will not solve this issue.

Root case

OOM happens when RoboVM compiles java code into LLVM IR code. This constant string only class doesn’t fit in MAX possible 2 GB java string. Let’s see what is in there.