0

I am trying to use DataBinding in my Android project where I code in Kotlin. First of all, I'm use DataBindingUtil.setContentView(layoutResId) method, it can run. Then I try to encapsulate DataBinding to BaseActivity, gradle build failed, I don't know why. Here is my code.

BaseActivity.kt

class BaseActivity<B : ViewDataBinding>
    : AppCompatActivity(){

    protected lateinit var mContext: AppCompatActivity

    protected lateinit var mBinding: B

    override fun onCreate(savedInstanceState: Bundle?) {
        // Dagger2 注入
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)

        // 保存当前 Context 对象
        mContext = this

        // 添加到 AppManager 应用管理
        AppManager.INSTANCE.addActivity(this)

    }

    override fun onDestroy() {

        // 从应用管理移除当前 Activity 对象
        AppManager.INSTANCE.removeActivity(this)

        super.onDestroy()
    }

    override fun setContentView(layoutResID: Int) {

        // 加载布局,初始化 DataBinding
        mBinding = DataBindingUtil.inflate(
                LayoutInflater.from(mContext),
                layoutResID, null, false
        )

        super.setContentView(mBinding.root)
    }

}

MainActivity.kt

class MainActivity : BaseActivity<ActivityMainBinding>() {    

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Messages Gradle Build

Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

idea.log

2017-10-17 13:10:33,992 [se-915-b01]   INFO - ild.invoker.GradleBuildInvoker - About to execute Gradle tasks: [:SwipToLoad:generateDebugSources, :SwipToLoad:generateDebugAndroidTestSources, :SwipToLoad:mockableAndroidJar, :SwipToLoad:compileDebugAndroidTestSources, :SwipToLoad:compileDebugUnitTestSources, :SwipToLoad:compileDebugSources, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources, :app:compileDebugSources] 
2017-10-17 13:10:33,998 [thread 138]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from C:/Program Files/Java/jdk1.8.0_101 
2017-10-17 13:10:34,086 [thread 138]   INFO - oject.common.GradleInitScripts - init script file sync.local.repo contents "allprojects {\n  buildscript {\n    repositories {\n      maven { url 'D:\\\\android-studio3.0\\\\gradle\\\\m2repository'}\n    }\n  }\n  repositories {\n      maven { url 'D:\\\\android-studio3.0\\\\gradle\\\\m2repository'}\n  }\n}\n" 
2017-10-17 13:10:34,087 [thread 138]   INFO - ild.invoker.GradleBuildInvoker - Build command line options: [--configure-on-demand, -Pandroid.injected.invoked.from.ide=true, --init-script, C:\Users\Administrator\AppData\Local\Temp\sync.local.repo43033.gradle] 
2017-10-17 13:10:34,087 [thread 138]   INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: --configure-on-demand -Pandroid.injected.invoked.from.ide=true --init-script C:\Users\Administrator\AppData\Local\Temp\sync.local.repo43033.gradle 
2017-10-17 13:10:35,800 [se-915-b01]   INFO - ild.invoker.GradleBuildInvoker - Gradle build finished with 1 error(s) in 1s 783ms 
2017-10-17 13:10:35,835 [se-915-b01]   INFO - pl.ProjectRootManagerComponent - project roots have changed 
2017-10-17 13:10:35,860 [thread 138]   INFO - .diagnostic.PerformanceWatcher - Pushing properties took 1ms; general responsiveness: ok; EDT responsiveness: ok 
2017-10-17 13:10:35,910 [thread 138]   INFO - .diagnostic.PerformanceWatcher - Indexable file iteration took 50ms; general responsiveness: ok; EDT responsiveness: ok 
2017-10-17 13:10:36,405 [J pool 2/4]   WARN - hes.resolve.KotlinCacheService - Could not find correct module information.
Reason: Analyzing element of type class com.android.tools.idea.databinding.LightGeneratedComponentClass with no containing file
Text:
null 
2017-10-17 13:10:36,405 [J pool 2/4]   WARN - .resolve.jvm.JvmAnalyzerFacade - Java referenced null from LibraryInfo(libraryName=library-1.3.1)
Referenced class was: JavaClassImpl: DATA binding component class

2017-10-17 13:10:36,405 [J pool 2/4]   WARN - hes.resolve.KotlinCacheService - Could not find correct module information.
Reason: Analyzing element of type class com.android.tools.idea.databinding.LightGeneratedComponentClass with no containing file
Text:
null 
2017-10-17 13:10:36,405 [J pool 2/4]   WARN - .resolve.jvm.JvmAnalyzerFacade - Java referenced null from LibraryInfo(libraryName=library-1.3.1)
Referenced class was: JavaClassImpl: DATA binding component class

I don't know how to solve this problem, all my code is in KotlinTest, thanks for your help!

W.J
  • 33
  • 1
  • 3
  • what's the content of `gradle console`? – pskink Oct 17 '17 at 05:49
  • Nothing, just Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. – W.J Oct 17 '17 at 05:51
  • no, its not `gradle console`, see https://developer.android.com/studio/run/index.html ^F `console` – pskink Oct 17 '17 at 05:51
  • here it is ... `错误: 无法访问NonExistentClass 找不到error.NonExistentClass的类文件 有关详细信息, 请参阅以下堆栈跟踪。 com.sun.tools.javac.code.Symbol$CompletionFailure: 找不到error.NonExistentClass的类文件 1 个错误 :app:compileDebugJavaWithJavac FAILED FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. * Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. * Get more help at https://help.gradle.org` – W.J Oct 17 '17 at 06:00
  • and whats that chinise letters in it? sorry i cannot read mandarin – pskink Oct 17 '17 at 06:02
  • unable to access NonExistentClass. Can't find error.NonExistentClass class file. – W.J Oct 17 '17 at 06:05
  • I konw what is going wrong, I missed the apply plugin 'kotlin-kapt' in app.gradle, Anyway, thanks for your help! – W.J Oct 17 '17 at 07:21

1 Answers1

0

I had the same issue. If you use Dagger2, try not to use generics like <B: ViewDataBinding> as it can conflict with it. See my answer here.

b1n0m
  • 283
  • 3
  • 10