【发布时间】:2016-12-30 10:23:05
【问题描述】:
我有一个用于交叉编译我们的代码的 cmake 脚本。为了针对 Android 进行编译,我们使用 Ant,它为原生部分运行 cmake。 Ant 处理 Android/java 部分。由于我们在 java 端有 import android.support.v4.app.ActivityCompat; 之类的导入,因此我将支持库 jar 放在 /my/sdk/folder/extras/android/m2repository/com/android/support/support-v4/24.0.0/support-v4-24.0.0-sources.jar 并将其粘贴在我的 libs 文件夹中。我很确定这在以前的旧 SDK 版本中有效,但现在它不起作用。 (好像jar的文件名也变了,不知道有没有关系。)
问题是,即使我的 libs 文件夹中有 jar,我在构建代码时仍然会遇到很多错误,例如 error: package android.support.v4.content does not exist。难道不应该像将支持库 jar 放到 libs 文件夹中那样简单吗?在包含支持库之前,一切编译正常。
我在这里可能做错了什么?我还可以尝试/检查哪些其他事情?
我尝试了什么:
- 解压缩 jar 以查看 Android 文件在那里。他们是,而且一切似乎都井井有条。
- 从 jar 中取出解压缩的文件并将其添加到我的
src文件夹中,这样,除了我自己的文件之外,我们还有src/com/android/support/v4/...。大部分支持库都编译了,但还有其他错误。这真的是无关紧要的,因为包含一个罐子应该很简单。 - 通过使用
<path>和<fileset>来强制Ant 直接包含jar,请参阅下面build.xml中的尝试。 - 其他各种小东西。
build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project name="myApp" default="debug" xmlns:if="ant:if" xmlns:unless="ant:unless">
<property environment="env" />
<path id="sdk.dir_path">
<pathelement path="${env.ANDROID_SDK}" />
</path>
<path id="ndk.dir_path">
<pathelement path="${env.ANDROID_NDK}" />
</path>
<pathconvert targetos="unix" property="sdk.dir" refid="sdk.dir_path"/>
<pathconvert targetos="unix" property="ndk.dir" refid="ndk.dir_path"/>
<property file="ant.properties" />
<loadproperties srcFile="local.properties" />
<loadproperties srcFile="project.properties" />
<!-- Attempting to force inclusion of jar. Does not help. -->
<property name="project.lib.dir" value="${basedir}/libs"/>
<path id="lib.path">
<fileset dir="${project.lib.dir}" includes="support-v4-24.0.0-sources.jar" />
</path>
<fail
message="sdk.dir is missing. Make sure to generate local.properties using 'android update project'."
unless="sdk.dir"
/>
<target name="clean" depends="android_rules.clean">
<delete dir="obj"/>
<delete dir="libs/x86"/>
<delete dir="libs/armeabi-v7a"/>
<delete dir="build_x86"/>
<delete dir="build_armeabi-v7a"/>
</target>
<!-- Not relevant -->
<target name="build-jni">
<!-- Snip -->
</target>
<target name="-pre-build" depends="build-jni">
</target>
<!-- Include Android build.xml -->
<import file="${sdk.dir}/tools/ant/build.xml"/>
</project>
文件夹结构
.
├── AndroidManifest.xml
├── ant.properties
├── build_cmake.xml
├── build.xml
├── jni
│ ├── Android.mk
│ └── Application.mk
├── libs
│ └── support-v4-24.0.0-sources.jar
├── local.properties
├── project.properties
├── README.txt
├── res
│ └── values
│ └── strings.xml
└── src
└── com
└── mycompany
└── myApp
└── myApp.java
8 directories, 12 files
其他文件并没有什么特别之处——它们只包含普通的东西。无论如何我都会在这里发布它们,以防我错过了一些愚蠢的东西。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.myApp"
android:versionCode="1"
android:versionName="1.0.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:label="@string/app_name"
android:debuggable="true">
<activity
android:name="com.mycompany.myApp.myApp"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
project.properties
target=android-15
local.properties
Empty file
ant.properties
// An attempt to force Ant to include libs folder
jar.libs.dir=libs
jni/Android.mk
// Not really used, cmake sets target platform.
APP_ABI := all
jni/Application.mk
// Not really used, cmake sets target platform.
APP_ABI := x86
strings.xml 不相关,我们需要从myApp.java 知道的唯一一件事是它具有上述包含语句以使用支持库。 build_cmake.xml 只是另一个从 build.xml 调用以运行 cmake 的 ant 脚本。
【问题讨论】: