androidgradle

Migrate BuildConfig to Gradle Build Files


When I build in Android Studio, i get this message:

The option setting 'android.defaults.buildfeatures.buildconfig=true' is deprecated.
The current default is 'false'.
It will be removed in version 9.0 of the Android Gradle plugin.
You can resolve this warning in Android Studio via `Refactor` > `Migrate BuildConfig to Gradle Build Files`

but there is no such option in the Refactor menu

how can I start the Refactor?


Solution

  • I just set manually in the gradle.properties file

     android.defaults.buildfeatures.buildconfig=false
    

    And make the project again, If there are errors resolve them

    I got the error

    error: cannot find symbol
        int versionCode = BuildConfig.VERSION_CODE;
                          ^
    

    this is my code there:

    @Override
    public void draw(RenderManager renderManager) {
        // Get app version information
        int versionCode = BuildConfig.VERSION_CODE;
        String versionName = BuildConfig.VERSION_NAME;
    

    so I passed a context (activity) from The GameManager to the CreditsGameScreen, I changed the constructor:

    public CreditsGameScreen(GameManager gameManager, Context context) {
        super(gameManager);
        mContext = context;
    }
    

    and used this then:

    // Method to retrieve version code using PackageManager
    private int getVersionCode(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            String packageName = context.getPackageName();
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return -1; // Error occurred, return -1 or handle it accordingly
        }
    }
    
    // Method to retrieve version name using PackageManager
    private String getVersionName(Context context) {
        try {
            PackageManager packageManager = context.getPackageManager();
            String packageName = context.getPackageName();
            PackageInfo packageInfo = packageManager.getPackageInfo(packageName, 0);
            return packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return ""; // Error occurred, return empty string or handle it accordingly
        }
    }
    
    public void draw(RenderManager renderManager) {
        // Get app version information
        int versionCode = getVersionCode(mContext);
        String versionName = getVersionName(mContext);
        ...
    

    or shorter:

    public void draw(RenderManager renderManager) {
        // Get app version information
        try {
            PackageInfo packageInfo = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0);
            int versionCode = packageInfo.versionCode;
            String versionName = packageInfo.versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        ...
    }
    

    (The whole app can be seen in https://github.com/Eastcoast-Laboratories/Roboyard/tree/master/app/src/main/java/roboyard/eclabs )