How to package Dotnet.Android as an aar ?

兴钢 李 0 信誉分
2024-08-06T11:53:05.6966667+00:00

I tried to build Dotnet.Android library as an aar using Android Studio, but without success:

The original version of Dotnet.Android built by MSBuild has the "assemblies" folder is in the top level folder of the apk , but I don't know how to achieve this effect with gradle commands.

I tried to copy assemblies to a folder and set the sourceSet attribute, but assemblies are then copied to "assets“ folder, not the "assemblies" folder, which causes the test app crashing with something as

Make sure that all entries in the APK directory named assemblies/ are STORED (not compressed)

I also asked ChatGPT, it told me to use the libraryVariants attribute as follows (kotlin):

libraryVariants.forEach { variant ->

val packTask = variant.packageLibraryProvider

//-----------------this log didn't show, meaning the code block is not executed.

println("------------------------------------------ variant = $variant, pack = $packTask")

variant.outputs.forEach { output ->

println("------------------------------------------ outputDir = $output")

val outputDir = output.outputFile?.parentFile

if(outputDir != null){

val assembliesDir = File(outputDir, "assemblies")

if (assembliesDir.exists()) {

tasks.register<Copy>("copyAssembliesToAAR") {

from(assembliesDir)

into(outputDir)

}

}

}

}

}

But as the comment showed, it didn't work.

So I am stuck on the above problem. To make further progress, I see two possibilities:

1, modify the source from https://github.com/dotnet/android (embedded-assemblies.hh, specifically), change the default assemblies' search path to "assets/assemblies". It means I must compile a version which is not compatible with MS's.

2, since Dotnet.Android app built from Visual Studio can put assemblies to the top level folder inside the apk, Learn how it does and replicate in the gradle scripts. I am not familiar with either MBBuild or gradle (I use Visual Studio/Android Studio primarily), so this road is more difficult to me.

So I am here, asking for help! Can you provide a guide? Both solutions (or any other solution you propose) are welcome, although solution #2 is more welcome :)

.NET MAUI
.NET MAUI
一种 Microsoft 开源框架,用于构建跨移动设备、平板电脑、台式机的原生设备应用程序。
91 个问题
C#
C#
一种面向对象的类型安全的编程语言,它起源于 C 语言系列,包括对面向组件的编程的支持。
189 个问题
{count} 票

你的答案

问题作者可以将答案标记为“接受的答案”,这有助于用户了解已解决作者问题的答案。