背景:
新版本1.12+与老版本引入方式发生了变化,特此记录
工程结构预览
![](https://img.haomeiwen.com/i2870365/dbba2187060a478f.png)
开始打包
1、首先连接真机,run一下,保证正常启动应用;
2、flutter clean
3、flutter build aar
注意:如果flutter工程中使用第三方库那么需要用到fat-aar 插件工具
详细https://juejin.im/post/5e153f10f265da5d6b3cb767
第三方Fat-aar https://github.com/kezong/fat-aar-android
使用方式一(依赖本地仓库)
-
Open <host>/app/build.gradle
-
Ensure you have the repositories configured, otherwise add them:
repositories { maven { url '/Users/niudong/Desktop/Project/android/flutter_library/build/host/outputs/repo' } maven { url '$storageUrl/download.flutter.io' } }
-
Make the host app depend on the Flutter module:
dependencies {
debugImplementation 'com.example.flutter_library:flutter_debug:1.0'
profileImplementation 'com.example.flutter_library:flutter_profile:1.0'
releaseImplementation 'com.example.flutter_library:flutter_release:1.0'
}
- Add the
profile
build type:
android {
buildTypes {
profile {
initWith debug
}
}
}
详细介绍:
[https://flutter.dev/docs/development/add-to-app/android/project-setup#option-a---depend-on-the-android-archive-aar]
使用方式二 (不依赖本地仓库)
1、找到你的flutter根目录下的build/host/outputs/repo,将flutter_release-1.0.aar复制到android项目下
/Users/niudong/Desktop/Project/android/flutter_library/build/host/outputs/repo/com/example/flutter_library/flutter_release/1.0
![](https://img.haomeiwen.com/i2870365/a61061bafcbb9540.png)
2、打开你的安卓工程,将aar copy到libs目录下
![](https://img.haomeiwen.com/i2870365/d4b5505a10653223.png)
3、app build.grade配置
repositories {
flatDir {
dirs 'libs' // aar目录
}
}
添加如下:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation(name: 'flutter_release-1.0', ext: 'aar')
implementation 'io.flutter:flutter_embedding_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
implementation 'io.flutter:armeabi_v7a_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
implementation 'io.flutter:arm64_v8a_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
implementation 'io.flutter:x86_64_release:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a'
}
注意:1.0.0-626244a72c5d53cc6d00c840987f9059faed511a 来自哪里呢?
姿势:找到flutter根目录下的build/host/outputs/repo, 将flutter_release-1.0.pom,里面有如下flutter基础库的地址
<groupId>com.example.flutter_library</groupId>
<artifactId>flutter_release</artifactId>
<version>1.0</version>
<packaging>aar</packaging>
<dependencies>
<dependency>
<groupId>io.flutter.plugins.sharedpreferences</groupId>
<artifactId>shared_preferences_release</artifactId>
<version>1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.flutter</groupId>
<artifactId>flutter_embedding_release</artifactId>
<version>1.0.0-626244a72c5d53cc6d00c840987f9059faed511a</version>
<scope>compile</scope>
</dependency>
4、外层build.grade配置
buildscript {
repositories {
google()
jcenter()
maven {
url "http://download.flutter.io"
}
}
dependencies {
classpath 'com.android.tools.build:gradle:4.0.0'
}
}
5、在你的MainActivity 加入如下代码
import androidx.appcompat.app.AppCompatActivity;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.view.FlutterMain;
/**
* 原生安卓跳转Flutter
*/
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
FlutterMain.startInitialization(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivity(FlutterActivity.createDefaultIntent(this));
}
}
6、在你的清单文件中加入FlutterActivity,注意导包io.flutter.embedding.android
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.androiddemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:name=".FlutterApplication"
android:theme="@style/AppTheme">
<activity android:name="io.flutter.embedding.android.FlutterActivity"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
7、End!run你的Android程序:
![](https://img.haomeiwen.com/i2870365/8865e159420d719b.jpg)
网友评论