Flutter plugin that uses .aar modules builds and runs fine in the example app but fails to build in a different app - flutter

I've written a Flutter plugin to use an SDK that requires the inclusion of some .aar modules. It builds and runs perfectly in the example app for the plugin, but when I import the plugin in a different app and try to build it, the build immediately fails with a message saying that one of the .aar modules could not be found in the plugin. This makes no sense because the module is definitely there - the platform channels to use the SDK would fail in the example app if the module wasn't there.
Why would the example app build and run without any problems but a different app won't? The only thing I can think of is that I import the plugin from path in my pubspec but it seems unlikely to me that this is the culprit.
Any advice or assistance here would be appreciated. TIA!

I got it!!!!
The answer is as found here: How to add .aar dependency in library module?
The way this adapts to a Flutter plugin is as follows:
Add a libs folder at the root of the android project in the plugin. Add the .aar files there.
In the plugin's build.gradle file, update rootProject.allProjects to look as follows:
repositories {
google()
jcenter()
flatDir {
dirs 'libs'
dirs project(':your_plugin_name_here').file('libs')
}
}
}
Still in the same build.gradle, add your .aar file(s) as dependencies as follows:
implementation(name:'aar_name_here', ext:'aar')
In the Flutter app that you want to use the plugin for, open the app-level build.gradle file and add the plugin itself as a dependency, like so:
android {
...
dependencies {
implementation project(':your_flutter_plugin');
}
}
In the settings.gradle file for the app that us using the plugin, change
include ':app'
to
include ':app', ':your_flutter_plugin'
And this should do it!!

Related

How to setup JUnit testing in Gluon Project with Gradle

I am trying to setup JUnit testing in my Gluon JavaFX Application. I am using the Gluon Eclipse Plugin with Gradle and Java 8.
My build.gradle file looks like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'org.javafxports:jfxmobile-plugin:1.0.0-b10'
}
}
apply plugin: 'org.javafxports.jfxmobile'
repositories {
jcenter()
}
dependencies {
compile 'com.gluonhq:ignite-dagger:1.0.0'
compile 'org.elasticsearch:elasticsearch:1.6.0'
compile 'ch.qos.logback:logback-classic:1.1.5'
testCompile 'junit:junit:4.12'
}
mainClassName = 'com.me.MyApplication'
jfxmobile {
android {
manifest = 'src/android/AndroidManifest.xml'
}
ios {
infoPList = file('src/ios/Default-Info.plist')
}
}
Resolving the dependency is no problem, but when running the 'test' task, gradle throws an error like this:
When running gradle with java 8, you must set the path to the old jdk, either with property retrolambda.oldJdk or environment variable JAVA6_HOME/JAVA7_HOME
Could not execute build using Gradle distribution 'https://services.gradle.org/distributions/gradle-2.2.1-all.zip'.
I already tried to add the retrolambda plugin to gradle according to the plugin's README on GitHub, but it doesn't work so far. Could someone tell me what to do to configure my Gluon project so that I am able to run my JUnit tests with Gradle?
Some important addidtions:
For the plugin version it says: Gluon Tools 1.0.0.201508201514
I think I fogot to mention that I want to use Dagger dependency injection with Gluon Ignite which might be the real problem in my case as it requires Java 8 and might conflict with javafxports or something else. However, I'm not able to make full sense of the various error messages I've seen.
My tests are empty, but they aren't even run, because it fails before.
Your problem seems like a retroLambda configuration issue. If you go through the configuration page for the plugin, it states that if you don't have an environment variable set for JAVA6_HOME or JAVA7_HOME than you need to explicitly define oldJdk for the plugin to work properly.

Compiling greenDAO source

When I do a fresh git clone of the greenDAO repo, import the project with Android Studio, and try to compile, references to all the Android objects throw Unresolved Symbol/Method errors. Similarly, the Android specific import statements are also unresolved.
I've gotten as far as realizing that the build.gradle files don't call apply plugin: 'android', but instead lists dependencies like:
dependencies {
provided 'com.google.android:android:4.1.1.4'
provided 'com.google.android:android-test:4.1.1.4'
provided 'com.google.android:annotations:4.1.1.4'
provided 'com.google.android:support-v4:r7'
provided 'com.google.android:support-v4:r7'
...
}
I've used the SDK manager to make sure I have all the files for API v4.1 installed. I also know how to use greenDAO by using the Maven repos and/or importing JARs. My problem is specific to building from source.
Update 1: As stated, when using provided, none of the Android files are found.
I don't have enough reputation to post images, but you can find a screenshot here.

Writing a buildSrc Gradle plugin that can be published

I'm writing a custom Gradle plugin for my company to assist with integration tests of our product. My team wants to have the plugin be built with and used in the main product build (like a 'buildSrc' plugin would), but also need the plugin to be published as an artifact for other teams to use in integration with our product.
If I try and include it as a standalone plugin in the settings.gradle file and then also include it in the buildscript as a dependency, it obviously does not work because the buildscript block is interpreted first.
I also tried running another build from within the buildscript like so:
buildscript {
def connection = GradleConnector.newConnector()
.forProjectDirectory(file("${project.projectDir}/theplugin"))
.connect()
try {
connection.newBuild()
.forTasks('clean', 'build', 'install')
.run()
} finally {
connection.close()
}
repositories {
mavenLocal()
...
}
dependencies {
classpath 'com.company.product.gradle.theplugin'
}
}
This causes the plugin to be built and placed in the local Maven repo, but then the initial Gradle build fails directly afterward because it can't resolve the newly built archive. If I run it again, it works. I don't understand this behavior.
I'm probably going down a rabbit hole with this approach. Is there a way to make this work and in a less 'hacky' way?
I discovered a hacky way to accomplish this: symlink the plugins to the buildSrc (on *nix at least).
project directory
project/
buildSrc/ -> gradle_plugins/
gradle_plugins/
pluginA/
pluginB/
...
build.gradle
settings.gradle
...
build.gradle
settings.gradle
project/settings.gradle
include 'gradle_plugins:pluginA'
include 'gradle_plugins:pluginB'
...
project/gradle_plugins/settings.gradle
include 'pluginA'
include 'pluginB'
...
project/gradle_plugins/build.gradle
...
rootProject.dependencies {
runtime project(path)
}
...
The way I'm solving this is the following:
Regular multi project build with buildSrc/myPlugin/.. Within my build process I call ./gradlew -b buildSrc/myPlugin/build.gradle uploadArchives (or whichever task you use to publish your maven artifact).
Due to the "official" hack of having to add the gradle plugin to the runtime dependencies of the root project this step would fail. So I surround it with a try catch. I feel that is not perfect but it seems to work.

Building library project in Android Studio that has been imported from Eclipse

I have an Android library project in Eclipse that I am trying to build with Android Studio so I can generate an .aar file for my users.
The project seems to have been imported cleanly into Android Studio using the "Import Project" option in the welcome screen.
How do I now build the module? The instructions on the dev site say that I need to change:
apply plugin: 'android'
to
apply plugin: 'android-studio'
However my build.gradle file doesn't have that line (I would have assumed that the importer would have added it(?)).
If I insert the line apply plugin: 'android-library', and try to 'Sync Project with Gradle Files', I get the error:
Gradle 'MyProject' project refresh failed
Error:C:\Users\Fred\AndroidStudioProjects\MyProject\src\main\AndroidManifest.xml
(The system cannot find the path specified)
The same happens if I try to make the project anyway.
Here's what my build.gradle looks like:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
apply plugin: 'android-library'
My searches to find a solution have failed. Anyone know what is going on?
I'm using the latest Android Studio (0.5.8)
Don't put the apply plugin: 'android-library' statement in the top-level build file. You should find an apply plugin statement in your module-level build file, and you can modify it there as necessary.
Okay. After some head scratching and more searching, I resolved all my problems I think. For the record:
I think my first import broke somehow. Partly because of the problem highlighted by Scott Barta and possibly because my project directory had spaces in it? Not totally sure.
A later import went more smoothly and I was able to sync and build without errors. However, apart from the build output in the Gradle Console, there was no evidence anything had actually happned. The artifact (ie. the .aar file) doesn't show up in the UI.
After finding this question: How to export library to Jar in Android Studio? I realised that the aar file is in fact built. It is just hidden from you. You have to root around in the file system to find the .aar. It is in:
<Library module>/build/libs/
Something that the docs don't tell you anywhere. Sigh.
Hope this helps someone else..

Android SDK tools rev 19: Issue with external jar files

I know this question was asked a couple of times on stackoverflow - but I still face some problems trying to add an additional jar file to an existing android project - using android sdk tools rev. 19.
Summary:
I use eclipse Version: 3.6.2
The project compiled and the artefacts worked with android sdk tools rev. 14/any android >2.2 (>= API 8)
The project does compile with sdk tools rev. 19/any android >2.2 BUT the artifacts do not contain the jars defined within the classpath - so I keep getting java.lang.NoClassDefFoundError exceptions
I tried to add "jar.libs.dir=lib" to my project.properties - since lib contains all the jars without any positive effect.
I also tried to create a new dummy project adding jars as I always did (all jars added to a lib folder within the project, right click on the jar > add to build path)
I tried to add the jars using import
Thank you for your help.
Rename your "lib" folder to "libs".