Add Flutter module to native Android app via standalone .aar file - android

I am trying to add a Flutter Module to a native Android app via an .aar file.
I followed the steps here:
https://flutter.dev/docs/development/add-to-app/android/project-setup
I create a new Android project. I also created a new Flutter Module independently (different location and IDE project outside of the Android project). Then I built an .aar via flutter build aar of the module. Then I created a module from that .aar file in the Android project.
Unfortunately, I can not get it to work. After adding the aar module, I can not access FlutterActivity, FlutterEngine etc. Only FlutterFragment and Flutter under io.flutter.facade are available. I can not see the io.flutter.embedding package.
Apparently, the Flutter dependencies are not included in the .aar archive. I tried various "fat aar" build scripts. No success. Also, as I understand, the described approach with the flutter build aar should contain all necessary Flutter dependencies, right?
Some people seem to got it to work. Unfortunately, there is no example project or specific help about the .aar approach anywhere.
Can someone help?

Add this in your AndroidManifest.xml
<activity
android:name="io.flutter.embedding.android.FlutterActivity"
android:theme="#style/LaunchTheme" android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"/>
Refer this below link
https://flutter.dev/docs/development/add-to-app/android/add-flutter-screen?tab=custom-activity-launch-kotlin-tab
This might help you.

You're right, AAR should contain all dependencies so from the native side, you don't need to do any extra steps, maybe you missing include one of them
maven {
url "$project.rootDir/../duy_flutter_module/build/host/outputs/repo"
}
maven {
url 'http://download.flutter.io'
}
remember that if your android native project has 2 modules, app and second module (contain an activity host your Flutter), your need to add AAR to both modules or add to build.gradle root inside allprojects block.
You can refer to here https://github.com/duytq94/demo-integrate-flutter

I was able to add Flutter module to Android via stand alone .aar.
Steps to add Flutter build aar to Android Native
Add aar to app/libs folder.If dont have libs folder create one.
Under app build.gradle
add
repositories
{
flatDir
{
dirs 'libs'
}
maven { url "https://storage.googleapis.com/download.flutter.io"}
maven { url 'https://maven.google.com' }
}
3.under dependencies in build.gradle.
Specify the aar that you are providing
dependencies {
implementation(name: 'flutter_release-1.0', ext: 'aar')
implementation 'io.flutter:flutter_embedding_release:1.0.0-a9d'
implementation 'io.flutter:armeabi_v7a_release:1.0.0-a9d'
implementation 'io.flutter:arm64_v8a_release:1.0.0-a9d'
implementation 'io.flutter:x86_64_release:1.0.0-a9d'
}
These below implementation you can find under outputs -> repo -> com.packagae --- -> /flutter_release/1.0{version}/flutter_release{version}.pom file
implementation 'io.flutter:flutter_embedding_release:1.0.0-a9d'
implementation 'io.flutter:armeabi_v7a_release:1.0.0-a9d'
implementation 'io.flutter:arm64_v8a_release:1.0.0-a9d'
You can find pom as below image

Related

Android Studio Gradle dependency doesn't show up in External Libraries

I'm using Android Studio 3.0.1 and I'm trying to add an online dependency and while Gradle initially syncs without a problem it doesn't show my dependency in External Libraries and my code that references the dependency doesn't work.
Here's a snippet of what my build.gradle file looks like:
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/groups/public/' }
}
dependencies {
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT'
}
I'm pretty new to android development (took over an existing project from a dev who quit without leaving any documentation) so I'm not sure if this is a mistake with how to add a project dependency or if there is a problem with the dependency that I'm trying to add. Any help would be greatly appreciated!
I was able to get this to work by changing the dependency declaration to:
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT', classifier: 'jar-with-dependencies'
The library artifacts up on the repository include an apklib and a JAR with a special classifier. The apklib format is not supported by Android Studio, and unfortunately the classifier on the JAR means that it's not accessible simply using the group-name-version format when declaring dependencies.
Your build.gradle file seems fine. If you want to keep the library specified as an external library, you can try and define the dependency using the alternative notation, replace:
compile group: 'com.fortysevendeg.android', name: 'swipelistview', version: '1.0-SNAPSHOT'
with:
compile 'com.fortysevendeg.android:swipelistview:1.0-SNAPSHOT'
The alternative approach is to download the jar file yourself and use it as a local dependency. If you navigate to the maven repository you can inspect the package which is included as a dependency and download the jar directly. Place the jar file in the libs folder of your project and add the following to your build.gradle file:
compile fileTree(dir: 'libs', include: ['*.jar'])
For further details on how to configure the dependencies of your gradle project, check out the Android Studio documentation here.
Based on the information you have provided, this should fix your issues. If this does not solve the error then there may be other issues with the project.
Your dependencies should not placed in the top-level build.gradle file where the repositories are defined. There is even a comment in that file that says so, by default.
You app dependencies should be the module's build.gradle along with the others like android-support
Additionally, that library is very old, and is a SNAPSHOT build, meaning it isn't meant to be generally used in a release environment. You should find an alternative... And there are plenty of other ListView swiping ones

Third Party Android Library Format

I have downloaded a lot of libraries from Github. When I want to add it in android studio, there's no .jar file in every library I have. Why is it that it has no jar file ? How do I add those libraries in android studio?
Whenever possible, you should find the correct line to add to the dependencies block in your build.gradle file. For example, look at the README for the Boom Menu library which you mentioned in your comment. Under the heading Gradle & Maven, you see
compile 'com.nightonke:boommenu:2.1.0'
Add this line to the dependencies block in build.gradle, sync Android Studio, and then use the library as you wish.
libraries from GitHub... Example is the Boom Menu library
This one? ... Well, you're just not reading the documentation
Otherwise, if there isn't a BinTray dependency.
This is exactly what JitPack is for
To get a Git project into your build:
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.User:Repo:Tag'
}
Regarding your questions
Why is it that it has no jar file ?
Because GitHub isn't meant for storing (potentially large) binaries of compiled code, and many Android libraries prefer BinTray over GitHub releases.
How do I add those libraries in android studio?
You could clone them directly into your project
app/
build.gradle
library/
build.gradle
settings.gradle
build.gradle
In settings.gradle
include :app, :library
In app/build.gradle
dependencies {
compile project(":library")
}

Why the lib module can't include the 3rd lib in android studio

I had an issue
"java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/commons/lang/StringUtils;"
when i tried to run an android app that deponds on my own android lib.
Fllowing is my steps:
Created an android project ,and then built a new module named "sptvlib" in it.
This module depends on a 3rd party lib named "common-lang" that is a String utils lib.please see the "sptvlib" gradle file:
dependencies {
...
compile 'org.apache.directory.studio:org.apache.commons.lang:2.6'
...
}
After built the whole project, i got the module sptvlib.aar file, and copied it to the "libs" directory of another android app project.
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
...
compile(name: 'sptvlib', ext: 'aar')
...
}
I built this app successfully, but when it run, the logcat showed " java.lang.NoClassDefFoundError" error.
I have to re-add "commons.lang" declatration to the app project gradle, and then it run ok... we have added the 3rd party lib to my module use the "compiled". Why the app that deponds on my own module still could't find it in the runtime?
I assume you are using some function of commons.lang in your main app project.
Since you are using compiled assembled code of your sptvlib library, you can used the compiled exposed functions of sptvlib in your main app project.
But you can not access the library functions of commons.lang directly in your main app project.
This is because you have not added sptvlib code as a module to your app project. you are only using the compiled version of sptvlib.

Reuse modules in Android Studio

Whenever I add a android library-project as module to my Android Studio project, the sources get copied to the Android Studio project folder.
Is there a way to solve it like in eclipse, where there is only one copy of library project, any many projects can reference it?
You have different ways to achieve it:
using a local module referring the right path
adding an aar file
using a maven repo
CASE 1:
Using gradle and a local library, inside a project you can refer an external module.
Just use:
Project
|__build.gradle
|__settings.gradle
|__app (application module)
|__build.gradle
In settings.gradle:
include ':app'
include ':myLib'
project(':myLib').projectDir=new File('pathLibrary')
In app/build.gradle:
dependencies {
compile project(':myLib')
}
Pay attention to myLib.
You have to use the path of the library inside the other project, not the root of the project.
CASE 2:
Compile the library module, get the aar file, and then add to the main project:
Add the folder where you put the aar file as repository:
repositories {
jcenter()
flatDir {
dirs 'libs'
}
}
Then add the dependency:
dependencies {
compile(name:'nameOfAarFile', ext:'aar')
}
CASE 3:
Another way is to to publish your module into a maven repository.
In this way, just use:
dependencies {
compile ('mypackage:mymodule:X.Y.Z')
}
I would compile your shared code as library project. So you will get an aar file which you can reference.
To create that Android Archive you need to build the project as a release build with this command:
gradlew aR
After that you have a file called <modulename>-release.aar this file is located in <projectroot>/<modulename>/build/outputs/aar. I rename those files to <modulename>.aar then you can put it into your lib directory of your module.
When done you can reference it from the module where you need it like this:
compile(name:'<modulename>', ext:'aar')
This also speeds up the build time since you don't need to compile the project anymore.

AChartEngine And Android Studio

I have to create an application that makes extensive use of charts.
Reading the web I chose achartengine that seems to have everything I need.
I downloaded the jar file, I plugged in the libs folder, I selected "add to library" and I lunch the gradlew clean.
Result in the sources where I do the import of org.achartengine.xxxx I always returned the error that fails to resolve symbols .
Do you have suggestions?
Thank you
Andrea
I am able to use this library in my Android Studio project, this topic explains how to add AChartEngine repo to your project.
What I did:
Added following to project-wide build.gradle (one from the project root):
allprojects {
repositories {
...
maven {
url "https://repository-achartengine.forge.cloudbees.com/snapshot/"
}
}
}
For every module that uses the library, add this to its build.gradle (you may put this to the top-level build.gradle if it should be included in all modules):
dependencies {
...
compile group: 'org.achartengine', name: 'achartengine', version: '1.2.0'
}
Now I can use the library in the project, I see the classes in code assist popups and build runs as succeeds.
It seems like the new version (1.2.0) is not available for download anymore in the http://www.achartengine.org/ site. and that's why the gradle/maven method doesn't work (or the snapshot file was removed).
I succeeded using and adding it to my project by downloading the jar file from here:
https://github.com/ddanny/achartengine/files/460139/achartengine-1.2.0.zip

Categories

Resources