Build the iOS Things with J2Objc

Build the iOS Things with J2Objc

1. Install the J2Objc
Get the latest file from here https://j2objc.googlecode.com/files/j2objc-0.7.zip.

Unzip and put in the tool directory. Link it to the work directory.
>cd /opt
>sudo ln -s /Users/carl/tool/j2objc-0.7 ./j2objc-0.7
>sudo ln -s ./j2objc-0.7 ./j2objc

Add that directory to the bash path
>vi ~/.profile
export PATH=/opt/j2objc:$PATH
>. ~/.profile

2. Try the Sample
Follow the document, try a JAVA class hello world.
public class Hello {
  public static void main(String[] args) {
    System.out.println("hello, world");
  }
}

Compile the Java source code
>javac Hello.java
I got the class file Hello.class

Run the java class file based on JVM
>java Hello

Translate the JAVA source code to Object-C
>j2objc Hello.java
translating Hello.java
Translated 1 file: 0 errors, 0 warnings

I got 2 files, Hello.m and Hello.m

Compile the Object C source codes
>j2objcc -c Hello.m
Then there is a Hello.o file then.

Generate the executable file
>j2objcc -o hello Hello.o
Then there is a executable file named hello

Try to run that
>./hello
2013-04-11 13:52:44.019 hello[41640:707] hello, world

3. How to make that work with Maven
<build>
...
<plugin>     <groupId>org.codehaus.mojo</groupId>     <artifactId>exec-maven-plugin</artifactId>     <inherited>false</inherited>     <executions>          <execution>          <id>gen-objc</id>          <phase>package</phase>          <goals>               <goal>exec</goal>          </goals>          <configuration>               <executable>./gen-objc.sh</executable>               <arguments>                    <argument>${project.artifactId}</argument>               </arguments>          </configuration>          </execution>     </executions>

</plugin>
</build>

This make the execution of the shell script during the package phase
>mvn package

Here are the shell script gen-objc.sh
#!/bin/bash
#  call this shell script with the project artifactId

if [ ! -n "$1" ]
then
  echo "Usage: `basename $0` artifactId"
  exit
fi 

ARTIFACT_ID=$1
J2OBJC_OUT_DIR=gen-objc
SRC=target/${ARTIFACT_ID}-*-sources.jar
J2OBJCFLAGS="--no-package-directories -use-arc --prefixes sdk-core-prefixes.properties"

# Set this to point to j2objc home 
J2OBJC_DISTRIBUTION=/opt/j2objc
J2OBJC=${J2OBJC_DISTRIBUTION}/j2objc

if [ ! -e "$J2OBJC" ]; then
  echo "***************************************************"
  echo "***************************************************"
  echo " ERROR ... ERROR ... "
  echo " --> could not find ${J2OBJC} "
  echo " please install before continuing..."
  echo "***************************************************"
  echo "***************************************************"
  exit
fi

rm -rf ${J2OBJC_OUT_DIR}

${J2OBJC} -d ${J2OBJC_OUT_DIR} ${J2OBJCFLAGS} ${SRC};

cd ${J2OBJC_OUT_DIR}

rm I*.m

for y in `ls *`;
do
  sed -f ../objc_post_processing.sed $y > temp; mv temp $y;
done

# this is the customer things, if you do not need that, that is fine
# Replace SxxError with NSError *
rm SxxError.h

cd ..


In the file, sdk-core-prefixes.properties, they are the packages I think.
com.sillycat.easyrestapi.core:
com.sillycat.easyrestapi.core.impl:

Something like that.

For the file objc_post_pressing.sed, that maybe just get rid of some lines in the source codes.
# 0. generic preprocessing .. remove comments..
s/\(\/\/.*Created by \).*/\1 Xxxxxxx Compiler/g

# 1. j2objc generates fooWithNSString:(NSString *)name; for Java method
# foo(String name). We want foo:(NSString *)name;

s/With.*:(/:(/g

# 2. Replace JavaUtilDate with NSDate

s/JavaUtilDate/NSDate/g
s_#import "java/util/Date.h"__g
s/@class NSDate;//g

# 3. Replace JavaUtilList with NSArray *

s/id<JavaUtilList>/NSArray */g
s_#import "java/util/List.h"__g
s/@protocol JavaUtilList;//g

# 4. Replace JavaUtilSet with NSSet *

s/id<JavaUtilSet>/NSSet */g
s_#import "java/util/Set.h"__g
s/@protocol JavaUtilSet;//g

# 5. Replace XxxxxError with NSError *

s/id<XxxxxError>/NSError */g
s/@protocol XxxxxError;//g

# 6. Remove JreEmulation

s/#import "JreEmulation.h"//g

# 7. Replace 'withXxx' with 'with'

s/withXxx/with/g


4. Eclipse Plugin
http://j2objc-updatesite.hsapkota.com.au/4.2
Choose the directory of the installation j2obc and select a output directory. It works.

References:
https://code.google.com/p/j2objc/
http://hsapkota.com.au/index.php/projects/20-j2objc-eclipse-plugin-howto




猜你喜欢

转载自sillycat.iteye.com/blog/1846126