I wrote this article before. How to deal with Java application dependencies that cannot be resolved with Android Studio 3
The method in this article wasn't smart, so I found a good way to try again.
--There is a project named MyApplication. --MyApplication has a module (Android application) called app. --MyApplication has a module (Java application) called purejava. (Dependency cannot be resolved by executing this module alone)
Each build.gradle
looks like this
./MyApplication/purejava/build.gradle
./MyApplication/app/build.gradle
./MyApplication/build.gradle
First, add ʻapply plugin:'idea'` to the build.gradle of the module you want to apply. This time, Gson is added as an example. Other than that, it is automatically generated by default.
groovy:./MyApplication/purejava/build.gradle
apply plugin: 'java-library'
apply plugin: 'idea' //this
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.google.code.gson:gson:2.8.2'
}
sourceCompatibility = "1.7"
targetCompatibility = "1.7"
Alternatively, it can be applied to all modules at once by adding it to the root build.gradle.
groovy:./MyApplication/build.gradle
allprojects {
apply plugin: 'idea'
}
Next, create Application Configuration by default and add a task called ** idea ** from ** Run Gradle task ** to ** Before launch **.
Now you can run from here with the dependencies resolved. Of course, if you run it with Debug, you can stop at a breakpoint.
If you use Gson in a suitable class of purejava module,
java:./MyApplication/purejava/src/main/com/example/MyClass.java
public class MyClass {
public MyClass() {
String jsonString = "{\"a\": 12345, \"b\": 67890}";
JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
System.out.println("a is" + jsonObject.get("a"));
System.out.println("b is" + jsonObject.get("b"));
}
public static void main(String[] args) throws Exception {
new MyClass();
}
}
Happy to use properly
a is 12345
b is 67890
Sync Project with Gradle Files updates **. Iml **.
Looking at .iml, for example, if Gson is in dependencies, such an orderEntry is generated.
.iml
<orderEntry type="library" exported="" scope="PROVIDED" name="gson-2.8.2" level="project" />
What's wrong here is scope =" PROVIDED "
.
This is because manually modifying it to scope =" "
can resolve the dependency without having to perform the idea task above.
Therefore, execute ./gradlew idea
.
After that, if you look at .iml, it specifies the Gson jar with the full path, and the dependency is resolved.
I think there is a way to implement a task in .gradle that modifies scope to "", but I didn't understand so I gave up.
For the time being, it's very good because you can easily stop at a breakpoint, just like in the Android Studio 2.x era.
Since the idea task is executed every time, the execution is delayed by one tempo.
After all, it seems that .iml will not be updated unless Sync Project with Gradle Files, so I also created a version of Run Configuration without idea task in Run Gradle task, and usually use this, and manually ./ after Sync You may want to run gradlew idea
to update the .iml.
Is it possible to execute additional tasks during Sync Project with Gradle Files?
Recommended Posts