Kotlin-Android-Extension无法跨模块使用?

时间:2019-11-10 09:54:47

标签: android kotlin kotlin-android-extensions

我正在使用Kotlint 1.3.50和Android Studio 3.4

我有我的代码

public function createUserAccount($username,$password,$department){
    if($this->userExists($username)){
        return "USER_ALREADY_EXISTS";
    }
    else{
        $pass_hash = password_hash($password,PASSWORD_BCRYPT,["cost"=>8]);
        $pre_stmt = $this->con->prepare("INSERT INTO `user`(`id`, `username`, `password`, `department`) VALUES (?,?,?,?)");
        $pre_stmt->bind_param("sss",$pass_hash,$username,$department);
        $result = $pre_stmt->execute() or die($this->con->error);
        if($result){
            return $this->con->insert_id;
        }
        else{
            return "some error";
        }
    }

我的布局如下

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        my_txt.text = "ABC"
    }
}

如果它们在同一个模块中,则所有编译都可以。

但是,如果我将布局移至android库,则gradle如下所示

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <TextView
            android:id="@+id/my_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

我仍然可以apply plugin: 'com.android.library' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' android { compileSdkVersion 28 defaultConfig { minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:28.0.0' implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' } ,并且Android Studio不会抱怨。但是,当我编译时,它会抱怨

import kotlinx.android.synthetic.main.activity_main.*

好像kotlin-extension无法跨其他库交叉使用布局?

如果我更改为Unresolved reference: activity_main Unresolved reference: my_txt ,它将正常工作。

findViewById<TextView>(R.id.my_txt).text = "XYZ"

0 个答案:

没有答案