Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fansmc opened this issue Nov 23, 2017 · 8 comments

Comments

@fansmc
Copy link

fansmc commented Nov 23, 2017

i use gradle 2.3.0 whith android studio 3.0

  • in model build.gradle
compile 'com.f2prateek.dart:dart:2.0.3'
kapt 'com.f2prateek.dart:dart-processor:2.0.3'
compile 'com.f2prateek.dart:henson:2.0.3'
kapt  'com.f2prateek.dart:henson-processor:2.0.3'
  • in DartTestActivity.java
@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
Copy link
Author

fansmc commented Nov 23, 2017

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;", "dartkotlintest_debug"})
@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
Copy link
Collaborator

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

@stephanenicolas
Copy link
Collaborator

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
Copy link
Author

fansmc commented Nov 29, 2017

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
Copy link
Author

fansmc commented Dec 1, 2017

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)"

  • this code will product compile error[@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)
}

}

  • just use java to write mode file, it worked

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

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

}

@StephenVinouze
Copy link
Contributor

@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
Copy link

rovellipaolo commented Jan 11, 2019

@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
Copy link

zofnk commented Aug 19, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants