f2prateek / dart

Extras binding and intent builders for Android apps.
Apache License 2.0
1.19k stars 88 forks source link

use in kotlin:Error:(8, 1) 错误: @InjectExtra fields must not be private or static. #186

Open fansmc opened 6 years ago

fansmc commented 6 years ago

i use gradle 2.3.0 whith android studio 3.0

@HensonNavigable
class DartTestActivity : AppCompatActivity() {
    @InjectExtra
    var mTitle: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Dart.inject(this)
        Toast.makeText(this, mTitle, Toast.LENGTH_SHORT).show()
    }
}

run make model this error appear:

"Error:(8, 1) 错误: @InjectExtra fields must not be private or static. (cn.fans.dartkotlintest.DartTestActivity.mTitle)
Error:(5, 1) 错误: @HensonNavigable class DartTestActivity must not contain any @InjectExtra annotation"

and in build/tmp/kapt3/stubs/.../DartTestActivity.java

@com.f2prateek.dart.HensonNavigable()
@kotlin.Metadata(mv = {1, 1, 8}, bv = {1, 0, 2}, k = 1, d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0005\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\b\u0007\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002J\u0012\u0010\t\u001a\u00020\n2\b\u0010\u000b\u001a\u0004\u0018\u00010\fH\u0014R\u001e\u0010\u0003\u001a\u00020\u00048\u0006@\u0006X\u0087\u000e\u00a2\u0006\u000e\n\u0000\u001a\u0004\b\u0005\u0010\u0006\"\u0004\b\u0007\u0010\b\u00a8\u0006\r"}, d2 = {"Lcn/fans/dartkotlintest/DartTestActivity;", "Landroid/support/v7/app/AppCompatActivity;", "()V", "mTitle", "", "getMTitle", "()Ljava/lang/String;", "setMTitle", "(Ljava/lang/String;)V", "onCreate", "", "savedInstanceState", "Landroid/os/Bundle;", "dartkotlintest_debug"})
 public final class DartTestActivity extends android.support.v7.app.AppCompatActivity {
    @org.jetbrains.annotations.NotNull()
    @com.f2prateek.dart.InjectExtra()
    private java.lang.String mTitle;
    private java.util.HashMap _$_findViewCache;

    @org.jetbrains.annotations.NotNull()
     public final java.lang.String getMTitle() {
        return null;
    }

    public final void setMTitle(@org.jetbrains.annotations.NotNull()
    java.lang.String p0) {
    }

    @java.lang.Override()
    protected void onCreate(@org.jetbrains.annotations.Nullable()
    android.os.Bundle savedInstanceState) {
    }

    public DartTestActivity() {
        super();
    }
}

why the "mTitle" is private ?

fansmc commented 6 years ago

when i use gradle3.0.1 in build.gradle

implementation 'com.f2prateek.dart:henson:2.0.3' kapt 'com.f2prateek.dart:henson-processor:2.0.3' then make model,will appear this

"Error:(4, 1) 错误: 不兼容的类型: NonExistentClass无法转换为Annotation"

and in build/tmp/kapt3/stubs/.../DartTestActivity.java

@kotlin.Metadata(mv = {1, 1, 8}, bv = {1, 0, 2}, k = 1, d1 = {"\u0000 \n\u0002\u0018\u0002\n\u0002\u0018\u0002\n\u0002\b\u0002\n\u0002\u0010\u000e\n\u0002\b\u0006\n\u0002\u0010\u0002\n\u0000\n\u0002\u0018\u0002\n\u0000\b\u0007\u0018\u00002\u00020\u0001B\u0005\u00a2\u0006\u0002\u0010\u0002J\u0012\u0010\n\u001a\u00020\u000b2\b\u0010\f\u001a\u0004\u0018\u00010\rH\u0014R$\u0010\u0003\u001a\u00020\u00048\u0006@\u0006X\u0087\u000e\u00a2\u0006\u0014\n\u0000\u0012\u0004\b\u0005\u0010\u0002\u001a\u0004\b\u0006\u0010\u0007\"\u0004\b\b\u0010\t\u00a8\u0006\u000e"}, d2 = {"Lcn/fans/dartkotlintest/DartTestActivity;", "Landroid/support/v7/app/AppCompatActivity;", "()V", "mTitle", "", "mTitle$annotations", "getMTitle", "()Ljava/lang/String;", "setMTitle", "(Ljava/lang/String;)V", "onCreate", "", "savedInstanceState", "Landroid/os/Bundle;", "dartkotlintestdebug"}) @error.NonExistentClass() public final class DartTestActivity extends android.support.v7.app.AppCompatActivity { @org.jetbrains.annotations.NotNull() private java.lang.String mTitle; private java.util.HashMap $_findViewCache;

@error.NonExistentClass()
public static void mTitle$annotations() {
}

@org.jetbrains.annotations.NotNull()
public final java.lang.String getMTitle() {
    return null;
}

public final void setMTitle(@org.jetbrains.annotations.NotNull()
java.lang.String p0) {
}

@java.lang.Override()
protected void onCreate(@org.jetbrains.annotations.Nullable()
android.os.Bundle savedInstanceState) {
}

public DartTestActivity() {
    super();
}

}

stephanenicolas commented 6 years ago

@fansmc I reformatted your first ticket, if you wanna check how to do it for future issues.. ;)

stephanenicolas commented 6 years ago

Unfortunately, I am not knowledegeable enough in Kotlin to understand the issue, but it's not related to Dart or Henson. Be sure to use the latest kotlin version, clean your project, and look for support in the Kotlin community.

fansmc commented 6 years ago

TKS. very much , i just don't understand why ,the 'var' in kotlin will compile to 'private' in java. in kotlin doc the 'var' just like 'public' in java ... i'll try other steps, thanks a lot

fansmc commented 6 years ago

resolve... when i write Model file use java ,it worked so. use Dart whih kotlin , it can't write Model file use kotlin ,if do this will appear error "Error:(7, 1) 错误: @InjectExtra fields must not be private or static. (xx.xx.xx.xxModet.aaa)"

class TargetModelKt { @InjectExtra("AAA") var aaa: String = ""

fun inject(bundle: Bundle) {
    Dart.inject(this, bundle)
}

}

public class TargetModel { @InjectExtra("AAA") String aaa;

public void inject(Bundle bundle) {
    Dart.inject(this, bundle);
}

}

StephenVinouze commented 6 years ago

@fansmc this should work.

class DartTestActivity : AppCompatActivity() {

    @InjectExtra
    lateinit var mTitle: String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Dart.inject(this)
        Toast.makeText(this, mTitle, Toast.LENGTH_SHORT).show()
    }
}
rovellipaolo commented 5 years ago

@fansmc to understand the issue you need to understand how Kotlin is compiled in Java (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html). In particular, that your var mTitle: String = "" (or any other Kotlin property for the matter) is compiled in a Java private field with a getter and a setter:

private String mTitle;

public String getMTitle() {
    return mTitle;
}

public void setMTitle(String mTitle) {
    this.mTitle = mTitle;
}

As @StephenVinouze pointed out, if you need to expose a Kotlin property as a public field in Java, you can use lateinit (see https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties-and-variables):

@InjectExtra
lateinit var mTitle: String

However, the above approach will work only as long as your intent's extras is non-nullable. If you need to expose a Kotlin nullable property as a public field in Java, then you can use the @JvmField annotation (see https://kotlinlang.org/docs/reference/java-to-kotlin-interop.html#instance-fields)

@InjectExtra
@JvmField
var mTitle: String? = null
zofnk commented 5 years ago

add apply plugin: 'kotlin-kapt' to the build.gradle file

and in dependencies :

implementation 'com.f2prateek.dart:dart:2.0.3'
kapt 'com.f2prateek.dart:dart-processor:2.0.3'

using:

@InjectExtra("title")
lateinit var mTitle: String