1

I have an Android project that has been migrated to AndroidX. At some point, I want to add a new library. This library is using a support library with data binding.

I have enabled Android Jetifier in my gradle.properties. I am using Android Gradle build tool v.3.3.2 and Gradle v.4.10.1.

Here is my gradle.properties:

org.gradle.jvmargs=-Xmx1536m
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true

Here is my build.gradle:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.test"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    dataBinding {
        enabled = true
    }
}

configurations.all {
    resolutionStrategy.eachDependency { DependencyResolveDetails details ->
        def requested = details.requested
        if (requested.group == 'com.android.support') {
            if (!requested.name.startsWith("multidex")) {
                details.useVersion '28.0.0'
            }
        }

    }
}


dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation <library with AndroidX and data binding>
}

I got the following error on compile time.

Task :app:compileDebugJavaWithJavac FAILED
GallerypickerBinding.java:22: error: package android.support.constraint does not exist
    private final android.support.constraint.ConstraintLayout mboundView0;

GallerypickerBinding is the generated class from data binding of the newly added library. When I checked this file, it uses androidx.databinding.ViewDataBinding from AndroidX, but in the same file, it still uses android.support.constraint.ConstraintLayout from support library.

I expect Android Jetifier to convert all support libraries including to AndroidX, but it seems like it fails to convert the ConstraintLayout generated from data binding to AndroidX.

Jaymin
  • 2,879
  • 3
  • 19
  • 35
Chayanin Wong
  • 331
  • 2
  • 11
  • 1
    try androidx.support.constraint.ConstraintLayout instead android.support.constraint.ConstraintLayout while implementation 'androidx.constraintlayout:constraintlayout:1.1.2' – A Maharaja Apr 01 '19 at 06:08
  • @AMaharaja I already have ```implementation 'androidx.constraintlayout:constraintlayout:1.1.3'``` in my build.gradle. But I cannot just change android.support.constraint.ConstraintLayout to androidx in GallerypickerBinding.java since this file is auto generated from data binding. – Chayanin Wong Apr 01 '19 at 06:20
  • @cynw how does the `Gallerypicker.java` and it's XML resource look alike? – Martin Zeitler Apr 01 '19 at 06:36
  • @MartinZeitler gallerypicker.xml is a resource file in third party library. It has support.constraint.ConstraintLayout as its root viewgroup. GallerypickerBinding is a generated file from databinding used in that library. – Chayanin Wong Apr 02 '19 at 06:37

2 Answers2

0

You have to change package name in java file as well as in xml file.

com.android.support.constraint to androidx.constraintlayout
  • Unfortunately, com.android.support.constraint is used in third party library not in my project. I can only see the generated files from databinding. – Chayanin Wong Apr 01 '19 at 06:31
  • **com.android.support.constraint** is not third party library. After migrating project into androidx, com.android.support.constraint this package is change to androidx.constraintlayout name. migrating your project is only migrate your library that used, not change the package name that use in existing project. So you manuaaly change the import library in java and xml file –  Apr 01 '19 at 06:38
  • @cynw as Suraj Singh said you will need to replace the lines "import com.android.support.constraint" by "import androidx.constraintlayout" in your java, kotlin files. And also in XML layout files replace with "androidx.constraintlayout.widget.ConstraintLayout". – MD Naseem Ashraf Apr 01 '19 at 11:32
0

For Kotlin, replace this dependency:

implementation "androidx.appcompat:appcompat:1.0.0-beta01"

with these (not sure if the second one is even required, but it is quite essential):

implementation "androidx.appcompat:appcompat:1.0.2"
implementation "androidx.core:core-ktx:1.0.1"

And for that data-binding error... either clean the project - or try to add the old one com.android.support.constraint package into the dependencies once, to make it stop complaining (only for a test, it will get it's namespace rewritten). If this not helps, please add Gallerypicker.java and it's XML into the question, for further review.

@Suraj Singh might be right about the resource - if so, his answer should be the accepted one.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • thanks. I have replaced the dependencies as you said. I also tried adding `support.constraint` to my dependencies but it still keep complaining `support.constraint` does not exist in the databinding generated files. – Chayanin Wong Apr 02 '19 at 07:03
  • gallerypicker.xml is a XML resource file from 3rd party library. It has `support.constraint.Constraint.ConstraintLayout` as its root widget. And, `GallerypickerBinding.java` is a generated file from databinding of this XML file. – Chayanin Wong Apr 02 '19 at 07:08
  • @cynw why you keep referring to a third party library... instead of simply adding [that library](https://github.com/tizisdeepan/gallerypicker) as library module to the project and refactor to `androidx`? this should be straight forward. – Martin Zeitler Apr 02 '19 at 11:31