Android with visual studio 2017 - build.gradle vs build.gradle.template - android

So I'm trying to build an Android app using Visual Studio. I started with a project created from the 'New Project' wizard ('Basic Application (Android, Gradle)'). I'm having trouble understanding how the build.gradle and build.gradle.template files relate to each other. The build.gradle file doesn't even show in the VS solution explorer until I enable 'show all files'. And when I make changes to build.gradle, they seem to be overwritten (sometimes?) upon build. Should I only edit build.gradle.template? What build step or tool processes this file to create the final build.gradle? Does the same apply to AndroidManifest.xml.template?
And what about gradle-wrapper - is gradle-wrapper.properties the same as gradle.properties? (I had to add the location of my JDK in there to get builds from the command line, outside VS, to work - so I just created my own gradle.properties but now I'm not sure if there are multiple parts doing the same thing, potentially stepping on each other).

The build.grade is generated by Visual Studio, from the build.gradle.template before it actually runs Gradle. This is true for the AndroidManifest.xml.template as well.
You should not edit the .gradle or .xml file itself and preferably not include it in your source control.
As for the wrapper, I can't really answer.

Related

How to build specific app APK with Android Studio when the project contains multiple apps?

I try to build and run the sample helloworld/mobile in project (https://github.com/android/car-samples)
My question is about how to select the sample app directory as described in README.md:
Select a sample app directory (e.g. helloworld/mobile) in the Project Structure UI on the top left. Each sample app has two build targets, one for the mobile platform and the other for the automotive platform.
<<<<
I went through Project Structure UI, I tried different possibilities but no way,
Build menu always shows Make Module car-simples-main and when I launch Make Module, nothing happens.
Wondering if something has changed in the last Android Studio version,
I use Android Studio Chipmunk | 2021.2.1 Patch 1
Anyone can help?
Thanks.
First of all, let's understand how the git directory is structured.
The directory has the two folders car-lib/CarGearViewerKotlin and car_app_library, which are gradle projects that can be built using gradle.
How do you know?
Any folder contains gradle wrapper and a build.gradle and settings.gradle files means that they're ready to be built using gradle, and these can contain modules inside of them.
Now helloworld/mobile is a module, or you can view it as a sub-project.
How do you know?
If you open up the parent's settings.gradle file, you will see the line include ':helloworld:mobile' meaning that the directory helloworld/mobile is a sub-project or module for the top level car_app_library project.
Modules by themselves are not runnable, they need to be contained in a parent project with top-level gradle files.
Now the solution for your problem should be: open up these two as projects in Android studio individually: car-lib/CarGearViewerKotlin and car_app_library, it will create a default run configuration for each project.
You won't be able to run helloworld/mobile alone, you'll need to run the parent project car-samples/car_app_library.
However, You should technically be able to open the root directory (the git directory containing all of the projects) and creating a run configurations by hand for each project, and you should be able to run the projects using one Android Studio window opened.

Linking an Android library that is on local machine

I have a set of utils and custom widgets that I want to pull out of my project as an Android library so that I can use them in other projects (and possibly share in the future). I created a new Android Studio project and changed the build.gradle file so that 'com.android.application' was 'com.android.library' and deleted the applicationId. It all compiles fine and I have a .aar file created.
I now want to use this new library as a module in my original project. When I do an Import Project, all the files from the library project are copied into my original project. But I don't want that because if I change the imported library code, it isn't reflected in the library project or vice versa.
I also tried adding this to settings.gradle:
include ':myutils'
project(':myutils').projectDir = new File(settingsDir, '../../../../Development/MyUtils/')
and in the original project app build.gradle:
dependencies {
implementation project(':myutils')
...
But I get this error:
ERROR: Unable to resolve dependency for ':app#debugUnitTest/compileClasspath': Could not resolve project :myutils.
Show Details
Affected Modules: app
How can I link the library to my project without importing it? I would prefer not to go through an external maven repo yet. I'm happy to (and would expect to) recompile my library whenever there is a change there and then rebuild my original project.
Thank you in advance.
I think I just had the same problem - I wanted to put the library .aar file somewhere on my local drive and then use it in a new app as a dependency. I didn't want to have to go through a repo or to include it in the libs folder in the new app. Hopefully that is what the OP asked and the following might be of help to others.
Searching on SO (Aug 2021), majority of answers seemed much more involved than what Android Studio offers (tested on version 4.2). That is, an .aar file that lives outside the app project can now be added as an implementation file in the gradle dependencies. So, it doesn't have to go through a repo, and it doesn't have to be included in the libs folder in the project.
The current documentation (Aug 2021) gives a fairly straightforward answer how to do it:
https://developer.android.com/studio/projects/android-library#psd-add-aar-jar-dependency
In short, if you have put your .aar file somewhere on your local drive, this is how to add it as an implementation file in another app project:
In Android Studio, in your new app project, go to: File > Project Structure > Dependencies.
In: Modules > app > Declared Dependencies, click '+' and select 'Jar Dependency'. (Even though you are trying to use an .aar file, you still select 'Jar Dependency').
In the 'Add Jar/Aar Dependency' popup dialog:
in step 1 enter the path to your .aar file on your local drive, and
in step 2 select 'implementation'.
If everything worked out, your build.gradle(Module) should have a line in the dependencies that looks like:
dependencies {
implementation files('../../../MyFolder/MyLibraryFile.aar')
A few notes:
You can actually just add the dependency manually, by typing it into the build.gradle(Module) dependencies, so you don't actually have to go through the Android Studio dialog outlined above.
you can either use a relative path (as the example above), or an absolute path.
the Android Studio dialog is somewhat limited in that you cannot just browse to your file (in point 3, step 1), but you have to actually enter the path manually.
Probably the most important: Whenever you make a change in the library and assemble a new .aar file, then remember to do the following in your app project that uses the .aar file as a dependency: Clean Project, then Sync Project with Gradle Files, and only then run the app, so that the changes in the library could take effect in your app.
I have been using Phgr's above technique for four years. I have three comments -
First, I don't clean the app project each time I change the library - I just do a Sync Project before building and testing the app.
Second, I changed the File-Settings-Keymap-Main Menu-File-Sync to Alt-S for easy of syncing - I hate wasting time using the mouse for selecting the Sync icon.
Third, I have an implementation line in the app's build module file for each app variant such as the following -
debugImplementation files('c:/Android Studio Projects/PciLibrary/app/build/outputs/aar/PciLibrary-debug.aar')
releaseImplementation files('c:/Android Studio Projects/PciLibrary/app/build/outputs/aar/PciLibrary-release.aar')
All of this is working fine with Android Studio 4.2.2 and sdk version 30.
Hope this helps others with this problem

.kt files not recognized in Android Studio

I opened my Studio today and Android Studio didn't recognize Kotlin files. .kt files started showing up as just an ordinary file. I checked my Plugins and I still have the Kotlin plugin. I tried uninstalling and installing it again. But no use.
I tried building my building my project from Terminal and it works fine. But Android studio doesn't understand Kotlin files.
Configuration:
Android Studio 3.1
Kotlin 1.2.41
OS: MacOS Sierra
Go to Library/Preferences and delete the AndroidStudio3.1 folder. This is the config directory for my Android Studio.
Once deleted, I started Studio again, and the config was created fresh. Now everything worked perfectly.
Update Kotlin plugin:
Go to
menu
preferences
plugins
find Kotlin plugin
update
I got this problem too, after upgrading my Android Studio from 3.1.4 to 3.2.1
And I have followed #Evgenii Vorobei step, found no "Update" button available just install and uninstall.
So I go to the root project build.gradle file update the kotlin_version from 1.3.11 to 1.3.21, sync project then restart Android studio
This is work for me
Update:
You also can update the plugin with Tools > Kotlin > Configure kotlin plugin update
I already had kotlin configured and could see the files in the "Project" view but could not see them in the "Android" view. When in "Android" mode, if you can see the File->new->Kotlin File/Class in the File Menu, create a new Kotlin file. When I did this, the new file appeared in the menu along with the other file I had copied in place.
I also had the similar issue, but in my case it was due to misc.xml file.
Android studio had added below line in misc.xml file
<component name="ProjectPlainTextFileTypeManager">
<file url="paht_to_file_not_being_recognized" />
</component>
after removing this line, everything worked perfectly. If you refactor -> rename file but then also the issue persist, this can be a possible reason.
I got the same problem when accidentally .kt file type association was moved to Text file from Kotlin.
To fix this remove *.kt from Text and add under Kotlin (if not present)
More info on it from jetbrains issue tracker
Lets say, I'm creating new file named HomeFragment, and AndroidStudio didn't recognize it as kotlin file. Fix Steps:
AndroidStudio -> Preferences -> Editor -> File Types
Click on File type auto-detected by file content, make sure this sections doesn't have prefill File name patterns - HomeFragment, if it does, delete it.
Click on Kotlin, make sure it has "*.kt"
Click on Text, make sure it doesnt have "*.kt"
once done, click Apply and OK

Module not specified after opening project from another computer

I keep my projects in my Dropbox folder. I installed Android Studio on my laptop and I tried to open the Project. All java files had a red circle which I managed to fix by marking the Directory as Sources Root. When I try to run the application, the Edit configuration window pops up with a sign at the bottom saying that Module is not specified, in red. There is no module I can select in the appropriate combobox. Syncing project with grandle files has no effect and importing the project instead of opening it makes things even worse. At the Projects side tab, by selecting the Android option, I can only see the Grandle Scripts structure and not the project files. The project files are visible when I select the Project option from the combobox.
The project cannot even open at the original computer anymore. I am new to Android and I have spent a lot of time developing this project. What can I do to reverse the situation?
You have to change some gradle properties.
First you open android studio and create a new project. Then change your old project's all gradle properties according to new project. like bellow--
Go to your build.gradle(Module:app) script and change the gradle version like compile 'com.android.support:appcompat-v7:25.3.1' to your current version.
Go to your build.gradle(Project:your app) script change this line classpath 'com.android.tools.build:gradle:2.3.0' to your current version.
Finally you have to change gradle-wrapper.properties(Gradle Version) distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip
to your current version
Now sync your project, I hope it will working fine.

Android studio: this project is not gradle based project

I have one project which was initially in eclipse, then i managed somehow to convert it to gradle and everything was working fine in Android Studio.
But then i didn't work some time with the project and after few months when i returned to project i am surprised that Android Studio has problems with this project.
When i selected "Android" from top left dropdown in AS, where you should see the project as Android, i saw no files, then i did something and now i see there one empty folder Java.
If i hit button "sync project with gradle files" AS says message as posted in name of question, but it is gradle based project.. there are gradle files etc.
Do you someone know why is this problem? Why it should stop work like this?
Thank you
Make sure you open the project from its root directory. i.e. the directory which contains the top level build.gradle file.
The missing top level gradle file is what causes this error.
For native Flutter Projects:
Note that this error will also appear if you "sync your project with Gradle files " in the root directory. Gradle works only in your android directory (flutter_name/android).
Configure Gradle version
I got this error because i wanted to change some build values like the Gradle version and the Gradle plugin version (and other build values ex. kotlin):
In your current project: File>Open
Select directory: user/StudioProjects/flutter_name/android
Open directory in a new window
After it is opened it will load some packages (see right bottom), which will take some time.
Under File>Project Structure Project you can set the gradle version and under Modules variables of your build
Now you can sync your project with Gradle files or at least read out the error messages, that come with your configuration
After configuration, you might need to restart your project with File>Invalid Caches/Restart....
That worked for me:
copy the file build.gradle
drop the copied file build.gradle in
your app/project folder .
My project stopped showing all of sudden and already had build.gradle file.
I just reopened it via File > Open.

Categories

Resources