How to work with build.gradle file in Android Studio - android

After I make a change in the build.gradle file, what is my next step? I think there must be more steps I am missing…. compiling?? building ?? I am making these changes in Android Studio.
Not being that familiar with the build.gradle file, I am not sure what to do next. Any help would be most appreciated.
I want the change I made to the build.gradle file to have an effect on being able to install my project onto my Oculus Device. Right now I get the "Requires newer sdk version #29 (current version is #25).

after making a change in build.gradle, you have 2 different options, but they're the same thing :
you can click on the gradle sync button, then gradle will apply the recent changes you made or, as you can see in the blue text underneath, you can also make use of the Sync Now feature which will be displayed after any changes have been made.
Here's an example of the complete warning which is displayed after a change
after this, you can build/deploy your project or make changes in code as you need to, your dependencies have now been updated and will be available, assuming gradle syncs successfully

Related

How to embed an Android library (AAR file) into a capacitor plugin?

The issue is already answered for iOS:
How to embed framework in capacitor plugin for iOS
For Android, I get issues when I add an aar file to the capacitor plugin by creating a new module. After publishing my capacitor plugin via npm and integrating it into an ionic app, the aar library cannot be found anymore.
As done on iOs, we do practically the same thing here. But, using Android Studio and gradle instead of xcode and cocoapods.
Ok, but first things first.
When the command npx #capacitor/cli plugin:generate is issued, what the CLI does is to start a folder containing a cocoa pod for iOs and a gradle plugin for Android. You can see this on the image below.
With the above command issued, there will be a ios and android folders inside you generated plugin folder. If this checks, open you android studio and select the option to open a android studio project, selecting the android folder to fulfil this procedure. You can select this option on the initial screen, like showed on the next image.
With the project opened, if all went well you'll se your android studio showing a screen like this
Now, lets add the plugin files. To do this, right click the android java module and select the "Open Module settings" option, as shown on the next image.
With the following screen opened, we need to add a module here that contains our jar/aar dependencies. To do that, just click on the + sign that is showed above.
On the new module screen, if you scroll down there will be a "Import .JAR/.AAR package", like shown above.
After that, locate you aar package. The result will be the following.
Click finish and the next screen will appear. If not, hit the apply button in this same screen.
Since my module has a dependency, I'll repeat this part of the tutorial.
Next, we need to add our recently added modules as dependencies of the android module. On the same screen, click on the dependencies option and on the plus sign of the "declared dependencies" pane.
The following screen will show. We need to select the 3rd option, since what we did early was adding a module.
Now, lets select our module. Its simple, just mark it and click ok. Since I have to, as said before, I'll check both of them.
If this step went well, you should have now you android module with one more dependency (or two in my case). The image bellow show that.
Hit apply and close this window. Gradle will rebuild your plugin project on the background and after that,since I know kotlin better, I'll convert my project to kotlin. If you wish, this can be done as shown on the next image, but is not mandatory.
With all the above done correctly, you can now import your lib on the TestPlugin.kt (or .java if you didnt use kotlin). For reference, the image bellow show the minimum implementation for my plugin.
In the plugin folder, we are done. Pack it using npm and install on you app.
Next, to the plugin works properly, we need to add its class on the MainActivity of the app that you'll be using it.
Now, to make it work, i'm not 100% sure that this is the correct way to do it, but since it works I do this way. The way I do is to add the plugin modules on the android module of the app via script. I think i am missing something on the gradle plugin implementation, but since it works.....
To do this, access, on your app, the android/settings.gradle file and append this:
Please note that depending on the changes that you do, the settings.gradle file will be rewritten. Do a script to keep the info always there or check it before build.
Thats it, please comment if you know another way to this next step or in any another if there is a better approach. And, if this answer is useful, please thumbs it up, since this implementation, when finished and working, made me cry a little (of happiness, of course).

Gradle project sync failed after upgrade to Android Studio 2.2.2

Basic functionality (eg. editing,debugging)will not work properly. Error:Unexpected lock protocol found in lock file. Expected 3, found 0
I recently updated my android studio on stable channel and since then all my old project files have this issue when i open them
on the gradle console -> Messages
Error:Unexpeced lock protocol found in lock file . Expected 3,found 0.
Please help me resolve this issue
Finally found the fix :
You just need to go to File -> Project Structure or jus press Ctrl+Alt+Shift+S
and then on the top left click Project Make sure that your Android Plugin Version is the same as you Android Studio Version .. This should fix it
additionally
You can also create a new Project and check under Project view initially you'll be in Android .. change the view to ... Project - > gradle (Note .. not .gradle only gradle ) ->wrapper ->gradle-wrapper properties.. open this file and you can see the last line ie ..
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
make sure you have the same gradle version in your old files as well
Check out this Youtube Video i made regarding this issue
https://youtu.be/-Ngn4YvbKtw

How to set up Android Studio project from scratch that allows me to use groovy

Groovy's 2.4 release comes with official support for android app development http://docs.codehaus.org/display/GROOVY/Groovy+2.4+release+notes.
Having previously used eclipse for Android development, with no experience at all with Android Studio nor with Gradle, the existing instructions for setting up groovy android projects with Android Studio (e.g. http://docs.groovy-lang.org/docs/next/html/documentation/tools-groovyc.html#section-android, https://github.com/groovy/groovy-android-gradle-plugin, or http://hosain.net/2015/02/07/getting-started-with-android-development-using-groovy-2.4-and-android-studio.html) are too vague, do not work for me, and the difference in version numbers used by them is confusing.
I'd like to see detailed step-by-step instructions that work for Android Studio 1.2.1.1 for Linux, and explain how to decide on the version numbers.
EDIT: In 2019, almost 4 years after the question and this answer, the method described below does not work anymore with the current Android Studio version (3.3.2). I could not find versions of the dependencies that still work, and Gradle complains about not being able to download some dependencies.
The fact that 4 years later there is still no reproducible setup guide by the Groovy maintainers is just one indicator that Groovy for Android has no future. We should have seen discussions on patterns regarding how Groovy's dynamic abilities simplify Android development long ago, but this has not happened. I'm going to try Kotlin next, which seems similar enough to Groovy on first glance, apart from the required type annotations, but most important: one does not have to fight so hard against Android Studio to use it.
The original answer follows, it was for a very specific Android Studio version, extended with guesses how to adapt versions of dependencies in future Android Studio versions. According to the comments, this has worked until Android Studio 3.0.1.
After learning by myself how to do it, this is the step-by-step description that I would have liked to read when I started. I have used information from a number of sources. None of these sources worked for me, and they differed in version numbers and where to insert things into build scripts. These instructions are the result of trying out various combinations and filling in some blanks.
From the welcome screen, select "Start a new Android Studio project".
Fill in the app name and a "company domain".
No changes necessary on this page.
This example uses the Blank Activity.
This example uses the name GroovyActivity for the main android screen. The new project now gets created. When android studio has finished creating the project, it shows two open files like this:
Ignore the "rendering problem" for now. It will be resolve itself later during compilation. Close the two open files by clicking on the crosses of their tabs.
Open this build.gradle (Module: app) file:
Insert the following code before the first line:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
Let's pause for a while to reflect about the version numbers. The lines
classpath 'com.android.tools.build:gradle:1.1.0'
and
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
contain version numbers. How do you know which version number to insert here? At the time you read these instructions, there might have been new releases and unless you have specific version requirements, you will want to use the latest version of whatever it is that is used here in the gradle script.
How can you learn about the latest available versions? It depends:
Note that the editor has highlighted com.android.tools.build:gradle:1.1.0 with a yellow background color. When you hover the mouse pointer over this text, a tooltip appears:
So it seems that android studio knows about the latest version for this thing and advises you to use it. Heed the advice and update this version number to what your version of android studio indicates.
However, no such tooltip appears for this gradle-groovy-android-plugin thing.
What is happening here, anyway? The way I understand it, these names and version numbers identify names of binary components that are used for your app or for building your app, but neither are they part of android studio, nor are you expected to find and install or compile them yourself. Instead, this build script specifies a repository location where these binary components can hopefully be downloaded from. The function jcenter() seems to return the location of the repository. Currently, this repository is bintray.com.
To learn about the latest version of this gradle groovy android plugin, visit bintray.com with your webbrowser and use its search function. Search for groovy-android-plugin. The beginnig of the result looks like this at the time I write this:
The sheer number of listed results is a bit discouraging. I hope they have some relevance sorting in there. Searching through the first page, I only see 2 relevant matches, and the latest version number is 0.3.6.
Having learned about the latest version numbers (at the time of this writing), the correct start of the gradle script is this code:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6'
}
}
Back to modifying the gradle script, insert the following code after the "apply plugin: 'com.android.application'" line:
apply plugin: 'groovyx.grooid.groovy-android'
Finally, near the end of the gradle script, insert a line
compile 'org.codehaus.groovy:groovy:2.4.1:grooid'
so that it looks like this:
Again, there is a version number here, you can find the latest version on bintray by searching for *grooid.jar (yes, with the star as first character). The result list spans two pages, on the second page I find that 2.4.3 is the latest version:
After changing the gradle file, gradle needs to be "sync"ed. The gradle will sync if you exit android studio completely and restart it, so this is what I've done at this point. This picture shows the syncing after the restart:
Later I found that this icon in the toolbar
triggers the sync.
After the syncing finishes, change the left pane from the "Android" view to the "Project" view with the pull-down menu:
Now build and execute your project in the emulator at least once. I'm not sure what this changes, but if you do not build and execute your project now, while everything is still java sources only, then your groovy sources will not be found after we change to groovy. You can build and run your project using the run icon in the toolbar or with the keyboard shortcut shift-F10.
After executing your project in the emulator, turn back to the left pane, which still shows the "Project" view. In this view, navigate to the app/src/main directory, which currently contains the subdirectories java and res, and the AndroidManifest.xml file.
You will now create an additional "Java Folder" below main, with the help of the context menu:
Be sure to use the menu item New->Folder->Java Folder, and not New->Directory, which would cause problems later on.
In the next screen,
tick the check box "Change Folder Location" and make sure the name in the entry field is src/main/groovy.
If you still have that gradle script open, you will see that it has been adapted and contains the name of the new directory in a line starting with "sourceSets". For good measure, click on the gradle sync icon to make sure everything is properly synced.
Next you want to achieve that the new groovy folder contains the proper package path for the package name of your app. The only way that I have found to achieve this properly, is to create a new, temporary, java class with the context menu of the groovy folder,
and enter the name of this class fully qualified with the package name:
Make sure the spelling of the package name is correct!
The new package will appear in the groovy folder, and inside it, the temporary java class.
All groovy classes and also all java classes that make in some way use of groovy classes have to live below the src/main/groovy directory instead of the src/main/java directory. We can now drag the Activity class from the java directory tree to the same package below the groovy directory:
The temporary class can now be deleted (with the context menu), and the Activity class can be migrated from .java to .groovy by choosing Refactor->Rename File from the context menu.
The file extension .java is simply replaced with a .groovy extension. In the same way, you can later create new java classes and rename their file to .groovy when you actually want to create groovy classes.
You can now change some code in the activity to verify that you can actually have groovy code in an android app. Suggestion:
Give the "Hello World" text view an id in the res/layout/activity_groovy.xml file, like this:
Then, programmatically change the text shown by this view using groovy's string interpolation:
Build and execute in emulator:

How do I find Google Play version?

When I am setting up gradle, I have to add something like:
compile 'com.google.android.gms:play-services:6.1+'
I don't like to use the +, so I can always know which version the project is being compiled with and so I can avoid some surprise bugs.
In order to find the Google Play version I have currently downloaded, I usually go to the library XML file which I can find something like: "6171000", which is the version as an integer.
Is it there a simpler way to figure out the version that I need to write on gradle?
cd into your module (eg: app) dir, and run gradle command line ..\gradlew -q dependencies --configuration compile. This will resolve the + , and show you exact version being used for your library. See below.
Or, if you use Android Studio, when you modify/edit the version (eg: simply delete/add + at the end), Android Studio will show a yellow-bulb action with Replace with specific version, click on it to automatically replace the + with exact version available.
Go to this site: http://gradleplease.appspot.com/#play-services
You can also search for any library (Ex. recyclerview). This will help you specify the library's latest version.

Android Annotations with android studio

I've been trying to get Android Annotations working on Android Studio v0.8.14 along with Android Annotations 3.1. But for some reason it's not generating the required files. Can someone point me to the right documentation or steps I need to get a project set up.
I've looked around, nothing seems solid.
Create root and app build.gradle like:
https://gist.github.com/Pierry/b3eab411c8865573b985
In activity, annotate this with #EActivity(R.layout.activity_my), and add library in your classpath (Android studio make automatically), (Right click).
Add in your manifest _ after activity name.
Note: when u change everything in buidle.gradle, usually the line is removed:
compile "org.androidannotations:androidannotations-api:$AAVersion"
You need add it again.
Verify always.

Categories

Resources