android studio: how to regenerate build.gradle - android-studio

Is there way to have android studio automatically rebuild build.gradle?
I have a case where the compileSdkVersion is wrong (says 18, should be 22). I changed it in the AndroidManifest.xml file but that change did not propagate to the build file. I tried build-clean project, build-rebuild project to no avail.
Obviously I can change it by hand but I would prefer to see a solution that is more automatic and less prone to errors I might unwittingly introduce.

From the official Android page: http://developer.android.com/tools/building/configuring-gradle.html
The defaultConfig element configures core settings and entries in the
manifest file (AndroidManifest.xml) dynamically from the build system.
The values in defaultConfig override those in the manifest file. The
configuration specified in the defaultConfig element applies to all
build variants, unless the configuration for a build variant overrides
some of these values.
You should provide sdk version only in gradle.build file once. If you provide any sdk settings in AndroidManifest.xml it will be overridden by build system with the values in your gradle.build file.
If you create a new project in your latest Android Studio you will notice that Android doesn't put any API version related settings in manifest file anymore. They all go to gradle.build file.

Related

How to get Android Studio to recognize my plain Gradle Java project?

I have a simple multi-module Gradle project with Java code. When I import it in IntelliJ Community, it asks me to select the build tool from among Maven and Gradle, and will properly recognize the Gradle modules and show me a Gradle tool window.
When I do the same in Android Studio, I don't get the build tool question, and it won't recognize the projects or show me the Gradle tool window.
How do I get this to work in Android Studio?
Simply Try This,
Go to File -> Project Structure -> Sdk Location
This You will get a option menu like this (the given image)
there you can update your gradle settings.
The answer, as far as I've been able to find out, is: Don't even try. Use IntelliJ instead. Installing it is most certainly quicker than trying to get Android Studio to do this.
How do you open the project in Intellij? Do you open the folder? Or do you open the build.gradle?
Just like a maven project (where you should open the pom.xml, not the folder) you should open the project using the build.gradle, not the folder.
Intellij will then do what you expect and import your gradle project
The least required to make it recognize the project is to add AGP, the Android Gradle Plugin:
buildscript {
repositories {
google()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.2"
}
}
Then one can define Android modules with:
apply plugin: "com.android.application"
apply plugin: "com.android.library"
apply plugin: "com.android.dynamic-feature"
Then it will recognize the android {} configuration block (without this Gradle DSL configuration block it would not know what or how to build for Android - as it isn't a sane Android project). Just see the documentation linked, this has little to do with the default Java tooling.
Gradle 7.2 is currently required to build. Even if one cannot mix the plugins per module, one can have different plugins per module - or use different IDE, depending on the tooling of the module. How compatible this really is, is being determined by the dependencies used in the Java module. The fact that one can only use either tooling per single module dictates the layout to be applied.
I suspect that you will have to manually move some code from the successful build files in intellij to the android build files.

What is CompositeDefinitionSource in Android Studio

Recently after upgrading Gradle Android Studio automatically added this to my .idea/gradle.xml :
<compositeConfiguration>
<compositeBuild compositeDefinitionSource="SCRIPT" />
</compositeConfiguration>
What is the purpose of this change?
First there is several points to understand:
The file .idea/gradle.xml is used by Android Studio to store the Gradle project settings.
According to the Gradle documentation:
A composite build is simply a build that includes other builds.
According to the documentation of IntelliJ IDEA on which Android Studio is based, there is two ways to define a Gradle composite build either through the IDE or through the settings.gradle file.
According to the source code of IntelliJ IDEA the value of compositeDefinitionSource can be either SCRIPT or IDE.
To answer your question, the purpose of the compositeConfiguration element in the .idea/gradle.xml file is to define as a default the Gradle composite build by script. If you set a Gradle composite build through the IDE this value will change.

Unable to migrate Flutter project to AndroidX

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.
Is there anything else I need to do?
I was able to resolve my issue:
Open the Flutter project into Android Studio
Right click on the android directory, click on Flutter -> Open Android module in Android Studio. It asked me to upgrade my gradle plugin.
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.
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.
Go to "Project Structure" - (2nd icon on left of AVD Manager icon on top right corner of IDE)
On "Project Structure" dialog under left navigation click on Project Settings->Modules
After selecting Modules, on the right pane you should see 3 tabs Sources, Paths, Dependencies
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
5. Click OK
Now Refactor->Migrate to AndroidX worked for me
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
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?
Click on Invalidate cache/Restart from file option worked for me
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!
Go to "Project structure" in the Files menu. Clear out every issue you see there, like
set the android sdk, fix the problems specified
A simple solution.
follow the steps.
Create a new app --> (tick androidX in the window) --> finish
Add some lines in the android --> app --> build.gradle as follow,
update the defaultConfig
minSdkVersion 19
targetSdkVersion 28
multiDexEnabled true
Go to Refractor --> migrateToAndroidx
Do Flutter clean
Restart
Run
hope it works now ...

Android Studio Failed to find target with hash string 'android-26'

I've installed latest Android Studio and
- I've created new project for sdk 25
- I have sdk 25 installed
I have this issues:
first of all, I don't see Tools > Android > AVD Manager. So I have no idea how can I set up additional AVD
second: I get this error in console
Error:Failed to find target with hash string 'android-26' in: C:\Android
Install missing platform(s) and sync project
Why is it complaining about version 26? I have project with api 25 set up?
This problem seem to emerge because AndroidStudio tries to target the last version of SDK it aware of. There few ways to solve it and #HarryFromMarydelDE Jan 7 at 20:18 presented nice solution in form of youtube video. I'll make this answer verbose and more detailed:
One way to deal it is to install sdk-26. You really may not have Android->AVD Manager menu items under Tools menu. You can find SDK Manager icon just under the Android Studio main menu, or open File->Settings (Ctrl+Alt+S) and go to Appearance&Behavior->System Settings->Android SDK
Other way is to find gradle.build file in YourAppName/app folder, find lines: compileSdkVersion 26 and targetSdkVersion 26 in it and change number into SDKs you already have.
But, if you'll change target sdk from 26 to 25, you may encounter more errors. Particularly, if you checked Back Compatibility box when you created your app in wizard. In this case you'll need to change 'com.android.support:appcompat-v7:26.1.0' string to use sdk 25 version of appcompat-v7:25.1.0 in the same file.
After this, you may encounter one more error:
Error:Execution failed for task ':app:preDebugAndroidTestBuild'.
Conflict with dependency 'com.android.support:support-annotations' in project ':app'. Resolved versions for app (25.1.0) and test app (27.1.1) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.
To resolve it, edit app.iml in YourAppName/app folder: change 27.1.1 numbers to 25.1.0 and hopefully, Gradle will finally build your project.

How to set an app as also a Library in Android Studio?

I've an app X that is also library for other apps. Its default state is as a Library. When I want to compile X as an app, I change that in Eclipse using Project > Properties > Android, and (un)clicking in Is Library.
How can I do this in Android Studio without editing the X build.gradle file line
apply plugin: 'com.android.library'
since this is not convenient and gives tons of errors for the other apps?
Thanks!
L.
Afaik this is the only way. The solution to giving tons of errors by other apps is to export the project as an .aar library, put it into libs folder of other projects and import it via build.gradle.

Resources