Generating Java Bindings in 1.3 beta - How to do so?

With 1.3, I’m having trouble understanding how to generate Java bindings for native libraries, such as Google’s Firebase. I’m not sure where to put the native library in the project such that it can be found in the project tree where I can right-click it to begin the process and/or, if using the new GUI, how to fill in the information so that the bindings can be created. (In the dialog that comes up after hitting +, what should go in the Name field).

Is there any new documentation for this process for 1.3 beta?

Mike

3 Likes

I also very interested in this.

It is also somewhat related to cocoapods support… I tried cocoapods but simple “pod install” on a MOE 1.3 project made bunch of changes in xcode files that broke project build. Also cocoapods is giving warnings about using xcode workspace instead of xcode project (the latter is used by MOE).

So, eagerly awaiting for new/updated tutorials for linking cocoapods and generating bindings.

Dear icd_mike and Eugene,

the tutorial for this will probably only be ready at 1.3 release. However these are the basic steps to get cocoapods to work:

  • Create a new MOE 1.3 project

  • Create a Podfile in the project’s xcode directory

  • Add the pods you need

    platform :ios, '8.0'
    use_frameworks!
    
    target 'MyApp' do
    	pod 'AFNetworking', '~> 2.6'
    end
    
  • Run pod install

  • Open the workspace in Xcode

  • Open the scheme manager and make your scheme ‘shared’

  • Open the build.gradle file and set the following values:

    moe {
    	xcode {
    		project 'xcode/MyApp.xcodeproj'
    		mainTarget 'MyApp'
    		testTarget 'MyApp-Test'
    
    		// SET THESE VALUES
    		workspace 'xcode/MyApp.xcworkspace'
    		mainScheme 'MyApp'
    		testScheme 'MyApp-Test'
    	}
    }
    
  • In Android Studio create an nbc file so you can generate bindings, I attached an example file.

    pods.nbc (305 Bytes)

  • I put my nbc file in my MOE module’s root

  • Open the nbc file and generate bindings

Best Regards,
Kristóf

2 Likes

Wow! That was quick, thank you Kristóf. I will have to try this.

One last question, is 1.3 beta2 going to have bindings for CaptiveNetwork, or those will be added some time later?

Dear Eugene,

1.3.0-beta-2 will have captive network bindings. If you need other iOS APIs which we currently don’t have, please comment on this GitHub issue:

https://github.com/multi-os-engine/multi-os-engine/issues/75

Best Regards,
Kristóf

1 Like

Kristóf, I tried to follow your instructions, got bindings generated for AFNetworking sample, but then build is failing with the “framework not found AFNetworking” error. Any idea what’s missing?

Ld /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/Objects-normal/i386/CocoapodsTest normal i386
    cd /Users/eu/Documents/workspace2/CocoapodsTest/xcode
    export IPHONEOS_DEPLOYMENT_TARGET=9.3
    export PATH="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin:/Applications/Xcode.app/Contents/Developer/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin"
    /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch i386 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator10.2.sdk -L/Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/Debug-iphonesimulator -F/Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/Debug-iphonesimulator -F/Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/sym/Debug-iphonesimulator/AFNetworking -F/Users/eu/Documents/workspace2/CocoapodsTest/xcode/../build/moe/sdk/sdk/iphonesimulator -filelist /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/Objects-normal/i386/CocoapodsTest.LinkFileList -Xlinker -rpath -Xlinker @executable_path/Frameworks -Xlinker -rpath -Xlinker @loader_path/Frameworks -Xlinker -rpath -Xlinker @executable_path/Frameworks -mios-simulator-version-min=9.3 -Xlinker -object_path_lto -Xlinker /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/Objects-normal/i386/CocoapodsTest_lto.o -Xlinker -export_dynamic -Xlinker -no_deduplicate -Xlinker -objc_abi_version -Xlinker 2 -stdlib=libc++ -fobjc-arc -fobjc-link-runtime -framework AFNetworking -sectcreate __OATDATA __oatdata /Users/eu/Documents/workspace2/CocoapodsTest/xcode/..//build/moe/main/xcode/Debug-iphonesimulator/i386.oat -sectcreate __ARTDATA __artdata /Users/eu/Documents/workspace2/CocoapodsTest/xcode/..//build/moe/main/xcode/Debug-iphonesimulator/i386.art -segprot __OATDATA rwx rx -segprot __ARTDATA rwx rw -pagezero_size 4096 -lstdc++ -framework MOE -Xlinker -sectcreate -Xlinker __TEXT -Xlinker __entitlements -Xlinker /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/CocoapodsTest.app.xcent -framework AFNetworking -Xlinker -dependency_info -Xlinker /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/Objects-normal/i386/CocoapodsTest_dependency_info.dat -o /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/Objects-normal/i386/CocoapodsTest
ld: warning: directory not found for option '-F/Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/sym/Debug-iphonesimulator/AFNetworking'
ld: warning: -pagezero_size not page aligned, rounding down
ld: framework not found AFNetworking
clang: error: linker command failed with exit code 1 (use -v to see invocation)

** BUILD FAILED **


The following build commands failed:
	Ld /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/obj/CocoapodsTest.build/Debug-iphonesimulator/CocoapodsTest.build/Objects-normal/i386/CocoapodsTest normal i386
(1 failure)
:moeMainDebugIphonesimulatorXcodeBuild FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':moeMainDebugIphonesimulatorXcodeBuild'.
> Task failed, you can find the log file here: /Users/eu/Documents/workspace2/CocoapodsTest/build/moe/xcodebuild/XcodeBuild-main-debug-iphonesimulator.log

Dear Eugene,

please make sure moe.xcode.workspace is set in your build.gradle.

Best Regards,
Kristóf

1 Like

I have this:

// Setup Multi-OS Engine
moe {
    xcode {
        project 'xcode/CocoapodsTest.xcodeproj'
        mainTarget 'CocoapodsTest'
        testTarget 'CocoapodsTest-Test'

        workspace 'xcode/CocoapodsTest.xcworkspace'
        mainScheme 'CocoapodsTest'
        testScheme 'CocoapodsTest-Test'
    }
}
1 Like

Dear Eugene,

I created a new project with my guide above, but it worked fine and the only way I could reproduce this exact message was to remove the moe.xcode.workspace setting.
Could you please open the xcode/CocoapodsTest.xcworkspace file and try running from Xcode?

Best Regards,
Kristóf

When I try to run from XCode it gives me similar error. Am I missing some step in XCode project/workspace setup? How do I add framework in there?

ld: warning: directory not found for option '-F/Users/eu/Library/Developer/Xcode/DerivedData/CocoapodsTest-gclnyaukpsaqckcqqzysjytnqxtu/Build/Products/Debug-iphonesimulator/AFNetworking'
ld: warning: -pagezero_size not page aligned, rounding down
ld: framework not found AFNetworking
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Dear Eugene,

this is something pod install should take care of. Pleased close Xcode and run pod install again. If you still have the same issue after this, could you please share your project with us?

Best Regards,
Kristóf

That did it. Closing xcode and re-runing pod install couple more times fixed it. It runs from gradle now. My next step would be to get Charts pod bindings working…

1 Like

Kristóf I’ve tried to generate bindings for the Charts pod. This one is written in Swift 2.x, so Xcode insisted on converting it to Swift 3 or else said it can’t build it.

Then after I switched to MOE and tried to generate bindings. But the binding generator gave me bunch of syntax errors in a non-scrollable popup with size larger than my screen that I can’t copy any text from and those errors wasn’t shown in Console view in Eclipse.

But even with the errors, it generated some Java classes, but these classes have Java compilation errors (e.g. incompatible return types on overwritten methods).

I attached my test project. So, after unzipping it, and running pod install you can open xcode workspace to build or convert installed pods and then open Java IDE to see issue with the binding generator. I put my binding file into the project root. I’ve used “Charts-Swift.h” as an include, that was a blind guess…

Please let me know if you could discover anything. Thank you.

CocoapodsTest.zip (93.2 KB)

Dear Eugene,

you were quite close to a good solution. The errors you saw were due to some types not being resolved. That is usually because of missing imports. After I modified the Import headers section to the following, binding generation completed successfully:

#import <UIKit/UIKit.h>
#import "Charts-Swift.h"

Best Regards,
Kristóf

Thank you Kristóf. It generates bindings successfully now, but there are compilation issues in generated Java code. See below.

It seem like bindings generator didn’t generate some of the parameter and return types correctly. Also some of initialize() methods in subclasses of UIView wasn’t generated as static. I am going to try to fix those errors manually and see if I can use Charts UI views in my app.

Also, should I open an issue regarding error reporting? It would be much more convenient to show generator errors in MOE Console view in Eclipse instead of modal popup dialog.

The return type is incompatible with ChartBaseDataSet.valueFormatter()	BarLineScatterCandleBubbleChartDataSet.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 413	Java Problem
The return type is incompatible with ChartBaseDataSet.valueFormatter()	PieChartDataSet.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 448	Java Problem
The type ChartBaseDataSet must implement the inherited abstract method IChartDataSet.setValueFormatter(Object)	ChartBaseDataSet.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type ChartDefaultFillFormatter must implement the inherited abstract method IChartFillFormatter.getFillLinePositionWithDataSetDataProvider(Object, Object)	ChartDefaultFillFormatter.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 36	Java Problem
The type ChevronDownShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	ChevronDownShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type ChevronUpShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	ChevronUpShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type CircleShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	CircleShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type CrossShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	CrossShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type LineChartDataSet must implement the inherited abstract method ILineChartDataSet.setFillFormatter(Object)	LineChartDataSet.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type SquareShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	SquareShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type TriangleShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	TriangleShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
The type XShapeRenderer must implement the inherited abstract method IShapeRenderer.renderShapeWithContextDataSetViewPortHandlerPointColor(CGContextRef, Object, ChartViewPortHandler, CGPoint, UIColor)	XShapeRenderer.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 37	Java Problem
This instance method cannot override the static method from UIView	BubbleChartView.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 324	Java Problem
This instance method cannot override the static method from UIView	CombinedChartView.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 374	Java Problem
This instance method cannot override the static method from UIView	ScatterChartView.java	/CocoapodsTest/src/main/java/com/euxx/charts/charts	line 320	Java Problem

After fixing compilation errors in generated bindings I’ve tried to add chart component into ui controller, but getting “unrecognized selector” error.

Is it related to binding issues or I missed something? I noticed that none of generated bindings have @RegisterOnStartup annotation. Do I need to somehow register them manually, given the warnings below?

I attached updated project here.
CocoapodsTest.zip (404.1 KB)

art I 22472 2387661 /Volumes/SSD/gh-moe-master-1.3.0-beta-1/aosp/art/runtime/parsed_options.cc:449] setting boot class path to /Users/eu/Library/Developer/CoreSimulator/Devices/2892A0BC-9C40-4FCD-B575-8607CE08D90D/data/Containers/Bundle/Application/84A1991A-0565-4EB0-A442-D44FDE46CF5A/CocoapodsTest.app/application.jar:/Users/eu/Library/Developer/CoreSimulator/Devices/2892A0BC-9C40-4FCD-B575-8607CE08D90D/data/Containers/Bundle/Application/84A1991A-0565-4EB0-A442-D44FDE46CF5A/CocoapodsTest.app

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/NatJ.cpp:353 DEBUG: Method 'boolean java.lang.reflect.Method.isDefault()' is not accessible.

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class NSUIView, but it can not be found. Fallback to indirect super class.
/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class ChartViewBase, but it can not be found. Fallback to indirect super class.

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class BarLineChartViewBase, but it can not be found. Fallback to indirect super class.

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class BarChartView, but it can not be found. Fallback to indirect super class.

2017-01-12 16:28:55.900 CocoapodsTest[22472:2387661] -[UIView setNoDataText:]: unrecognized selector sent to instance 0x7be444a0
org.moe.natj.objc.ObjCException: -[UIView setNoDataText:]: unrecognized selector sent to instance 0x7be444a0
	at apple.uikit.c.UIKit.UIApplicationMain(Native Method)
	at com.euxx.cocopods.Main.main(Main.java:18)

art I 22473 2387703 /Volumes/SSD/gh-moe-master-1.3.0-beta-1/aosp/art/runtime/parsed_options.cc:449] setting boot class path to /Users/eu/Library/Developer/CoreSimulator/Devices/2892A0BC-9C40-4FCD-B575-8607CE08D90D/data/Containers/Bundle/Application/84A1991A-0565-4EB0-A442-D44FDE46CF5A/CocoapodsTest.app/application.jar:/Users/eu/Library/Developer/CoreSimulator/Devices/2892A0BC-9C40-4FCD-B575-8607CE08D90D/data/Containers/Bundle/Application/84A1991A-0565-4EB0-A442-D44FDE46CF5A/CocoapodsTest.app

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/NatJ.cpp:353 DEBUG: Method 'boolean java.lang.reflect.Method.isDefault()' is not accessible.

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class NSUIView, but it can not be found. Fallback to indirect super class.
/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class ChartViewBase, but it can not be found. Fallback to indirect super class.

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class BarLineChartViewBase, but it can not be found. Fallback to indirect super class.

/Volumes/SSD/gh-moe-master-1.3.0-beta-1/moe/natj/natj/src/main/native/natj/ObjCRuntime.mm:1481 WARNING: Binding class refers to class BarChartView, but it can not be found. Fallback to indirect super class.

2017-01-12 16:28:56.514 CocoapodsTest[22473:2387703] -[UIView setNoDataText:]: unrecognized selector sent to instance 0x7b14a6c0
org.moe.natj.objc.ObjCException: -[UIView setNoDataText:]: unrecognized selector sent to instance 0x7b14a6c0
	at apple.uikit.c.UIKit.UIApplicationMain(Native Method)
	at com.euxx.cocopods.Main.main(Main.java:18)

Tried to generate Charts bindings using 1.3.0-beta-2 but still getting the same issue. The following swift code declared at IChartDataSet:

/// Custom formatter that is used instead of the auto-formatter if set
var valueFormatter: IValueFormatter? { get set }

MOE generates:

@Generated
@Selector("valueFormatter")
@MappedReturn(ObjCObjectMapper.class)
Object valueFormatter();

@Generated
@Selector("setValueFormatter:")
void setValueFormatter(@Mapped(ObjCObjectMapper.class) Object value);

Note the Object type instead of IValueFormatter and Java interface for IValueFormatter type is not generated.

Could you please advise what am I missing?

I managed to manually tweak generated bindings to get the Charts pod work. It looks like binding generator is choking on the IValueFormatter.swift file. Very likely because of the following code in Charts pod. Note that protocol name and objc name aren’t not the same, so MOE generates IChartValueFormatter.java class, but all references in swift to IValueFormatter are generated as Object.

@objc(IChartValueFormatter)
public protocol IValueFormatter : NSObjectProtocol

After I manually edited them all I was able to hook up a basic Charts widget and get it to draw the chart in iOS app.

Actually this issue should be fixed in 1.3 beta 2. Did you regenerate the bindings with the 1.3 beta 2 natjgen?

Gergerly, yes, I’m running beta-2 and I used nbc editor from updated Eclipse plugin to generate bindings.