32

I just created a new flutter project, added a few plugins and I'm getting the plugin switched to android x therefore i need to switch to android x. I've tried all the different ways of moving to android x and none has worked for me so far. Right now i don't even know what to do anymore, its so frustrating, why wouldn't flutter handle that when creating new projects automatically. Might be using ionic for the project.

marvin ralph
  • 1,100
  • 3
  • 23
  • 43
  • As of this comment date `flutter create` default to androidx. If you want you can specify command line argument with `flutter create` as `--androidx`. – Blasanka Sep 26 '19 at 07:24

9 Answers9

81

As you are creating a new project, just follow @harsh 's answer and you're done.

However, as I recently upgraded my existing app to use new plugins, I had to migrate to AndroidX as well... and the default procedure in the official instructions didn't work, which is using Android Studio to migrate the project - it said "No usages found!".

So what I did was:

  1. updated Android Studio to 3.3
  2. tried menu Refactor > Migrate to AndroidX
  3. got "No usages found" (if you manage to do it here instead, stop! you're done! ... actually, you can give this answer a try before continuing)
  4. opened android/gradle.properties and added
android.useAndroidX=true
android.enableJetifier=true
  1. opened android/build.gradle and changed

    • com.android.tools.build:gradle to version 3.3.0
    • com.google.gms:google-services to version 4.2.0
  2. opened android/app/build.gradle and

    • changed compileSdkVersion to 28
    • replaced android.support.test.runner.AndroidJUnitRunner to androidx.test.runner.AndroidJUnitRunner
    • replaced com.android.support.test:runner to androidx.test:runner:1.1.0
    • replaced com.android.support.test.espresso:espresso-core to androidx.test.espresso:espresso-core:3.1.0
    • in my case that was it, but here's the complete list of required replacements
  3. opened android/gradle/wrapper/gradle-wrapper.properties and changed distributionUrl to https\://services.gradle.org/distributions/gradle-4.10.2-all.zip (you might have to use 5.4.1, please see update below)

  4. executed flutter clean

And contrary to what I expected, IT WORKED! :)

UPDATE (2019 October 27th)

When updating my app to flutter v1.9.1+hotfix.5, as I use app signing, I was stuck with the error SigningConfig "release" is missing required property "storePassword" and in the end it was the gradle version from step 7 above. So now I'm using 5.4.1 as suggested in this wiki.

[android/gradle/wrapper/gradle-wrapper.properties]

distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
Feu
  • 5,372
  • 1
  • 31
  • 57
  • where exactly is `gradle.properties` located? is it the one in the root folder? `/Users/(user)/.gradle` or somewhere specific? – Malcolm Salvador Jun 27 '19 at 14:55
  • 2
    This worked for me as well. @MalcolmSalvador the gradle.properties referred to in instructions is located in android/gradle.properties – SamIAmHarris Jul 01 '19 at 20:50
  • @MalcolmSalvador C:\Users\username\AndroidStudioProjects\projectname\android\gradle.properties – Developer Sep 30 '19 at 05:46
  • 'com.android.support.test.espresso:espresso-core' I did not find this into my 'app/build.gradle' – Developer Sep 30 '19 at 05:54
  • I have done rest of the steps but I am still getting this error. https://stackoverflow.com/questions/58115011/unable-to-build-apk-firebase-admob-making-error/58152929#58152929 and I am unable to build apk. – Developer Sep 30 '19 at 05:55
  • 1
    After making these changes, I had to run "flutter clean" which fixed the app crashing on startup. – rolznz Oct 11 '19 at 05:08
  • It was working for flutter build apk, but for flutter build appbundle it's not working. Gives me an error that says that "The Gradle failure may have been because of AndroidX incompatibilities in this Flutter app." – atereshkov Oct 26 '19 at 12:27
  • The only thing that helped me is to set minifyEnabled from true to false. But disabling obfuscation is not a good idea. – atereshkov Oct 26 '19 at 12:47
  • you're a legend – TheDevWay Jan 09 '20 at 23:42
6

I've made the migration successfully by using Sam's answer!

1. Open the Flutter project into Android Studio
2. Right click on the android directory and 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 chek the Goolge Play Services.
  • what should one do if the 'Flutter' option (after right clicking on android directory) is not available /greyed out? What does that even mean? – Merv Merzoug Mar 18 '19 at 20:14
  • I believe it means that the Android Studio Flutter plugin isn't installed. Can you verify that? – Luan Almeida Mar 24 '19 at 21:06
  • https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects --> this helps – Llama Nov 18 '20 at 21:49
5

Add this to the end of your pubspec.yaml file, without any parent:

module:
  androidX: true

Then, at android/graddle.properties add:

android.useAndroidX=true
android.enableJetifier=true

Run at your CLI:

flutter clean

and rerun your code

flutter run

This may adjust your problem with AndroidX.

For your SDK, i think you may change inside android/app/build.gradle

minSdkVersion 21
targetSdkVersion 28
multiDexEnabled true

the multiDexEnabled is optional (in case of some error related to it).

I think this may solve your problem, please give me a feedback about it.

Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Renaud
  • 51
  • 1
  • 2
4

After a lot of tries, I've just created a new Flutter project and just copy past the pubspec.yaml, lib and assets folder from the old project to the new one, and the problem solved

Robert Apikyan
  • 1,972
  • 1
  • 20
  • 23
3

I Faced this problem recently. After looking for many hours I came across this medium page https://medium.com/@swhussain110/how-to-migrate-your-flutter-app-to-androidx-9a78eaaa924b

Follow these steps

  1. In project level build.gradle change classpath to com.android.tools.build:gradle:3.3.1.
  2. In app level build.gradle change your compileSdkVersion and targetSdkVersion to 28.
  3. Now right click on android directory in your flutter project go to Flutter and click on Open Android module in Android Studio. Open project in a new window.
  4. Now go to Refactor in the toolbar and click Migrate to AndroidX.
  5. Then click Do Refactor and wait for gradle to build.
A Vyom
  • 105
  • 1
  • 8
1

When you update your Android Studio version from 3.2 to 3.3. Android Studio version 3.3 onwards you can either use either android libraries or androidx libraries but

In flutter platform, flutter will always take the newest configuration of android studio.

This is the main reason why users get an error to use dependency according to androidx.

To migrate the flutter project to AndroidX, check this link: https://flutter.dev/docs/development/packages-and-plugins/androidx-compatibility

Feu
  • 5,372
  • 1
  • 31
  • 57
harsh
  • 337
  • 1
  • 2
  • 14
1

I faced the similar problem.

This Medium article helped me to find 75% solution for my problem

I followed the steps mentioned in this article. and I'm stuck in the end

Android studio is not allowed me to open android module (folder) from the project structure directly. So I opend manually in the new window of android studio.

Android is not refactored the gradle testRunner classpath androidTestImplementation it is prompting like is dependent So then I manually replaced all the paths like.

  • replaced android.support.test.runner.AndroidJUnitRunner to androidx.test.runner.AndroidJUnitRunner
  • replaced com.android.support.test:runner to androidx.test:runner:1.1.0
  • replaced com.android.support.test.espresso:espresso-core to androidx.test.espresso:espresso-core:3.1.0

and at the end I added to android/gradle.properties

android.useAndroidX=true
android.enableJetifier=true

Then ran the application. ;) after that androidX warning disappeared.

Rajath
  • 2,611
  • 8
  • 27
  • 43
0

it was still not working after I migrated to androidx

I simply install google play service on my sdk and it worked

kojo justine
  • 103
  • 1
  • 12
0

Faced with the same issue.

In my build.gradle compileSdkVersion and targetSdkVersion were already set to 28. minSdkVersion was set to 23.

Had already added android.useAndroidX=true android.enableJetifier=true to my android/graddle.properties.

After adding all these still migrating to AndroidX was not possible. When doing so a message pop up saying "No usage found in this project"

Adding the following code to my app/build.gradle solved the issue.

android {

defaultConfig {
    ...
      minSdkVersion 23 
      targetSdkVersion 28
      multiDexEnabled true
}

}

dependencies {

implementation 'com.android.support:multidex:1.0.3'

}