33

I have a Flutter project in Android Studio. I am planning to migrate to AndroidX. Whenever I do Refactor -> Migrate to AndroidX, Android Studio shows error message:

You need to have compileSdk set to at least 28 in your module build.gradle to migrate to AndroidX.

However I have already set the compileSdkVersion 28 in my app/gradle.build file.

enter image description here

Is there anything else I need to do?

DarkNeuron
  • 7,981
  • 2
  • 43
  • 48
Sam
  • 2,972
  • 6
  • 34
  • 62

8 Answers8

57

I was able to resolve my issue:

  1. Open the Flutter project into Android Studio
  2. Right click on the android directory, click on Flutter -> Open Android module in Android Studio. It asked me to upgrade my gradle plugin.
  3. Then I installed Google Play Services. Tools -> SDK Manager -> SDK Tools and check the Google Play Services.

After the Google Play Services Installed, I did Refactor -> Migrate to AndroidX. It started working.

Note: Do the refactor from the project window you opened in the step 2.

Alice Bru
  • 45
  • 8
Sam
  • 2,972
  • 6
  • 34
  • 62
  • 7
    I tried but my "Flutter" menu voice is disabled, and I do not know howto open the "Open Android" menu voice. – Giacomo M Feb 22 '19 at 10:18
  • @GiacomoMasseroniChiaro I'm having the same issue https://imgur.com/2C4EnIH Have you had any luck? It works on the iOS module – Niamh Mar 02 '19 at 09:14
  • @GiacomoMasseroniChiaro I've raised this as an issue in the Flutter repository if you wanted to follow https://github.com/flutter/flutter/issues/28791 – Niamh Mar 02 '19 at 23:13
  • Instead of ... "2. Right click on the android directory and Flutter -> Open Android module in Android Studio. It asked me to upgrade my gradle plugin." -- I used Tools -> Flutter -> Open Android Module... – Jerry May 02 '19 at 17:00
  • 3
    @Jerry I have no such entry in Tools -> Flutter – DarkNeuron Jun 14 '19 at 09:08
  • 3
    yes, it did the trick. Just one more thing to be added - need to wait sometimes to finish its pcroesses after everything has installed/upgrade. – Sithu Oct 08 '19 at 16:51
  • Just wanted to chime in and say that this initially *did not* work for me. It *did work* after updating my Flutter and Dart plugins and installed Google Play Services via the SDK Manager. Then I was able to right-click on the android dir and go to Flutter -> Open Android module in Android Studio. Once it opened, I had to *wait patiently* quite a while for all the processes to finish. Then it showed an "Update Gradle plugin" in the bottom right. Did that and was able to start the AndroidX migration by right clicking on the android directory. This also took quite a while. – shaun m Oct 26 '19 at 17:15
  • I did all steps without "upgrade gradle plugin" and it worked pretty well. That was important for me as the upgrade created version dependency errors and in my case the upgrade was not necessary. – Ahmed Hammad Dec 05 '22 at 11:59
8

Here is how I got rid of "compileSdkVersion 28" error. But before I solved it this way, I upgraded Android Studio IDE from 3.4.1 -> 3.5.1. I am not sure if this was really needed. So whatever version of IDE you have, see if you can use following steps to solve it.

  1. Go to "Project Structure" - (2nd icon on left of AVD Manager icon on top right corner of IDE)

  2. On "Project Structure" dialog under left navigation click on Project Settings->Modules

  3. After selecting Modules, on the right pane you should see 3 tabs Sources, Paths, Dependencies

  4. Click on Dependencies, if you are getting compileSdkVersion 28 error the SDK version under Dependencies is pointing to version lower than 28 -> Select 28 or higher

Project Structure -> Modules -> Dependencies 5. Click OK

  1. Now Refactor->Migrate to AndroidX worked for me
Mathieu J.
  • 1,932
  • 19
  • 29
smajithia
  • 81
  • 1
  • 2
4

In gradle.build (app) add this

 compileSdkVersion 28
    defaultConfig {
        ......
        minSdkVersion 21
        targetSdkVersion 28
        ......
    }

And there are some implementations required to use androidx :-

implementation 'androidx.appcompat:appcompat:1.1.0-alpha01'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'

And add these given two lines(below) to gradle.properties:-

android.useAndroidX=true
android.enableJetifier=true
DeePanShu
  • 1,236
  • 10
  • 23
0

I'm having exact same problem. Been trying to migrate since all the new firebase stuff is forcing it, but the current guides to upgrade are sparse. First I was trying on a backup of my project with a lot of addons and kept getting that error, then I created a brand new flutter project with all the latest (dev channel), followed every step, checked every detail, read every forum, but still getting "You need to have compileSdk set to at least 28 in your module build.gradle to migrate to AndroidX."... Matched the steps from https://androidxhackathon.blogspot.com/2018/05/refactor-to-android-x.html too and can't get it to refactor right. Even went to File>ProjectStructure>Project SDK and set to Android API 28 Platform. Anyone know what we might be missing? Did you have any luck Sam?

  • I am going to try what @Deepansu suggested. If it won't get any luck then I will uninstall Android studio and then try importing the project again. will update the post – Sam Jan 29 '19 at 15:15
  • Any luck? @Alan Bedian – Sam Feb 08 '19 at 13:43
  • Nope, spent hours trying to debug, and not sure if anybody has been able to run the Migrate to AndroidX feature. Once again, created clean barebone project, followed all the steps, compileSdkVersion and targetSdkVersion is 28, changed all the implements to the androidx equivalent, enabled the gradle.properties, and played with everything else I can think of, and still saying "You need to have compileSdk set to at least 28"... Running the latest Android Studio 3.3 (on Windows), Flutter 1.2.0, etc.. What else could we be missing? Has anybody out there gotten it to migrate correctly? – Alan Bedian Feb 10 '19 at 00:54
  • I also tried to uninstalling and reinstalling my Android Studio but stilling giving the same error... :-( – Sam Feb 10 '19 at 04:21
  • Alright, that was the trick to Open Android module in Android Studio, yay.. I would not have stumbled on that process myself, thanks. That resolution should be added to the official Migration to AndroidX guide. Now I'm going to risk it on my Flutter project with over 60 add-ons I'm using, slightly nervous but at least we figured this part out.... – Alan Bedian Feb 11 '19 at 06:17
  • 1
    @AlanBedian What do you mean "Open Android module in Android Studio"? Can you provide a little more detail so that I can try the same solution? Thanks! – Eric Duffett Mar 04 '19 at 12:30
  • I added a note for "Right click on the android directory and Flutter -> Open Android module in Android Studio. It asked me to upgrade my gradle plugin." Above. – Jerry May 02 '19 at 16:59
0

Click on Invalidate cache/Restart from file option worked for me

Manish Butola
  • 557
  • 6
  • 15
0

Try replacing the default in dependencies {...} in app/build.gradle with this:

dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

The replacement is similar to what is posted in the "Not recommended..." section from this link: https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility

If you can explain why this works, please add a comment. Thank you!

dkea
  • 690
  • 1
  • 9
  • 24
0

Go to "Project structure" in the Files menu. Clear out every issue you see there, like set the android sdk, fix the problems specified

shahana kareen
  • 364
  • 4
  • 10
0

A simple solution. follow the steps.

  1. Create a new app --> (tick androidX in the window) --> finish
  2. Add some lines in the android --> app --> build.gradle as follow, update the defaultConfig

    minSdkVersion 19
    targetSdkVersion 28
    multiDexEnabled true
    
  3. Go to Refractor --> migrateToAndroidx
  4. Do Flutter clean
  5. Restart
  6. Run

hope it works now ...

zakaria
  • 163
  • 1
  • 8