Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot start FlutterActivity when adding flutterpart library as aar module to existing application #18025

Closed
tomergoldst opened this issue May 30, 2018 · 32 comments
Labels
a: existing-apps Integration with existing apps via the add-to-app flow engine flutter/engine repository. See also e: labels. t: gradle "flutter build" and "flutter run" on Android tool Affects the "flutter" command-line tool. See also t: labels. waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds

Comments

@tomergoldst
Copy link

tomergoldst commented May 30, 2018

Steps to Reproduce

  1. Create new flutter project

  2. Create new Android app

  3. Add flutter project into existing android app (from step 2) according to

  4. run the android app and start the MainActivity of flutter project -> everything works

  5. run gradle -> assemble for the flutter project Build directory

  6. copy the flutterpart-release.aar generated at build/outputs/arr

  7. Create a different new Android app

  8. Create new module ->Import JAR/ARR package -> select flutterpart-release.aar -> finish

  9. add to app build.grade "implementation project(':flutterpart')"

  10. add button to start flutterpart MainAcitivty
    ComponentName componentName = new ComponentName(getBaseContext(),
    "com.example.flutterpart.MainActivity");
    Intent intent = new Intent().setComponent(componentName);
    startActivity(intent);

  11. Run the app -> click the button -> app crashed

Logs

Crash log

05-30 16:32:36.124 21199-21199/com.example.androidwithflutter3 A/flutter: [FATAL:flutter/fml/icu_util.cc(95)] Check failed: context->IsValid(). Must be able to initialize the ICU context. Tried: /data/user/0/com.example.androidwithflutter3/app_flutter/icudtl.dat
05-30 16:32:36.124 21199-21199/com.example.androidwithflutter3 A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 21199 (oidwithflutter3)
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel beta, v0.4.4, on Microsoft Windows [Version 10.0.16299.125], locale he-IL)
[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[√] Android Studio (version 3.1)
[!] IntelliJ IDEA Community Edition (version 2018.1)
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
[√] Connected devices (1 available)
@luoyushouchai
Copy link

For my app, add :
ndk { abiFilters 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'x86' }
App crash and throw the same error!

@alexandresoler
Copy link

Same for me.
Works well when including the flutter project as a module with sources (like https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps)
Doesn't works when exporting the flutter project as an aar and include it on my Android APP as a fat aar. (Must be able to initialize the ICU context...)

@xster xster added engine flutter/engine repository. See also e: labels. a: existing-apps Integration with existing apps via the add-to-app flow labels Jun 25, 2018
@davidbilly
Copy link

Same for me, after export to AAR.

App crash and show "A/flutter: [FATAL:flutter/fml/icu_util.cc(95)] Check failed: context->IsValid(). Must be able to initialize the ICU context. Tried: /data/user/0/APP_ID/app_flutter/icudtl.dat"

@davidbilly
Copy link

Guys, after few attempt and i manage let AAR work with my demo app on simulator.

Reason : The "assets/flutter_shared/icudtl.dat" was not generated when you're export to AAR but export APK does include "icudtl" file if you directly embedded flutter SDK.

How to :
Step :

  1. Build APK with flutter SDK directly. e.g : https://github.com/flutter/flutter/tree/flutter_module/examples/flutter_view
  2. After you successfully build APK and use zip to extract APK and copy out the "icudtl.dat"
  3. create "src/main/assets/flutter_shared/" folders and copy "icudtl.dat" to under "flutter_shared" folder on your library which used to export AAR later.
  4. export your library to AAR and extract it make sure "icudtl.dat" created.
  5. import AAR file into your main project and enjoy it.

Note : i'm not sure that "icudtl.dat" file what for and not sure will be work on all of android devices, please share/contribute me the result as i not own much android devices to test.

Please share if have any better workaround for this issue.

Hopefully, in future flutter dev @mit-mit @mravn-google will included for export AAR.

Sorry for my bad english.

@mravn-google
Copy link
Contributor

@davidbilly Using the new Flutter module template, the icudtl.dat file is included in the apk of the host app. That file tells Flutter how to do international text processing with unicode.

@davidbilly
Copy link

davidbilly commented Jun 27, 2018

@mravn-google

Hi, thanks for quick reply.

For my case, i have to use our company existing native SDK for our customer app and i'm thinking by using flutter framework just for UI part for IOS and android for 1 time development.

But the logic part still on native which we already developed long time ago.

So we have to use AAR to support our customer app. So far seem "flutter_view" and "platform_view" fulfil our requirement.

Also, we don't wish our customer have to setup for flutter env and download flutter source code just because they using our SDK. its too much development cost for them.

So, all the source within AAR will be better, they just need to import our AAR SDK to their project.

Thanks.

This is my env :

[✓] Flutter (Channel master, v0.5.6-pre.82, on Mac OS X 10.12.6 16G29, locale en-SG)
[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[✓] Android Studio (version 3.1)

@matthew-carroll matthew-carroll moved this from To do to To do (Bugs) in Add-to-App (stale) Aug 20, 2018
@chaangliu
Copy link

@davidbilly Using the new Flutter module template, the icudtl.dat file is included in the apk of the host app. That file tells Flutter how to do international text processing with unicode.

Hi I'm using the new Flutter module template and adding flutter module as source code, but still encountering this issue..

When searching in the project I can see icudtl.dat is already included in the jar. Why can't flutter find it?
img

@matthew-carroll
Copy link
Contributor

@tomergoldst I tried to repro your issue but was unable to find any issues. Here are the steps that I took:

  1. Created an Android app project called android_app
  2. Created a Flutter module project called flutter_module
  3. Linked android_app to flutter_module as per the steps in the Wiki
  4. Changed MainActivity in android_app to extend FlutterActivity
  5. Launched android_app and confirmed that the Flutter UI displayed as expected
  6. Changed android_app to an Android library by editing the build.gradle file
  7. Ran ./gradlew assemble to create an AAR
  8. Created a new Android app project called android_app2
  9. Added the earlier AAR as a module to android_app2
  10. Added a button to MainActivity2 within android_app2 that launches MainActivity from within the AAR

The above steps worked as expected without any build errors or crashes.

I'm going to close this ticket. If you still have issues with the above steps then please re-open with any additional information that might help.

@comigor
Copy link
Contributor

comigor commented Oct 30, 2018

@matthew-carroll We've just created a repo to reproduce the issue following your step above (using flutter 40fc45a): https://github.com/Igor1201/min-repro-flutter

Your steps actually were missing the import of flutter classes themselves (the .aar generated from the .android/Flutter project) that is not embedded on the MyApplication project (the one you converted from an app to a library).

So our steps actually were:

  1. Created an Android app project called app (on android folder)
  2. Created a Flutter module project called flutter_module (on root)
  3. Linked app to flutter_module as per the steps in the Wiki
  4. Changed MainActivity in app to extend FlutterActivity
  5. Launched app and confirmed that the Flutter UI displayed as expected
  6. Changed app to an Android library by editing the build.gradle file
  7. Ran ./gradlew assemble to create an AAR
  8. Created a new Android app project called app (on android_app_2 folder)
  9. Added the earlier AAR as a module to android_app_2
  10. Ran ./gradlew flutter:assemble on android folder
  11. Added the flutter AAR (flutter_module/.android/Flutter/(...)/outputs/flutter-debug.aar) as a module to android_app_2
  12. Added a button to MainActivity2 within android_app_2 that launches MainActivity from within the app AAR
  13. Crashed with the missing icudtl.dat error

We thought about some solutions to this problem:

  1. Change flutter.jar to be flutter.aar
    By definition, jar files on android shouldn't contain assets, so migrating flutter to an AAR would make it work, as the compiler would than merge the assets.

  2. Add an assets folder containing flutter_shared to the generated .android/Flutter module
    This way, the output of this project (which is an AAR) will contain the flutter_shared folder inside assets, which will be merged to the final APK.

What do you think about the solutions? Do you have anything in mind, or do you have any concern with any of these approaches?

@graydalf
Copy link

graydalf commented Nov 2, 2018

@tomergoldst I tried to repro your issue but was unable to find any issues. Here are the steps that I took:

  1. Created an Android app project called android_app
  2. Created a Flutter module project called flutter_module
  3. Linked android_app to flutter_module as per the steps in the Wiki
  4. Changed MainActivity in android_app to extend FlutterActivity
  5. Launched android_app and confirmed that the Flutter UI displayed as expected
  6. Changed android_app to an Android library by editing the build.gradle file
  7. Ran ./gradlew assemble to create an AAR
  8. Created a new Android app project called android_app2
  9. Added the earlier AAR as a module to android_app2
  10. Added a button to MainActivity2 within android_app2 that launches MainActivity from within the AAR

The above steps worked as expected without any build errors or crashes.

I'm going to close this ticket. If you still have issues with the above steps then please re-open with any additional information that might help.

Hi, I need your help.
Can you show me the build.gradle of your sixth step? please.

@matthew-carroll
Copy link
Contributor

I wanted to let you all know that I've seen the new posts here, I just haven't had a chance to come back to this issue. I'll re-open it and come back to it when I can.

@Igor1201 FYI - Changing flutter.jar to flutter.aar is currently not possible due to infrastructure issues within the Flutter embedding/engine. I'll look into your other suggestion when I get a chance.

Add-to-App (stale) automation moved this from Done to In progress Nov 2, 2018
@MichaelShnaider
Copy link

MichaelShnaider commented Nov 7, 2018

@matthew-carroll Manually adding the icudtl file into a new assets folder works and fixes the issue, but now when you have a Flutter project that uses any plugin like path_provider for example, and you build the AAR file (with the icu file included), the project throws a missing plugin exception cause none of the plugins were included in the AAR im guessing. It's an addition to the icu issue. I made a separate issue for the missing plugins in the AAR here: #23944

@jelenalecic
Copy link

@mravn-google any updates here? issue is still present.

@matthew-carroll
Copy link
Contributor

Update: Support for AAR packaging is still not an imminent implementation. It is on the roadmap but there are no hard dates.

CC for feature request visibility: @dnfield @cbracken @chinmaygarde @jason-simmons @sbaranov @mklim

@comigor
Copy link
Contributor

comigor commented Nov 28, 2018

@matthew-carroll We've already implemented the second suggestion discussed here on the following PR: #23782

Hixie requested some tests on this matter however, as we described here, we're not sure how to implement these tests as the changes are on gradle level and none of these are currently tested (and we're not sure how to setup a infrastructure to do this).

Do you have any idea on how can we advance this for now?

@jelenalecic
Copy link

@Igor1201 and this PR solves the issue?
Are you able to import your flutter .aar into you project, and everything works fine? Even plugins used in flutter .aar?
This is a showstopper for me at the moment, and I hope this will be fixed asap.

@comigor
Copy link
Contributor

comigor commented Nov 28, 2018

@jelenacarnegie Yes. We are able to make this work with the PR + generate a FatAAR, as discussed on #23944

@jelenalecic
Copy link

@eseidel @cbracken

As I already said, this is a deal breaker for me(and probably many other developers).
I'm not able to import generated Flutter .aar into my existing Android project(and solution to import it as a module is not an option at the moment).

@Igor1201 says his PR solves this issue.

Can anyone from Flutter team at least take a look at it?

Thank you.

@eseidelGoogle
Copy link
Contributor

eseidelGoogle commented Nov 28, 2018

Things are pretty busy with Flutter Live being only a few days away. I suspect this will need some help from @matthew-carroll. To set expectations: with Flutter Live and the holiday season, it's possible we won't get to help get the PR resolved until early 2019. :( Obviously we'll try to do to better than that.

@matthew-carroll
Copy link
Contributor

In additio to @eseidel's comments, for visibility, this issue might require work or input from any of the following: myself, @jason-simmons, @sbaranov, @mklim, @chinmaygarde, @cbracken

If this problem is primarily Gradle related then I think myself, @jason-simmons, and @sbaranov are likely to be involved.

If this problem requires general AAR support then we have a buildroot issue that is likely to involve @jason-simmons, @chinmaygarde, and @cbracken

@zoechi zoechi added tool Affects the "flutter" command-line tool. See also t: labels. t: gradle "flutter build" and "flutter run" on Android labels Nov 29, 2018
@pandavickey
Copy link

pandavickey commented Nov 30, 2018

  1. It's a Bug with the Android Gradle tools for version 3.0.+. It will leave out a file (icudtl.dat) into your AAR.
  2. You can change your Android Gradle tools version to 2.0.+.(2.2.3 is ok for me)
  3. But there is another problem. Gradle 2.0.+ has a bug that it can't stay same the buildtype for library with main project. In my case, I copy the flutter.gradle file into my project,change the buildModeFor function from read buildtype to read local.properties:
private String buildModeFor(Project project) {
     return resolveProperty(project, 'flutter.buildMode', 'release')
}

@carl1990
Copy link

carl1990 commented Dec 3, 2018

+1
when I user debug.aar integrate into my native app is ok,use release.aar will be into crash

@jelenalecic
Copy link

@carl1990 proguard maybe?

@carl1990
Copy link

@jelenalecic yes ,has resolved

@kimlet
Copy link

kimlet commented Dec 21, 2018

just copy flutter_shared to your project assets

image

@jelenalecic
Copy link

@eseidel

any updates? :)

@eseidelGoogle
Copy link
Contributor

I have no updates, no.

Re-reading the bug, is the fix here just to make Flutter's gradle plugin and gradle templates a bit smarter to be .aar compatible? Is that a correct read @Igor1201 @matthew-carroll ?

We don't have anyone on the Flutter team at Google who is particularly well versed in Gradle (hence a bunch of small bugs and needless performance slowdowns I'm told in our gradle rules).

If someone in the community is particularly gradle-saavy and would like to help with this or other bugs in Flutter, contributions would be most welcome! :)

FYI @tvolkert

@mahendramahi
Copy link

Use case

In my project we have ios and android native project. But after heard about flutter we decide that new functionality will develop into flutter module ( flutter create -t module project_name ) and finish work on flutter, we export .aar file from flutter project.

Now go to my android project and import flutter exported .aar file in android project as module or lib. for reference see the image
Untitled

After import we analyze .aar just like .apk file and here are image for reference
analyze

How can we call dart route page from Android app

`
@OverRide
protected void onCreate(Bundle savedInstanceState){
Flutter.startInitialization(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics displayMetrics=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
height=displayMetrics.heightPixels;
width=displayMetrics.widthPixels;

    View flutterView=Flutter.createView(
    MainActivity.this,
    getLifecycle(),
    "/"
    );
    FrameLayout.LayoutParams layout=new FrameLayout.LayoutParams(width,height);

    addContentView(flutterView,layout);
    }`

Call this code from my android activity ..
and we are successfully able to load flutter view on android activity
and for android we export app.framwork and flutter.framwork , ios app also able to load same view as like android(save time of development same thing in different lang or on difference platform )

issue

Now we create other module in flutter and import same as last time. during the import .aar we can change name of module using android studio. Here are we attaching image reference.

next

Now you can see the .so file in second .aar file with the same name of last one .aar file..

when we run this it gives error
More than one file was found with OS independent path libflutter.so'

@tvolkert
Copy link
Contributor

FYI @blasten @xster

@xster
Copy link
Member

xster commented Aug 9, 2019

I'd like to reset this bug since the previous solution was a half offering and a lot has changed since. For instance, we have a proper flutter build aar CLI function now.

If you had issues with this in the past, could you try the new process https://github.com/flutter/flutter/wiki/Add-Flutter-to-existing-apps#1-depend-on-the-android-archive-aar on the master channel?

@xster xster added the waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds label Aug 14, 2019
@kf6gpe kf6gpe added this to the December 2019 (Add-to-App) milestone Aug 29, 2019
@no-response
Copy link

no-response bot commented Sep 4, 2019

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away!
Thanks for your contribution.

@no-response no-response bot closed this as completed Sep 4, 2019
Add-to-App (stale) automation moved this from In progress to Done Sep 4, 2019
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a: existing-apps Integration with existing apps via the add-to-app flow engine flutter/engine repository. See also e: labels. t: gradle "flutter build" and "flutter run" on Android tool Affects the "flutter" command-line tool. See also t: labels. waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds
Projects
No open projects
Development

No branches or pull requests