Gradle 使用自定义 XML 标记和数据绑定构建不成功

Unsuccessful Gradle build with custom XML tag and Databinding

当我尝试 运行 使用数据绑定和绑定布局中的自定义 XML 标记的项目时,我遇到了下面描述的构建错误:

布局:

<data>
   <variable
        name="data"
        type="com.melontech.sff.viewmodel.DataViewModel" />
</data> 

...

<ImageView 
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/placeholder_img"
   app:imageUrl="@{data.imageUrl}" />

...

视图模型具有以下绑定适配器:

@BindingAdapter("bind:imageUrl")
public static void getImageUrl(ImageView imageView, String url) {
    Timber.d("LOAD IMAGE WITH URL %s", url);
}

创建 ViewHolder 并进行绑定的代码如下:

@Override
public ModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ModelListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.model_list_item, parent, false);
    return new ModelViewHolder(binding);
}

构建错误:

Error:Execution failed for task ':app:transformJackWithJackForDebug'.

com.android.jack.ir.JNodeInternalError: java.lang.Exception: java.lang.RuntimeException: failure, see logs for details. cannot generate view binders java.lang.IllegalArgumentException: element public default java.util.stream.IntStream codePoints() is not a member of the containing type java.lang.String nor any of its superclasses at com.android.jack.eclipse.jdt.internal.compiler.apt.model.TypesImpl.asMemberOf(TypesImpl.java:129) at android.databinding.tool.reflection.annotation.AnnotationMethod.(AnnotationMethod.java:49) at android.databinding.tool.reflection.annotation.AnnotationClass.getDeclaredMethods(AnnotationClass.java:314) at android.databinding.tool.reflection.ModelClass.getAbstractMethods(ModelClass.java:401) at android.databinding.tool.expr.MethodBaseExpr.resolveListenersAsMethodReference(MethodBaseExpr.java:71) at android.databinding.tool.expr.FieldAccessExpr.resolveListeners(FieldAccessExpr.java:131) at android.databinding.tool.Binding.resolveListeners(Binding.java:65) at android.databinding.tool.BindingTarget.resolveListeners(BindingTarget.java:164) at android.databinding.tool.LayoutBinder.(LayoutBinder.java:250) at android.databinding.tool.DataBinder.(DataBinder.java:52) at android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:88) at android.databinding.tool.CompilerChef.sealModels(CompilerChef.java:187) at android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:184) at android.databinding.annotationprocessor.ProcessExpressions.onHandleStep(ProcessExpressions.java:86) at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.runStep(ProcessDataBinding.java:189) at android.databinding.annotationprocessor.ProcessDataBinding$ProcessingStep.access[=17=]0(ProcessDataBinding.java:174) at android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:79) at com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:139) at com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatcher.java:121) at com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159) at com.android.jack.eclipse.jdt.internal.compiler.Compiler.processAnnotations(Compiler.java:909) at com.android.jack.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:434) at com.android.jack.frontend.java.JAstBuilder.compile(JAstBuilder.java:269) at com.android.jack.frontend.java.JackBatchCompiler.performCompilation(JackBatchCompiler.java:219) at com.android.jack.eclipse.jdt.internal.compiler.batch.Main.compile(Main.java:1712) at com.android.jack.frontend.java.JackBatchCompiler.compile(JackBatchCompiler.java:184) at com.android.jack.Jack.buildSession(Jack.java:1052) at com.android.jack.Jack.run(Jack.java:540) at com.android.jack.api.v01.impl.Api01ConfigImpl$Api01CompilationTaskImpl.run(Api01ConfigImpl.java:124) at com.android.builder.core.JackToolchain.convertUsingJackApis(JackToolchain.java:310) at com.android.builder.core.JackToolchain.convertUsingApis(JackToolchain.java:189) at com.android.builder.core.JackToolchain.convert(JackToolchain.java:170) at com.android.build.gradle.internal.transforms.JackTransform.runJack(JackTransform.java:230) at com.android.build.gradle.internal.transforms.JackTransform.transform(JackTransform.java:198) at com.android.build.gradle.internal.pipeline.TransformTask.call(TransformTask.java:185) at com.android.build.gradle.internal.pipeline.TransformTask.call(TransformTask.java:181) at com.android.builder.profile.ThreadRecorder.record(ThreadRecorder.java:102) at com.android.build.gradle.internal.pipeline.TransformTask.transform(TransformTask.java:176) at sun.reflect.GeneratedMethodAccessor311.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:73) at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$IncrementalTaskAction.doExecute(DefaultTaskClassInfoStore.java:163) at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:134) at org.gradle.api.internal.project.taskfactory.DefaultTaskClassInfoStore$StandardTaskAction.execute(DefaultTaskClassInfoStore.java:123) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(ExecuteActionsTaskExecuter.java:95) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(ExecuteActionsTaskExecuter.java:76) at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:55) at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(SkipUpToDateTaskExecuter.java:62) at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:58) at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:88) at org.gradle.api.internal.tasks.execution.ResolveTaskArtifactStateTaskExecuter.execute(ResolveTaskArtifactStateTaskExecuter.java:46) at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:51) at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:54) at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(ExecuteAtMostOnceTaskExecuter.java:43) at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:34) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:236) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:228) at org.gradle.internal.Transformers.transform(Transformers.java:169) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:61) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:228) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter$EventFiringTaskWorker.execute(DefaultTaskGraphExecuter.java:215) at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(AbstractTaskPlanExecutor.java:77) at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.run(AbstractTaskPlanExecutor.java:58) at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(DefaultTaskPlanExecutor.java:32) at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(DefaultTaskGraphExecuter.java:113) at org.gradle.execution.SelectedTaskExecutionAction.execute(SelectedTaskExecutionAction.java:37) at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37) at org.gradle.execution.DefaultBuildExecuter.access[=17=]0(DefaultBuildExecuter.java:23) at org.gradle.execution.DefaultBuildExecuter.proceed(DefaultBuildExecuter.java:43) at org.gradle.execution.DryRunBuildExecutionAction.execute(DryRunBuildExecutionAction.java:32) at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:37) at org.gradle.execution.DefaultBuildExecuter.execute(DefaultBuildExecuter.java:30) at org.gradle.initialization.DefaultGradleLauncher.execute(DefaultGradleLauncher.java:196) at org.gradle.initialization.DefaultGradleLauncher.execute(DefaultGradleLauncher.java:193) at org.gradle.internal.Transformers.transform(Transformers.java:169) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:56) at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:193) at org.gradle.initialization.DefaultGradleLauncher.doBuild(DefaultGradleLauncher.java:119) at org.gradle.initialization.DefaultGradleLauncher.run(DefaultGradleLauncher.java:102) at org.gradle.launcher.exec.GradleBuildController.run(GradleBuildController.java:71) at org.gradle.tooling.internal.provider.runner.BuildModelActionRunner.run(BuildModelActionRunner.java:50) at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35) at org.gradle.tooling.internal.provider.runner.RunAsBuildOperationBuildActionRunner.execute(RunAsBuildOperationBuildActionRunner.java:43) at org.gradle.tooling.internal.provider.runner.RunAsBuildOperationBuildActionRunner.execute(RunAsBuildOperationBuildActionRunner.java:40) at org.gradle.internal.Transformers.transform(Transformers.java:169) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:106) at org.gradle.internal.progress.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:56) at org.gradle.tooling.internal.provider.runner.RunAsBuildOperationBuildActionRunner.run(RunAsBuildOperationBuildActionRunner.java:40) at org.gradle.tooling.internal.provider.runner.SubscribableBuildActionRunner.run(SubscribableBuildActionRunner.java:75) at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35) at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:41) at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:26) at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:75) at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:49) at org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:44) at org.gradle.tooling.internal.provider.ServicesSetupBuildActionExecuter.execute(ServicesSetupBuildActionExecuter.java:29) at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:67) at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:47) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.ForwardClientInput.call(ForwardClientInput.java:74) at org.gradle.launcher.daemon.server.exec.ForwardClientInput.call(ForwardClientInput.java:72) at org.gradle.util.Swapper.swap(Swapper.java:38) at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.LogAndCheckHealth.execute(LogAndCheckHealth.java:55) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:60) at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:72) at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36) at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:120) at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy.run(StartBuildOrRespondWithBusy.java:50) at org.gradle.launcher.daemon.server.DaemonStateCoordinator.run(DaemonStateCoordinator.java:297) at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54) at org.gradle.internal.concurrent.StoppableExecutorImpl.run(StoppableExecutorImpl.java:40) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at java.lang.Thread.run(Thread.java:745)

Gradle 文件中启用了数据绑定。

如果我将此功能移动到一个单独的干净项目中并像这样绑定数据:

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

自定义 XML 属性按预期工作并且构建成功。

您知道构建错误的原因吗?

试试这个方法...

@BindingAdapter("imageUrl")
public static void getImageUrl(ImageView imageView, String url) {
    Timber.d("LOAD IMAGE WITH URL %s", url);
}


<ImageView 
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/placeholder_img"
   tools:imageUrl="@{data.imageUrl}" />

Jd Prajapati 这是我的 ViewModel class:

public class DataViewModel {

   private Data data;

   public DataViewModel() {
   }

   public DataViewModel(Data data) {
       this.data = data;
   }

   public Data getData() {
       return data;
   }

   public void setData(Data data) {
       this.data = data;
   }

   public String getTitle() {
       if (data != null) {
           if (!TextUtils.isEmpty(data.getTitle())) {
               return data.getTitle();
           }
       }
       //Case when title doesn't exist
       return "";
   }

   public String getImageUrl() {
       if (data != null) {
           if (!TextUtils.isEmpty(data.getMainPic())) {
               return data.getMainPic();
           }
       }
       //Case when poster url doesn't exist
       return "";
   }

   @BindingAdapter("imageUrl")
   public static void getImageUrl(ImageView imageView, String url) {
        Timber.d("LOAD IMAGE WITH URL %s", url);
   }
}

顺便说一句,我找到了一种通过将 ImageView 属性 更改为以下内容来使 Gradle 构建项目的方法:

<ImageView
   android:id="@+id/movie_poster"
   android:layout_width="@dimen/dim_160dp"
   android:layout_height="@dimen/dim_240dp"
   android:src="@mipmap/movie_placeholder_img"
   app:imageUrl="@{singleMovie.getImageUrl()}" />

我遇到了和你一样的问题。 但是我找到了绑定的解决方案,而不是直接调用 getImageUrl。

<ImageView 
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/placeholder_img"
   app:imageUrl="@{data.imageUrl}" />
@BindingAdapter("bind:imageUrl")
public static void getImageUrl(ImageView imageView, Object url) {
    Timber.d("LOAD IMAGE WITH URL %s", (String) url);
}

使用对象而不是字符串。然后它会起作用。可能是绑定或插孔的错误。

我认为您正在尝试同时使用 Java 8 功能和 Data Binding。当您添加

 compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

Jack Compiler

jackOptions {
            enabled true
        }

对于您的项目,您遇到了自定义属性错误。

Google 说:

The Jack toolchain is deprecated, as per this announcement. If your project depends on Jack, you can keep using it. However, it may be removed completely in a future preview release of Android Studio 2.4. So we recommend disabling Jack and using Java 8 support built into Android Studio’s default toolchain.

官方文档如下: https://developer.android.com/studio/preview/features/java8-support.html

如果您只是将它用于 RetroLambda,您可以像下面那样迁移它而无需启用 Jack

buildscript {
  ...
   dependencies {
      // Remove the following dependency.
      classpath 'me.tatarka:gradle-retrolambda:<version_number>'
   }
}

并且在您的应用程序中 gradle:

apply plugin: 'me.tatarka.retrolambda'

还有第二种方式; - 正如 Seto 提到的那样 - 您可以像下面的代码一样更改您的 Binding Adapter

@BindingAdapter("imageUrl")
public static void getImageUrl(ImageView imageView, Object url) {
    Timber.d("LOAD IMAGE WITH URL %s", (String)url);
}

这也适用于您。祝你好运。