133

I have updated my Android Studio and the targetSdkVersion (build.gradle) to 29. I migrated to AndroidX. Now the Gradle build gives me these two errors:

This project uses AndroidX dependencies, but the 'android.useAndroidX' property is not enabled. Set this property to true in the gradle.properties file and retry.

The following AndroidX dependencies are detected: androidx.versionedparcelable:versionedparcelable:1.0.0, androidx.fragment:fragment:1.0.0, androidx.slidingpanelayout:slidingpanelayout:1.0.0, androidx.core:core:1.0.0, androidx.customview:customview:1.0.0, androidx.swiperefreshlayout:swiperefreshlayout:1.0.0, androidx.interpolator:interpolator:1.0.0, androidx.loader:loader:1.0.0, androidx.drawerlayout:drawerlayout:1.0.0, androidx.viewpager:viewpager:1.0.0, androidx.collection:collection:1.0.0, androidx.localbroadcastmanager:localbroadcastmanager:1.0.0, androidx.lifecycle:lifecycle-common:2.0.0, androidx.arch.core:core-common:2.0.0, androidx.annotation:annotation:1.1.0, androidx.lifecycle:lifecycle-livedata:2.0.0, androidx.legacy:legacy-support-core-ui:1.0.0, androidx.lifecycle:lifecycle-viewmodel:2.0.0, androidx.lifecycle:lifecycle-livedata-core:2.0.0, androidx.arch.core:core-runtime:2.0.0, androidx.legacy:legacy-support-core-utils:1.0.0, androidx.documentfile:documentfile:1.0.0, androidx.cursoradapter:cursoradapter:1.0.0, androidx.lifecycle:lifecycle-runtime:2.0.0, androidx.coordinatorlayout:coordinatorlayout:1.0.0, androidx.asynclayoutinflater:asynclayoutinflater:1.0.0, androidx.print:print:1.0.0

My code is:

File MainActivity.java

package it.manuel.myapp;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.DownloadListener;
import android.webkit.URLUtil;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.InstanceIdResult;
import com.google.firebase.messaging.FirebaseMessaging;

public class MainActivity extends AppCompatActivity {

    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = (WebView) findViewById(R.id.webview);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl("http://google.com/");
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()){
            webView.goBack();
        } else {
            super.onBackPressed();
        }
    }
}

File build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.6.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.google.gms:google-services:4.2.0'
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

File build.gradle(:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "it.manuel.myapp"
        minSdkVersion 29
        targetSdkVersion 29
        versionCode 2
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
    }
    compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-core:17.2.3'
    implementation 'com.google.firebase:firebase-messaging:20.1.1'
    testImplementation 'junit:junit:4.13'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

apply plugin: 'com.google.gms.google-services'

File AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="it.manuel.myapp">

    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:usesCleartextTraffic="true"
        android:icon="@mipmap/ic_launcher"
        android:label="MyApp"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

Why doesn't it work any longer?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sklero Mc
  • 1,365
  • 2
  • 10
  • 9

1 Answers1

280

The Android Gradle plugin provides the following global flags that you can set in your gradle.properties file:

android.useAndroidX: When set to true, this flag indicates that you want to start using AndroidX from now on. If the flag is absent, Android Studio behaves as if the flag were set to false.

android.enableJetifier: When set to true, this flag indicates that you want to have tool support (from the Android Gradle plugin) to automatically convert existing third-party libraries as if they were written for AndroidX. If the flag is absent, Android Studio behaves as if the flag were set to false.

To enable jetifier, add those two lines to your gradle.properties file:

android.useAndroidX=true
android.enableJetifier=true
Community
  • 1
  • 1
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
  • 1
    Ok.. i have insert, now gine me Jackpot allarm :D
    error: cannot find symbol public class MainActivity extends AppCompatActivity { ^ symbol: class AppCompatActivity
    MailActivity give this error..
    – Sklero Mc Mar 03 '20 at 13:14
  • @SkleroMc you need to import import `androidx.appcompat.app.AppCompatActivity` – Waqar UlHaq Mar 03 '20 at 13:41
  • @WaqarUlHaq This androidx won't react differently than the android, right? – Maria Irudaya Regilan J Apr 15 '20 at 18:46
  • @MariaIrudayaRegilanJ androidx is different from android support library as it provides backwards-compatibility across Android releases. – Waqar UlHaq Apr 15 '20 at 20:08
  • 54
    Life would be so much easier if Android Studio would suggest solutions for these kinds of problems with actual usable answers like the two lines you provide! They seem to think a clue that leads to web searches is enough, when they know exactly what would be needed to solve the problem. Thanks for the solution they should have given me. – Brendon Whateley May 14 '20 at 16:50
  • Docs: https://developer.android.com/jetpack/androidx – maguri Jul 30 '20 at 13:28
  • The gradle.properties is it the global one or the project one? – ikmazameti Feb 27 '21 at 21:19
  • Hello, I user cordova for my mobile app development and I am currently facing this issue. whenever I set android.useAndroidX=false and android.enableJetifier=false to android.useAndroidX=true android.enableJetifier=true then I try running the command again ... the values changes back to default. Please what is the problem – DUG Mar 31 '21 at 09:58
  • If you are using Flutter, please check this: https://flutter.dev/docs/development/androidx-migration – Philipos D. Apr 04 '21 at 07:04
  • I'm having same trouble as @DUG ! the variables get back to the false even after I change them to true. – Hamid Araghi Jun 06 '21 at 05:24
  • 2
    I found this file should be modified for `android.useAndroidX=true android.enableJetifier=true` : project_folder\platforms\android\cordova\lib\config\GradlePropertiesParser.js – Hamid Araghi Jun 06 '21 at 06:52
  • @HamidAraghi In my case, it is a cordova related problem. Changing android.useAndroidX=true android.enableJetifier=true is not sufficient as it requires the project to rebuild every time you want to run the app. The Ideal solution is to install the cordova-androidx-adapter plugin – DUG Aug 04 '21 at 14:21