API 'variant.getJavaCompiler()' has been replaced with 'variant.getJavaCompileProvider'

随着AndroidX的迁移,gradle升级,Gradle难免有一些API已经过时。
我的环境
Android Studio 3.5.1
gradle 5.4.1
gradle build-tools 3.5.3
aspectJ 1.8.13

//之前可能是这样的
android.applicationVariants.all { variant ->
    JavaCompile javaCompile = variant.getJavaCompiler()
      javaCompile.doLast {

      }
    }
//----------------------------------------------------------------------------------------
//现在直接替换掉API getJavaCompiler --> getJavaCompileProvider
// 返回值为 TaskProvider,直接放进去一个Action即可
  android.applicationVariants.all { variant ->
    TaskProvider<JavaCompile> taskProvider = variant.getJavaCompileProvider()
    taskProvider.configure { javaCompile ->
      javaCompile.doLast {
        
        }
      }
    }

BaseVariant

 /**
     * Returns the Java Compilation task
     *
     * @deprecated Use {@link #getJavaCompileProvider()}
     */
    @NonNull
    @Deprecated
    JavaCompile getJavaCompile();

    /**
     * Returns the {@link TaskProvider} for the Java Compilation task
     *
     * <p>Prefer this to {@link #getJavaCompile()} as it triggers eager configuration of the task.
     */
    @NonNull
    TaskProvider<JavaCompile> getJavaCompileProvider();

TaskProvider

**
 * Providers a task of the given type.
 *
 * @param <T> Task type
 * @since 4.8
 */
public interface TaskProvider<T extends Task> extends NamedDomainObjectProvider<T> {
    /**
     * Configures the task with the given action. Actions are run in the order added.
     *
     * @param action A {@link Action} that can configure the task when required.
     * @since 4.8
     */
    void configure(Action<? super T> action);

    /**
     * The task name referenced by this provider.
     * <p>
     * Must be constant for the life of the object.
     *
     * @return The task name. Never null.
     * @since 4.9
     */
    String getName();
}

唉。发现好多文章都是说降级降级降级,能解决问题吗?还不如点点看看源码。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容