【问题标题】:How to compile an Android app with aapt2 without using any build tools?如何在不使用任何构建工具的情况下使用 aapt2 编译 Android 应用程序?
【发布时间】:2019-01-20 08:12:05
【问题描述】:

我正在尝试在 Android Studio 上编译我刚刚在 Kotlin 中创建的 Android 应用程序,而不使用 Gradle 或任何其他构建工具。我的目的是加快应用程序的编译速度,而无需使用 Android Studio 或安装 Gradle、ant 或 buck 等构建工具。我在使用 aapt2 链接文件时遇到问题。

我正在使用 aapt2 将 res 文件夹中的所有文件编译成一个 zip 文件。但是当我尝试链接它们时,aapt2 会吐出错误。 错误似乎是由于缺少应用程序兼容库,我的问题是如何成功链接所有这些和 kotlin 文件以创建可部署的 apk 文件。

下面编译res文件夹中的所有文件并创建resources.zip文件。

$AAPT compile --dir $APP_DIR/src/main/res -o $APP_DIR/build/intermediate/resources.zip    

然而这失败了。

$AAPT link -o $APP_DIR/build/intermediate/ -I $PLATFORM $APP_DIR/build/intermediate/resources.zip --manifest $APP_DIR/src/main/AndroidManifest.xml

出现以下错误

error: resource style/Theme.AppCompat.Light.DarkActionBar (aka com.example.myapplication:style/Theme.AppCompat.Light.DarkActionBar) not found.
./projects/MyApplication/app/src/main/res/values/styles.xml:6: error: style attribute 'attr/colorPrimary (aka com.example.myapplication:attr/colorPrimary)' not found.
./projects/MyApplication/app/src/main/res/values/styles.xml:7: error: style attribute 'attr/colorPrimaryDark (aka com.example.myapplication:attr/colorPrimaryDark)' not found.
./projects/MyApplication/app/src/main/res/values/styles.xml:8: error: style attribute 'attr/colorAccent (aka com.example.myapplication:attr/colorAccent)' not found.
error: resource style/ThemeOverlay.AppCompat.Dark.ActionBar (aka com.example.myapplication:style/ThemeOverlay.AppCompat.Dark.ActionBar) not found.
./projects/MyApplication/app/src/main/res/values/styles.xml:11: error: style attribute 'attr/windowActionBar (aka com.example.myapplication:attr/windowActionBar)' not found.
./projects/MyApplication/app/src/main/res/values/styles.xml:12: 
error: style attribute 'attr/windowNoTitle (aka com.example.myapplication:attr/windowNoTitle)' not found.
error: resource style/ThemeOverlay.AppCompat.Light (aka com.example.myapplication:style/ThemeOverlay.AppCompat.Light) not found.

错误:链接引用失败。

这似乎是由于缺少应用程序兼容库。我尝试手动下载 appcompat-v7-27.1.1.aar 文件并链接它,但这失败了。

如果有人遇到此问题的解决方案,请赐教。谢谢。

我想用 aapt2 复制这里可用的内容 https://github.com/authmane512/android-project-template/blob/master/build.sh

【问题讨论】:

  • 我怀疑这会“加速编译应用程序”。 Gradle 在避免不必要的编译方面做得很好。您认为哪一部分可以做得更快?
  • 加速部分是有问题的,我同意。我只是想看看我是否可以在不使用 Gradle 的情况下完成我正在从事的项目。我的计划是预先下载依赖项 gradle 拉入到一个文件夹中,并在 aapt2 链接阶段添加它们以创建一个中间 apk 文件,然后我可以将其与 kotlin 二进制文件合并。

标签: android bash aapt2


【解决方案1】:

实际上使用额外的支持库非常困难和混乱,您会遇到这些错误,因为您使用的支持库的主题不是 android.jar 的一部分,而是您可以使用 android.jar 的默认主题。 随便放

public  class MainActivity extends Activity {...}

代替

public  class MainActivity extends AppCompatActivity {...}

并更改清单的应用程序主题指向“styes.xml”,所以基本上你必须将样式 XML 文件更改为这个

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>

</resources>

您还应该在“Manifest.xml”文件中使用以下代码将您的项目主题或样式 XML 文件中的活动指向值下

android:theme="@style/AppTheme"

这篇参考文章对您正在尝试做什么非常有帮助https://geosoft.no/development/android.html,还要签署您的 APK 以便在设备上运行您的应用程序,否则安装时可能会显示错误。 谢谢。

【讨论】:

    【解决方案2】:

    使用aapt/appt2编译java app的参考链接如下,你可以自己制作一个编译kotlin。

    https://github.com/HemanthJabalpuri/AndroidExplorer

    【讨论】:

      猜你喜欢
      • 2018-09-04
      • 2015-08-17
      • 1970-01-01
      • 2017-10-06
      • 2021-12-25
      • 2021-11-17
      • 1970-01-01
      • 2014-12-19
      • 1970-01-01
      相关资源
      最近更新 更多