laim2003
laim2003

Reputation: 369

Migration to androidX missing in Android Studio

I am trying to migrate my project to androidX. The project i am trying to migrate is based on android support libraries AppCompat. In the Developer Documentation it says that you can migrate to AndroidX by doing the Following: Android Studio -> Refactor -> Migrate to AndroidX But when i am following these steps, i end up at Refactor, because i only have got the option Migrate to AppCompat. I am not sure what to do.

Additional Info:
Android Studio Version: 3.1.4(Stable Channel)
Android SDK Tools Version: 26.1.1

Thanks for any help!

Upvotes: 14

Views: 12054

Answers (5)

Mr.Dark
Mr.Dark

Reputation: 391

You can not find Refactor > Migrate to AndroidX option in latest version of android studio(now Ladybug). So follow below options.

  1. Add following lines in gradle.properties
android.useAndroidX=true   
android.enableJetifier=true

2.Make sure build.gradle(:app) had following lines if not, add it

android { 
      compileSdk 34
      compileOptions {
        sourceCompatibility JavaVersion.VERSION_17 (add compatible version to your project)
        targetCompatibility JavaVersion.VERSION_17
    }
    namespace 'your app's namespace' (Get it from manifest file like package="com.example.appname")
}
  1. if you set your app targeting android sdk 31 or greater then add android:exported="true" every activity in AndroidManifest.xml like below.

<activity android:name=".MainActivity"
android:exported="true" />

  1. Upgrade gradle dependency in build.gradle(:project)
dependencies {
        classpath 'com.android.tools.build:gradle:8.7.2' (set version until your project compatible)
    }
  1. Remove Legacy support libraries

Search com.android.support in your project mostly in import section of java/kotlin and xml files and remove them and rewrite that line as Android Studio suggessts you to same class with AndroidX which you need to add. Remove Android Support Dependency from build.gradle(:app) by replacing AndroidX dependancy which is compatible with that dependancy.Below i have provided common dependency for each project, just grab it as per your requirements.

implementation 'androidx.appcompat:appcompat:1.7.0'
implementation 'androidx.annotation:annotation:1.9.1'
implementation 'androidx.core:core-ktx:1.15.0'
androidTestImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.2.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.6.1'
androidTestImplementation 'androidx.test:core:1.6.1'
  1. Sync project > clean project > rebuild project and if this not works then perform invalid cache from File > Invalidate caches..> select all options and do Invalidate and restart

If still issue happens paste your questions in comment, I will try to resolve.

Upvotes: 0

lotosbin
lotosbin

Reputation: 689

Android Studio Hedgehog is the last version which has "Refactor to AndroidX" option

Upvotes: 4

CathyChen
CathyChen

Reputation: 1

If you have the same problem after you updated AndroidStudio to 3.2 or later, you can modify your ***.kts to ***.gradle. I solved my problem this way.

Upvotes: 0

Anbarasu Chinna
Anbarasu Chinna

Reputation: 1005

Switch to "Android" or "Project" tab in the file navigation window. And right click on your parent folder and go to refactor, there listed "Migrate to android" option. It is very simple. In case of any mistakes in migration we can switch back to appcompat version using "migrate to appcompat" under same menu.

Upvotes: 3

Tuby
Tuby

Reputation: 3253

"Refactor to AndroidX" option is available for AndroidStudio 3.2 stable and later. https://developer.android.com/studio/

Upvotes: 8

Related Questions