4

Ich habe ein Anwendungsmodul, sagen wir "Test". Das Modul "Test" hängt von einem Untermodul B ab. Beide aktivieren die Datenbindung. Im Bibliothekmodul B erstelle ich eine einfache Aktivität mittels Databinding, deren Zweck die Wiederverwendbarkeit ist, zum Beispiel: Ich kann einen Basis-Login-Bildschirm erstellen und ihn später in vielen Apps verwenden. Im Folgenden finden Sie Beispielcode im Paket B.Android Databinding in Sub-Modul

package com.test.packageb 

     open class MainActivity : AppCompatActivity() { 

     lateinit var binding : ActivityMainBinding 

     override fun onCreate(savedInstanceState: Bundle?) { 
      super.onCreate(savedInstanceState) 
      binding = DataBindingUtil.setContentView(this, R.layout.activity_main) 
     } 
    } 

und dann in „Test“ Modul, kann ich nur einfach MainActivity Klasse erben Dinge zu tun, anpassen, wie folgt aus:

class MainActivity1 : MainActivity(){ 

    override fun onCreate(savedInstanceState: Bundle?) { 
     super.onCreate(savedInstanceState) 
    } 

    fun doSomething(){ 
     binding.rootLayout.setBackgroundResource(R.color.colorPrimary) 
    } 
} 

aber wenn ich versuche, um "Test" -Anwendung zu starten, habe ich diesen Fehler erhalten

Was habe ich vermisst? Muss noch etwas umgesetzt werden?

Test-App build.gradle

apply plugin: 'com.android.application' 

apply plugin: 'kotlin-android' 
apply plugin: 'kotlin-kapt' 
apply plugin: 'kotlin-android-extensions' 

android { 
    compileSdkVersion 26 
    buildToolsVersion "26.0.1" 
    defaultConfig { 
     applicationId "com.test.testapplication" 
     minSdkVersion 15 
     targetSdkVersion 26 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 

    dataBinding{ 
     enabled true 
    } 

    buildTypes { 
     debug { 

     } 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    kapt 'com.android.databinding:compiler:3.0.0-beta4' 
    implementation fileTree(include: ['*.jar'], dir: 'libs') 
    implementation 'com.android.support:appcompat-v7:26.0.2' 
    implementation 'com.android.support.constraint:constraint-layout:1.0.2' 
    testImplementation 'junit:junit:4.12' 
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 
    implementation project(':packageb') 
} 

Paket B build.gradle

apply plugin: 'com.android.library' 
apply plugin: 'kotlin-android' 
apply plugin: 'kotlin-android-extensions' 
apply plugin: 'kotlin-kapt' 
android { 
    compileSdkVersion 26 
    buildToolsVersion "26.0.1" 


    defaultConfig { 
     minSdkVersion 15 
     targetSdkVersion 26 
     versionCode 1 
     versionName "1.0" 

     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 

    } 
    dataBinding{ 
     enabled true 
    } 
    buildTypes { 
     debug { 
      minifyEnabled false 
     } 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 

} 

dependencies { 
    kapt 'com.android.databinding:compiler:3.0.0-beta4' 
    implementation fileTree(dir: 'libs', include: ['*.jar']) 
    implementation 'com.android.support:appcompat-v7:26.0.2' 
    implementation 'com.android.support.constraint:constraint-layout:1.0.2' 
    testImplementation 'junit:junit:4.12' 
    androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" 
} 
+0

setzen Sie Gradle Code für Hilfe zu sehen. –

+0

@KuLdipPaTel: Ich habe Gradle-Dateien beider Module hinzugefügt. Vielen Dank. – jeytoe

+0

https://blog.mindorks.com/implementation-vs-api-in-gradel-3-0-494c817a6fa überprüfen Sie den Link –

Antwort

0

Nicht sicher, ob das Problem für Sie relevant ist, aber ich schaffte es eine Art der finden Lösung.

Um es Basisklasse generic sollte

class A<BINDING extends ViewDataBinding> { 
    protected ABinding binding; 

    void init(){ 
     binding = (ABinding) DataBindingUtil.setContentView(this, R.layout.a); 
    } 
} 

und übergeben die gleiche Bindung an das Kind Klasse vom Submodul

class B<ABinding> { 
    // you can use instance in this class 
} 

Das Hauptproblem hier, dass Sie Änderungen vornehmen arbeiten kann nicht drastisch die Benutzeroberfläche, nur vorhandene Elemente ausblenden oder neue zur Laufzeit hinzufügen. Aber ich nehme an, dass in einem solchen Fall eine völlig neue Klasse entsteht.