IntelliJ, Android and Gradle

I'm trying to get my existing Android project to work with gradle and IntelliJ 12. I used to work with maven, but it didn't seem to be as flexible as gradle, and from what I think I found out is that I need fewer subfolders.

My Android project is split into a clean java library (: core) and the actual application (: android). These two projects are in my main project folder.

~-+MainProject |--+core | L--build.gradle |--+android | L--build.gradle |--build.gradle L--settings.gradle 

I think that the ideal solution for getting gradle to work with this is to consider the project (: core) and (: android) as nested projects, that is, I can just be with my cmd in the MainProject source folder to start gradle tasks.

However, I ran into various problems:

  • Gradle dependencies are included only in the main project
    • this means that neither the testTest tool nor the main project files have directories correctly set as source / test directories.
    • IntelliJ does not allow any classes that I added as dependencies in gradle (AndroidSDK, junit, mockito, those from the project: core)
      • I tried to use the plugin idea, but either I used it incorrectly or did not solve the problem.
  • I get duplicate dependency Error to create APK
    • should have something to do with junit: 4.11 and mockito-core: 1.9.5, which I added
    • what i tried:
      • removing dependencies -> building failures leads to the fact that some classes, of course, cannot be resolved.
      • transition to junit: 4.5+ as suggested in some other thread -> no changes at all

Here are the * .gradle configurations

MainProject: - settings.gradle

 include ':core', ':android' 

- build.gradle

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } subprojects { repositories { mavenLocal() maven { url "http://repo.maven.apache.org/maven2" } } } 

: core - build.gradle

 apply plugin: 'java' dependencies { testCompile 'junit:junit:4.11' testCompile 'org.mockito:mockito-core:1.9.5' } 

: Android

 apply plugin: 'android' repositories { mavenCentral() } dependencies { compile project(":core") compile 'com.google.android:android:4.1.1.4' instrumentTestCompile 'junit:junit:4.11' instrumentTestCompile 'org.mockito:mockito-core:1.9.5' instrumentTestCompile 'com.google.dexmaker:dexmaker:1.0' instrumentTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0' instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:3.6' } /* ... androidSettings 

I hope someone can help me with this

MFG Dornathal

+5
source share
1 answer

Ok, so you have the right idea, but there are a few changes that you still need.

Your root build.gradle file should be as follows:

 subprojects { repositories { mavenCentral() } } 
  • You need to enable mavenLocal() if you are using a locally installed repo. Most people do not, and nothing in your project indicates what you need.
  • mavenCentral() can be used to replace with the maven url you used.
  • We only need to change the buildscript for the android project, so we need to localize it in the android project build.gradle .

Your settings.gradle and your build.gradle for the main project are good.

However, your build.gradle for an Android project needs some changes:

 buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.6.+' } } apply plugin: 'android' dependencies { compile project(":core") instrumentTestCompile 'com.jayway.android.robotium:robotium-solo:3.6' } 
  • Including the android plugin, we directly contact the installed SDK. This means that we no longer need to include the android: compile 'com.google.android:android:4.1.1.4' dependency.
  • We do not need to include junit . It is provided by the SDK, so we just use it. It should be noted that the SDK includes only JUnit 3.
  • We do not need to include mockito and dexmaker unless we actually use it for Android tests. If it is used only for tests in the java library, it is not needed here.

To answer your questions:

  • I'm not sure what you are asking here. This can help if you need your project structure, including directories in which you have files, etc.
  • Your guess is correct. Hamcrest, which provides tests for tests, has made changes to their API between versions 1.1 and 1.3. JUnit 4.11 has a dependency on Hamcrest 1.3. However, Mockito 1.9.5 is dependent on Hamcrest 1.1. Therefore, when both are turned on, 1.3 overrides 1.1, and Mockito no longer works. Reaching out to JUnit 4.5 is also a problem. Junit 4.5 includes the Hamcrest 1.1 jar as a file, and not as a dependency in POM. Thus, this will cause problems if we have 2 versions of the same file. JUnit 4.10 is how you want to go here. It depends on Hamcrest 1.1 and includes it in the POM instead of the file. To win! I also had problems with this, and the best way to figure it out is to just look at the POM files on Maven Central and see what they tell me.

Last remark. Intellij 12 cannot handle Gradle multi-project builds. To do this, you need to switch to Android Studio or Intellij 13.

+6
source

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


All Articles