Android Studio w / gradle: r package does not exist

Final Edit:

It was a long question with a few changes, and I did not always do the right thing at every step of the way. For those who stumble upon this question and have difficulty converting the project to gradle in Android Studio, I would suggest checking out this repository mentioned in question # 16718026 and trying to import it and compile and run it. Once something works for you, it’s easier for you to see what might be wrong with your own project.

In short, here is what I did to convert a project without Gradle Android Studio into a working gradle alone using Android Studio.2.5, which automatically installed gradle 1.6 and the gradle.5 plugin for me.

(1) Created a new empty Android Studio project with icon and activity. NOTE. At first I didn’t understand this, because I was learning Android and gradle at the same time, but Android Studio (in this version anyway) actually sets you up to a multi-project setup, which confuses me. Multi-Project means that there is another folder in the hierarchy above your project, which gives you a location to store settings for all projects. AS did not create the build.gradle file in the subproject folder (in my case FrontlineSMS), despite the fact that the video guide indicates this. Therefore, I had to create this build.gradle subproject file and also create very simple .gradle settings (see Update 2 ).

(2) Copied src /, res /,. Git /, lib /, .gitignore and AndroidManifest.xml to the root of the new project.

(3) Used by git mv to move these elements to the appropriate places in accordance with the conventions in the gradle plug-in User Guide, replacing or deleting some existing files or folders in an empty project if necessary.

(4) Created build.gradle in my subproject folder and gave it some basic settings (see below or in the question above)

(5) Imported the project as described in Update 4

The original question:

I am a novice Android developer trying to convert an Android Studio project that currently does not use gradle (and compiles and works fine) to one that uses gradle in OS X. I am stuck with a Resource Error, which I think may have relevant to my project structure, settings in the IDE, settings in the build.gradle files, or a combination thereof.

The way I did the conversion was to first create a new project in Android Studio, copy the files to the appropriate places, and then add some parameters to the IDE and build.gradle files. My resulting project structure,

 β”œβ”€β”€ frontlinesms-for-android β”œβ”€β”€ build.gradle β”œβ”€β”€ gradle β”œβ”€β”€ gradlew β”œβ”€β”€ gradle.bat β”œβ”€β”€ settings.gradle (added in update 2) β”œβ”€β”€ local.properties β”œβ”€β”€ lib └── lib1.jar └── lib2.jar └── ... β”œβ”€β”€ frontlinesms-for-android.iml β”œβ”€β”€ FrontlineSMS └── build.gradle └── src β”œβ”€β”€ main β”‚  └── res β”‚  └── AndroidManifest.xml β”‚  └── java β”‚  └── net β”‚  └── frontlinesms β”‚  └── android β”‚  β”œβ”€β”€ FrontlineSMS.java β”‚  β”œβ”€β”€ ... 

In particular, perhaps I set the res / folder to the same level as java / and AndroidManifest.xml. I am trying to comply with the conventions mentioned in the gradle plugin user guide, so I don’t need to configure build.gradle files too much.

I also marked the FrontlineSMS / src / main / java / net folder as the source, because if I do not, nothing will try to compile. I created a new build.gradle file in FrontlineSMS / build.gradle because it did not exist when I created a new project, although the tutorial video on the gradle website reported that it would be.

In the IDE, I also install dependencies for external libraries that I have in / lib.

When I try to compile through the IDE, I get a lot of errors Package r does not exist , indicating that the automatically generated class R is not generated or not recognized. This is defined in the project as

 import net.frontlinesms.android.R; 

in several files. What if Android Studio / gradle recognizes the resources that I defined in src / main / res?

While addressing the issues of others, I notice that some solutions to this error included marking the β€œgen /” folder as the source root or β€œcleanup” of the project. I tried creating a gene / folder in the root directory and marking it as a source, but that didn't work. And, restoring a project just gives me the same errors.

The root.gradle file contains the following:

 dependencies { compile project(':FrontlineSMS') } 

And the one under FrontlineSMS / build.gradle has

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.4' } } apply plugin: 'android' dependencies { compile file('../lib/acra-3.1.1.jar') compile file('../lib/activation.jar') compile file('../lib/additionnal.jar') compile file('../lib/annotations.jar') compile file('../lib/libGoogleAnalytics.jar') compile file('../lib/mail.jar') } android { compileSdkVersion 17 buildToolsVersion "17.0.0" defaultConfig { minSdkVersion 7 targetSdkVersion 16 } } 

When I try to build from the command line using ./gradlew clean , I get this error,

 (removed error) 

In addition to my previous question, I would appreciate any advice you can provide regarding how this project should be structured. This does not mean that this is the setting of several projects. Thank you for your help.

Update 1

I managed to create an APK from the command line. My main problem is that I was calling gradle clean from the wrong directory. Called him from FrontlineSMS. I assume that Android Studio set me up with several projects when I have only one project, and I did not enter all the settings for this, i.e. I did not create the settings.gradle file.

Update 2

Created the following settings.gradle file, which allows me to run gradle clean && gradle build from the root directory (where settings.gradle resides):

 include ':FrontlineSMS' 

Note: gradle version I am using here the one installed by homebrew, 1.6. If I use the installed gradlew shell, it says:

 Gradle version 1.6 is required. Current version is 1.7 

A web search for this error seems to indicate that Android does not support 1.7 at the moment, only 1.6 (and perhaps this is always a version or two behind), but this is the version installed by Android Studio when I created a new project!

Update 3 (edit: this is basically all wrong, do not follow this example)

Trying to create a project from Android Studio gives the same error as above with respect to 1.6. I tried to reimport the project, although he said that he could not find any source files. I assume this means: (1) I have the wrong version of gradle, and I need to fix it and maybe (2) I will need to identify the source files in the build.gradle file, although I thought I was following the instructions guides.

I tried copying gradle-wrapper-1.6.jar to replace gradle/wrapper/gradle-wrapper.jar , but it just caused a class or method error when I ran it through ./gradlew clean .

Attempted solution: in gradle/wrapper/gradle-wrapper.properties there is a line distributionUrl= , and I changed this to point to version 1.6.

 distributionUrl=http\://services.gradle.org/distributions/gradle-1.6-bin.zip 

Then the next time I run ./gradlew clean , it uploaded a new version of gradle to my ~/.gradle home directory (which still didn't exist), and now when I type ./gradlew --version it says that it is on 1.6, and builds everything through the command line using ./gradlew clean && ./gradlew build , resulting in apks. So this works from the command line, but I also want this to work from the IDE

The IDE now says

 Project is using an old version of the Android Gradle plug-in. The minimum supported version is 0.5.0 

So, I am updating the build.gradle file to say

 classpath 'com.android.tools.build:gradle:0.5' 

And again open the IDE that tells me

 Could not find com.android.tools.build:gradle:0.5. 

Are Android Studio gradle and gradle plugins connected? If so, what do I need, gradle.4 + gradle 1.6 or gradle.5 and gradle 1.7 plugin? And how to install them correctly?

Update 4

I finally got this to compile from both the command line and the IDE. The trick was to import the project using gradle after creating the base gradle files. Thanks Ethan! During the project import process, you can choose "Create a project from existing sources" or "Import a project from an external model." I chose the first, when I had to choose the last, and chose Gradle. After that, I selected "use gradle -wrapper (recommended)" and check "use auto-import" and click "Finish". He warned me about a few things that I had to change, for example, using the last gradle plugin, for which I made this change in my build.gradle file,

 classpath 'com.android.tools.build:gradle:0.5.+' 

which is suggested in known issues for the android gradle plugin.

+6
source share
3 answers

It looks like you are missing the settings.gradle file from the project root directory.

You will need the settings.gradle file to access the subproject that is inside your source repo.

When importing a project, be sure to select Gradle as the source!

+2
source

Actually, a couple of points:

Go to file β†’ Settings β†’ gradle and go to:

select Use local gradle distribution and go to:

C: \ Users \ .gradle / packer / dists / gradle -1,6-bin / 72srdo3a5eb3bic159kar72vok / gradle -1,6

This works, but you need to clear all previous materials by going to:

File -> Invalid Cache / Restart

Then you have to go back, it will be re-imported (may ask you to configure gradle again, if the above was not entirely correct, I think)

Now it should work ... you do not need to manually create build.gradle files.

the settings.gradle in the top-level project itself.

Finally, if the project structure does not show you much (mine only showed the platform settings and did not have all the other options in most manuals), you can right-click on your top-level project, for example, HelloWorldProject, and then select Module Settings, and this can lead you to where you need to.

Some screenshots for the above may be useful - they seem to change layouts, etc. and hide the material, so the official guidelines are not always correct:

Lame cannot send you images ...

+1
source

sample (with projects with several modules and several libraries).

http://tools.android.com/tech-docs/new-build-system/

scroll down.

about

 Could not find com.android.tools.build:gradle:0.5. 

this jug is not in the center of maven, it comes with Android Studio 0.2. + This usually happens if you upgrade Android Studio to version 0.2, which does not have an internal repo. Install the new Android studio and it should work. The same story with library support.

0
source

Source: https://habr.com/ru/post/952140/


All Articles