Android Annotations with android studio - android

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.

Related

How to work with build.gradle file in Android Studio

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

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).

SafeArgs generated classes (Directions/Args) are not recognized by lint in AS 3.5

I'm migrating several apps to use the Navigation Component, and I ran into a problem - the Directions/Args classes generated by the android-safe-args-gradle-plugin are not being recognized in Android Studio.
Code demonstrating this problem is located at github.com/wooldridgetm/android-navigation.git, but crux of the matter is, in the code below, HomeFragmentDirections isn't recognized. This is taken from the class HomeFragment, lines 76 - 82.
val b2 = view.findViewById<Button>(R.id.navigate_action_button)
b2.setOnClickListener {
// PROBLEM: HomeFragmentDirections isn't recognized.
val action = HomeFragmentDirections.nextAction()
// PROBLEM: results in Unresolved reference: flowStepNumber
// action.flowStepNumber = 1
findNavController().navigate(action)
}
I know what the documentation says & I know that class HomeFragmentDirections is being generated by the plugin & is located at app/build/generated/source/navigation-args/debug/code/example/android/codelabs/navigation, but it is not recognized by AS.
I also know that this did work pre-v2 of the plugin...but it's not working now & I can't even get a simple example to behave as expected.
Any ideas on what's wrong?
BACKGROUND
I'm using AS 3.5 on MacOS Mojave, 10.14.6.
Project build.gradle dependencies:
classpath 'com.android.tools.build:gradle:3.5.0'
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:2.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.50"
App build.gradle file:
apply plugin: 'androidx.navigation.safeargs.kotlin'
dependencies {
// Navigation
def nav_version = "2.1.0"
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}
The reason Android Studio/Lint wasn't "seeing" these classes was BIAD (b/c I'm a dummy).
I excluded the build folder in the Project Panel via Preferences > Editor > File Types > Ignore files and folders.
In any event, removing the build folder from Ignore files and folders did the trick.
Select the Project view in the project tab, so you can see the actual folders. You'll notice the app/build folder is oranged out, which means it's being excluded. Simply right click > Mark directory as > Cancel exclusion, then right click again > Mark directory as > Sources root. The folder will be blued out like the java folder and AS should recognize the generated classes.
Although I'm not sure I believe all the code you're gonna use is actually in app/build/generated, so you could be more specific marking that one as the sources root instead. So it should look something like this.
I had a similar issue with actions which were clearly added in the navigation graph not 'autocompleting' or showing an option to import in the kotlin class trying to access them, even with the correct import added manually.
For me the fix was simply to to rebuild the project and they automatically appeared then.
It was an issue in Navigation API, Apparently, They have fixed this issue in 2.5.0-rc02 version.
Please refer to this for more details.

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 import com.google.android.gms.* in Android Studio using a Gradle build?

I have followed the procedure as described here : Setup
I have clicked on the little 'Sync project with Gradle' button. Gradle and Android Studio seem to find everything but then I can't actually use the gms code. If I try to import, I will get autocomplete for com.google.android.gms but no further. I have updated all the packages with Android SDK Manager.
I'm running Android Studio 0.4.2.
My minSdk is set to 9
my build.gradle includes compile 'com.google.android.gms:play-services:4.0.30'
As far as the procedure is concerned I should be ready to code, but it just doesn't work. Any ideas?
[Edit, added info]
I can find the ComGoogleAndroiddGmsPlayServices3265.aar file in my exploded bundles directory. Inside of that file I also find the common directory and inside that I find the GooglePlayServicesUtil.class (which is what's not being found in my app)
I am lost.
[Edit 2]
The problem is not specific to Google Play Services OR Android Studio. I tried adding another library (HoloColorPicker) and had the same results. However, I was able to add the library's resources to my project! I was able to add them in my XML layouts and view them in my application. I was able to interact with them, they worked fine. The problem arose again when I tried to reference them in the code. Exactly like the case with gms, I had code completion when trying to import up to the point of the actual class, and I could not declare the class in the code.
I was able to use the library by cloning it and importing the project.
Also, this is not an Android Studio problem because the same thing happens on the command line with "./gradlew clean build"
This is a current bug in Android Studio: https://code.google.com/p/android/issues/detail?id=64508 to be fixed this week.
The workaround is to close the project, delete the .iml files and .idea project and re-import the project.
Keep Your compile 'com.google.android.gms:play-services:4.0.30' as very first line in build.gradle dependencies like
dependencies {
compile 'com.google.android.gms:play-services:4.0.30'
compile 'com.android.support:support-v4:18.0.+'
compile 'com.android.support:appcompat-v7:+'
}
`
Open File> Project Structure and do the following steps
Select your main module in which you want to add dependency and click on OK.
Now try to import.
I think the most important question is what you want to achieve. Not all code is under this package. Not even sure which one is.
As noted before, this is a bug with Android Studio. It just don't recognize the path for classes and shows you like if there is an error. If you try compiling you'll see that everything just work fine.
A googler recently said it'll be addressed in this week release, so, be patient and lets see whats coming.
The fact that AS is in Preview mode tell us this sort of things are going to happen :)
Android studio is crazy one, I think.
It's removed "Import module" function and you can do "New module" only.
If you are developed on Eclipse, you need export all your projects to Gradle before switch to Androids studio (WTF?)
I prefer "IntelliJ IDEA Community Edition", although It's similar Android studio but it's better than Android studio (at least until now). You just import your project as eclipse format, IntelliJ IDEA will detect dependences libraries automatic (May be you need import jar libraries by hand) and rebuild project. That Done.

Categories

Resources