j2objc-contrib / j2objc-gradle

This plugin is no longer actively maintained. J2ObjC Gradle Plugin: enables Java source to be part of an iOS application's build so you can write an app's non-UI code (such as application logic and data models) in Java, which is then shared by Android apps (natively Java) and iOS apps (using J2ObjC), all as part of your usual Android Gradle build.
https://github.com/j2objc-contrib/j2objc-gradle
Apache License 2.0
139 stars 43 forks source link

Allow "com.android.library" plugin #181

Open theoriginalgri opened 9 years ago

theoriginalgri commented 9 years ago

In the old version, before restructuring on this project began, I was able to make my library be a "com.android.library" target instead of pure "java" (even though it warned about the missing java plugin).

I don't know the exact reasons if it would not work again with "com.android.library" instead of "java", but it would be great to have this feature back :)

The reason I need this plugin is that I've shared my networking code and use one of the few android.* classes given by j2objc: AsyncTask (currently pending pull request https://github.com/google/j2objc/pull/545).

advayDev1 commented 9 years ago

thanks for the feedback, @theoriginalgri. removing support for Android plugins (in favor of pure java) was a conscious decision @brunobowden and I made in balancing supporting the most common use-cases automatically (Java plugin) vs. dealing with hard-to-predict conflicts from a large API surface.

if you need a single or a few android. classes, is the right answer using the Android plugin, or is the right answer having a Gradle compile dependency on a particular Android library that provides that class(es) while using Java plugin? i don't know the answer yet; it has come up in my projects as well.

brunobowden commented 9 years ago

@theoriginalgri - let's leave this bug open for the moment and see how many people ask for this. It was a conscious decision as Advay said but we might re-evaluate this is the future. When you have a separated Java library, it forces you to have cleaner separation of UI and Logic... and you also get something that can be used server side as well. On Tue, Jun 16, 2015 at 9:36 PM Advay Mengle notifications@github.com wrote:

thanks for the feedback, @theoriginalgri https://github.com/theoriginalgri. removing support for Android plugins (in favor of pure java) was a conscious decision @brunobowden https://github.com/brunobowden and I made in balancing supporting the most common use-cases automatically (Java plugin) vs. dealing with hard-to-predict conflicts from a large API surface.

if you need a single or a few android. classes, is the right answer using the Android plugin, or is the right answer having a Gradle compile dependency on a particular Android library that provides that class(es) while using Java plugin? i don't know the answer yet; it has come up in my projects as well.

— Reply to this email directly or view it on GitHub https://github.com/j2objc-contrib/j2objc-gradle/issues/181#issuecomment-112620207 .

poernahi commented 9 years ago

I encountered this issue while trying to use some emulated android.util classes. I don't really want to pull the whole android dependency, just what is emulated by j2objc. I'm thinking since all these classes are available in their jre_emul.jar, I would be able to somehow use it as dependency for both android and ios build. However, I'm not quite sure yet how to tell gradle to do so.

brunobowden commented 9 years ago

@poernahi - for the moment, we require that you separate out the code for translation as a separate Java library that your Android app then depends on. This makes it much cleaner for organizing the code and doing the translation. Not sure when, or ever, we'll support translating an Android project.

brunobowden commented 9 years ago

This won't be supported for now and may never be.

For people finding this bug, please comment if you're interested in this.

poernahi commented 9 years ago

For additional info, I confirmed adding jre_emul.jar from j2objc as dependency works for my use case. Oh, and I did separate codes to be translated into a 'shared' module.

dependencies {
    compile files("/Users/z/j2objc/dist/lib/jre_emul.jar")
}
brunobowden commented 9 years ago

@poernahi - what Android code were you using?

poernahi commented 9 years ago

I am only using android.util.Log and org.json.* for the time being.

brunobowden commented 9 years ago

J2ObjC does automatically handle porting of android.util.Log. Right now, we're focused on clean Java only projects but we'll be happy to review pull requests. A new 0.3 version of the plugin has just been released which is a big improvement.

On Thu, Jul 2, 2015 at 11:43 PM poernahi notifications@github.com wrote:

I am only using android.util.Log and org.json.* for the time being.

— Reply to this email directly or view it on GitHub https://github.com/j2objc-contrib/j2objc-gradle/issues/181#issuecomment-118251832 .

confile commented 9 years ago

@brunobowden Would you recommend using android.util.Log or java.util.Logging in the shared project?

brunobowden commented 9 years ago

I would suggest java.util.Logging so it's properly cross platform and you can also use the code server side as well. Then if you need more sophisticated logging support, use SLF4J or something else.

On Fri, Jul 3, 2015 at 10:59 AM Michael Gorski notifications@github.com wrote:

@brunobowden https://github.com/brunobowden Would you recommend using android.util.Log or java.util.Logging in the shared project?

— Reply to this email directly or view it on GitHub https://github.com/j2objc-contrib/j2objc-gradle/issues/181#issuecomment-118402131 .

confile commented 9 years ago

@brunobowden Good, that is what I am currently doing.

poernahi commented 9 years ago

It turns out the jre_emul dependency is also needed to use j2objc annotations such as @ObjectiveCName. Maybe the jre_emul can be added as default dependency? I think the plugin should be able to find the jar in j2objc.home. I don't know if it will break pure java lib though. It is part of j2objc, but I don't know enough.

brunobowden commented 9 years ago

It currently defaults to including all the J2ObjC libraries by default. From J2objcConfig.java:

https://github.com/j2objc-contrib/j2objc-gradle/blob/master/src/main/groovy/com/github/j2objccontrib/j2objcgradle/J2objcConfig.groovy#L174

    /**
     * Additional libraries that are part of the j2objc distribution.
     * <p/>
     * For example:
     * <pre>
     * translateJ2objcLibs = ["j2objc_junit.jar", "jre_emul.jar"]
     * </pre>
     */
    // J2objc default libraries, from $J2OBJC_HOME/lib/...
    // TODO: auto add libraries based on java dependencies, warn on version differences
    List<String> translateJ2objcLibs = [
            // Comments indicate difference compared to standard libraries...
            // Memory annotations, e.g. @Weak, @AutoreleasePool
            "j2objc_annotations.jar",
            // Libraries that have CycleFinder fixes, e.g. @Weak and code removal
            "j2objc_guava.jar", "j2objc_junit.jar", "jre_emul.jar",
            // Libraries that don't need CycleFinder fixes
            "javax.inject-1.jar", "jsr305-3.0.0.jar",
            "mockito-core-1.9.5.jar"]
poernahi commented 9 years ago

Please correct me if I'm wrong, I'm still very new to gradle. It looks like the translateJ2objcLibs property is not used by the compileJava task which actually calls javac. I confirmed that my compiler argument does not have any -classpath ... (I have no external dependency). This caused javac to choke on the j2objc annotation imports since it cannot find the classes.

brunobowden commented 9 years ago

Good point. You'd need a substitution library to work with the Java compile. This will need more thought about how to do this.

On Mon, Jul 6, 2015, 1:21 PM poernahi notifications@github.com wrote:

Please correct me if I'm wrong, I'm still very new to gradle. It looks like the translateJ2objcLibs property is not used by the compileJava task which actually calls javac. I confirmed that my compiler argument does not have any -classpath ... (I have no external dependency). This caused javac to choke on the j2objc annotation imports since it cannot find the classes.

— Reply to this email directly or view it on GitHub https://github.com/j2objc-contrib/j2objc-gradle/issues/181#issuecomment-119001518 .

maxbritto commented 9 years ago

Hi guys, I coded my shared code using org.json library based on this discussion : https://groups.google.com/forum/#!searchin/j2objc-discuss/org.json/j2objc-discuss/qom_hE8Rsos/UKs--sNZNlgJ I understand your point and your need to remove extra libraries for the translator/compiler. Though is there any way for those who need it to add it as an external jar ? Thanks

maxbritto commented 9 years ago

I've found a way to include it. In case someone else needs it :

dependencies {
    compile 'org.json:json:20141113'
}

j2objcConfig {
 //...
    translateArgs '--build-closure'
    translateSourcepaths 'lib/jre_emul.jar'
 //...
}
advayDev1 commented 9 years ago

@maxbritto - I'm pretty confused as to why lib/jre_emul.jar is on your sourcepath. That is the JAR that is already translated to libjre_emul.a.

maxbritto commented 9 years ago

@advayDev1 : you're right, the translate parameters were useless in that case. The only thing I needed was actually this :

dependencies {
    compile 'org.json:json:20141113'
}

Even if I'm starting to grasp the concepts behind j2objc, it is still mostly blurry to me as to how to make it work on a real case. That's why when I made it work, I didn't realize some of the stuff I did were not necessary. Thanks for the clarification