Error with build.xml for unrecognized directories in res/ - android

I am trying to port an existing C++ app to android using JNI. However, after generating the Build.xml file from the AndroidManifest.xml and attempting to build with it, I get the following (relevant) output:
[aapt] Generating resource IDs...
[aapt] invalid resource directory name: /{Project Directory}/res/audio
[aapt] invalid resource directory name: /{Project Directory}/res/img
BUILD FAILED
/{Android SDK Dir}/tools/ant/build.xml:560: The following error occurred while executing this line:
/{Android SDK Dir}/tools/ant/build.xml:589: null returned: 1
These are existing directories in a directory already named "res" by coincidence, not meant to have a semantic meaning for Java. If I remove those from res/ then it builds just fine. So my question is-- what is the best way to organize this (or tell ant to ignore unrecognized directories)? Res is already a very large directory structure with mainly .png .mp3 and text files.

If renaming them is out of the question for whatever reason, you could override -package-resources in the build.xml and customize aapt, specifying a new location for res/ along with various other options to handle issues that might come up (Then aapt would be the part to research).
You can inspect android-sdk/tools/ant/build.xml for what that target already does, but essentially you would simply paste in the following to your own build.xml
<target name="-package-resources">
<echo>Packaging resources</echo>
<aapt executable="${aapt}"
command="package"
verbose="${verbose}"
versioncode="${version.code}"
manifest="AndroidManifest.xml"
assets="${asset.absolute.dir}"
androidjar="${android.jar}"
apkfolder="${out.absolute.dir}"
resourcefilename="${resource.package.file.name}"
projectLibrariesResName="${project.libraries.res}"
projectLibrariesPackageName="${project.libraries.package}"
resourcefilter="${aapt.resource.filter}">
<res path="${resource.absolute.dir}"/>
</aapt>
</target>
and customize to your needs.
Also notice that it pulls the res path from resource.absolute.dir, so if thats all you really need to do, then you could set that in ant.properties or whatever the default properties file is in the sdk tools are that you're using. Just inspect the build.xml to see or add a new loadproperties line pointing to your own. IIRC, resource.absolute.dir gets set based on resource.dir, so you might have to try each but in the properties file you would just set
resource.dir=new/path/to/res

audio goes in {Project Directory}/res/raw and images go in {Project Directory}/res/drawable
This page may be of some use to you:
http://developer.android.com/guide/topics/resources/index.html

Related

Android resource linking failed AAPT: unknown option '--proguard-minimal-keep-rules'

My Android Studio was working fine, everything was good but after updating Android Studio to the latest version and updating gradle I got the following error
Could you please provide the solution, thank you
Task :app:processDebugResources FAILED
AGPBI: {"kind":"error","text":"Android resource linking failed","sources":[{}],"original":"AAPT: unknown option '--proguard-minimal-keep-rules'.\n\naapt2 link [options] -o arg --manifest arg files...\n\nOptions:\n -o arg Output path.\n --manifest arg Path to the Android manifest to build.\n -I arg Adds an Android APK to link against.\n -A arg An assets directory to include in the APK. These are unprocessed.\n -R arg Compilation unit to link, using overlay semantics.\n The last conflicting resource given takes precedence.\n --package-id arg Specify the package ID to use for this app. Must be greater or equal to\n 0x7f and can't be used with --static-lib or --shared-lib.\n --java arg Directory in which to generate R.java.\n --proguard arg Output file for generated Proguard rules.\n --proguard-main-dex arg Output file for generated Proguard rules for the main dex.\n --proguard-conditional-keep-rules Generate conditional Proguard keep rules.\n --no-auto-version Disables automatic style and layout SDK versioning.\n --no-version-vectors Disables automatic versioning of vector drawables. Use this only\n when building with vector drawable support library.\n --no-version-transitions Disables automatic versioning of transition resources. Use this only\n when building with transition support library.\n --no-resource-deduping Disables automatic deduping of resources with\n identical values across compatible configurations.\n --enable-sparse-encoding This decreases APK size at the cost of resource retrieval performance.\n -x Legacy flag that specifies to use the package identifier 0x01.\n -z Require localization of strings marked 'suggested'.\n -c arg Comma separated list of configurations to include. The default\n is all configurations.\n --preferred-density arg Selects the closest matching density and strips out all others.\n --product arg Comma separated list of product names to keep\n --output-to-dir Outputs the APK contents to a directory specified by -o.\n --no-xml-namespaces Removes XML namespace prefix and URI information\n from AndroidManifest.xml and XML binaries in res/*.\n --min-sdk-version arg Default minimum SDK version to use for AndroidManifest.xml.\n --target-sdk-version arg Default target SDK version to use for AndroidManifest.xml.\n --version-code arg Version code (integer) to inject into the AndroidManifest.xml if none is\n present.\n --version-code-major arg Version code major (integer) to inject into the AndroidManifest.xml if none is\n present.\n --version-name arg Version name to inject into the AndroidManifest.xml if none is present.\n --replace-version If --version-code and/or --version-name are specified, these\n values will replace any value already in the manifest. By\n default, nothing is changed if the manifest already defines\n these attributes.\n --compile-sdk-version-code arg Version code (integer) to inject into the AndroidManifest.xml if none is\n present.\n --compile-sdk-version-name arg Version name to inject into the AndroidManifest.xml if none is present.\n --shared-lib Generates a shared Android runtime library.\n --static-lib Generate a static Android library.\n --proto-format Generates compiled resources in Protobuf format.\n Suitable as input to the bundle tool for generating an App Bundle.\n --no-static-lib-packages Merge all library resources under the app's package.\n --non-final-ids Generates R.java without the final modifier. This is implied when\n --static-lib is specified.\n --stable-ids arg File containing a list of name to ID mapping.\n --emit-ids arg Emit a file at the given path with a list of name to ID mappings,\n suitable for use with --stable-ids.\n --private-symbols arg Package name to use when generating R.java for private symbols.\n If not specified, public and private symbols will use the application's\n package name.\n --custom-package arg Custom Java package under which to generate R.java.\n --extra-packages arg Generate the same R.java but with different package names.\n --add-javadoc-annotation arg Adds a JavaDoc annotation to all generated Java classes.\n --output-text-symbols arg Generates a text file containing the resource symbols of the R class in\n the specified folder.\n --allow-reserved-package-id Allows the use of a reserved package ID. This should on be used for\n packages with a pre-O min-sdk\n \n --auto-add-overlay Allows the addition of new resources in overlays without\n tags.\n --rename-manifest-package arg Renames the package in AndroidManifest.xml.\n --rename-instrumentation-target-package arg Changes the name of the target package for instrumentation. Most useful\n when used in conjunction with --rename-manifest-package.\n -0 arg File extensions not to compress.\n --no-compress Do not compress any resources.\n --warn-manifest-validation Treat manifest validation errors as warnings.\n --split arg Split resources matching a set of configs out to a Split APK.\n Syntax: path/to/output.apk:[,[...]].\n On Windows, use a semicolon ';' separator instead.\n -v Enables verbose logging.\n --debug-mode Inserts android:debuggable=\"true\" in to the application node of the\n manifest, making the application debuggable even on production devices.\n --strict-visibility Do not allow overlays with different visibility levels.\n -h Displays this help menu\n\n ","tool":"AAPT"}
AAPT: unknown option '--proguard-minimal-keep-rules'.
aapt2 link [options] -o arg --manifest arg files...
Options:
-o arg Output path.
--manifest arg Path to the Android manifest to build.
-I arg Adds an Android APK to link against.
-A arg An assets directory to include in the APK. These are unprocessed.
-R arg Compilation unit to link, using overlay semantics.
The last conflicting resource given takes precedence.
--package-id arg Specify the package ID to use for this app. Must be greater or equal to
0x7f and can't be used with --static-lib or --shared-lib.
--java arg Directory in which to generate R.java.
--proguard arg Output file for generated Proguard rules.
--proguard-main-dex arg Output file for generated Proguard rules for the main dex.
--proguard-conditional-keep-rules Generate conditional Proguard keep rules.
--no-auto-version Disables automatic style and layout SDK versioning.
--no-version-vectors Disables automatic versioning of vector drawables. Use this only
when building with vector drawable support library.
--no-version-transitions Disables automatic versioning of transition resources. Use this only
when building with transition support library.
--no-resource-deduping Disables automatic deduping of resources with
identical values across compatible configurations.
--enable-sparse-encoding This decreases APK size at the cost of resource retrieval performance.
-x Legacy flag that specifies to use the package identifier 0x01.
-z Require localization of strings marked 'suggested'.
-c arg Comma separated list of configurations to include. The default
is all configurations.
--preferred-density arg Selects the closest matching density and strips out all others.
--product arg Comma separated list of product names to keep
--output-to-dir Outputs the APK contents to a directory specified by -o.
--no-xml-namespaces Removes XML namespace prefix and URI information
from AndroidManifest.xml and XML binaries in res/*.
--min-sdk-version arg Default minimum SDK version to use for AndroidManifest.xml.
--target-sdk-version arg Default target SDK version to use for AndroidManifest.xml.
--version-code arg Version code (integer) to inject into the AndroidManifest.xml if none is
present.
--version-code-major arg Version code major (integer) to inject into the AndroidManifest.xml if none is
present.
--version-name arg Version name to inject into the AndroidManifest.xml if none is present.
--replace-version If --version-code and/or --version-name are specified, these
values will replace any value already in the manifest. By
default, nothing is changed if the manifest already defines
these attributes.
--compile-sdk-version-code arg Version code (integer) to inject into the AndroidManifest.xml if none is
present.
--compile-sdk-version-name arg Version name to inject into the AndroidManifest.xml if none is present.
--shared-lib Generates a shared Android runtime library.
--static-lib Generate a static Android library.
--proto-format Generates compiled resources in Protobuf format.
Suitable as input to the bundle tool for generating an App Bundle.
--no-static-lib-packages Merge all library resources under the app's package.
--non-final-ids Generates R.java without the final modifier. This is implied when
--static-lib is specified.
--stable-ids arg File containing a list of name to ID mapping.
--emit-ids arg Emit a file at the given path with a list of name to ID mappings,
suitable for use with --stable-ids.
--private-symbols arg Package name to use when generating R.java for private symbols.
If not specified, public and private symbols will use the application's
package name.
--custom-package arg Custom Java package under which to generate R.java.
--extra-packages arg Generate the same R.java but with different package names.
--add-javadoc-annotation arg Adds a JavaDoc annotation to all generated Java classes.
--output-text-symbols arg Generates a text file containing the resource symbols of the R class in
the specified folder.
--allow-reserved-package-id Allows the use of a reserved package ID. This should on be used for
packages with a pre-O min-sdk
--auto-add-overlay Allows the addition of new resources in overlays without
tags.
--rename-manifest-package arg Renames the package in AndroidManifest.xml.
--rename-instrumentation-target-package arg Changes the name of the target package for instrumentation. Most useful
when used in conjunction with --rename-manifest-package.
-0 arg File extensions not to compress.
--no-compress Do not compress any resources.
--warn-manifest-validation Treat manifest validation errors as warnings.
--split arg Split resources matching a set of configs out to a Split APK.
Syntax: path/to/output.apk:[,[...]].
On Windows, use a semicolon ';' separator instead.
-v Enables verbose logging.
--debug-mode Inserts android:debuggable="true" in to the application node of the
manifest, making the application debuggable even on production devices.
--strict-visibility Do not allow overlays with different visibility levels.
-h Displays this help menu
I had the same problem after updating AS and grade plugin, the problem is with Gradle version 6.1.1 try downgrading to the latest stable one from the project structure till the release of another Gradle version.
Go to -
file -> project structure
This is a common issue
Try these basic solutions:
(Check if it works after trying every solution)
File->Invalidate Cache and restart
Build -> clean project.
Delete the build folder from your code.
Make sure that you have updated all dependencies in the project-level Gradle
file.

Why is aapt not adding `<values/>` in the apk?

I am using Titanium which is a cross-platform development Tool. Titanium uses it own building script to create the apk.
I have a Titanium application that uses a native module : a Java library that makes VOIP calls. The Java application works, and when I create a Titanium application with an empty module, it works.
But I have a build problem using a module based on the VOIP module inside the Titanium application : some Resources are missing in the apk.
First the build system merges some Resources from our Titanium application and some native android modules and put them in a directory build. I have for exemple : build/android/res/values/strings.xml file where all keys and values seems ok and valid xml.
Then the build calls aapt and create the apk using also the Titanium Resources for library widgets (ex: strings for notifications). And in this apk, the build/android/res/values/strings.xml is missing. It doesn't exists. The application starts, the VOIP service starts, and then it's crashes as soon that a required missing Resource is called.
I can prove with aapt list or unzipping the apk that strings.xml is missing.
Looking deep in the titanium javascript build file, I see this command is executed :
aapt "package" "-f" "-m" "-J" "/Users/nicorama/ti-voip/build/android/gen"
"-M" "/Users/nicorama/ti-voip/project/build/android/AndroidManifest.xml"
"-S" "/project/build/android/res"
"-S" "/var/folders/6f/twxz46614h7_q/res"
"-S" "/var/folders/6f/twxz46614h7_q/res" ....
The /var/folders/... are files for Titanium widgets. Compilation of the apk fails if I remove them.
I have executed this command outside the build, adding -v for verbose mode and saving result into a log.txt. I'm searching in this file for strings.xml and I find :
Found 18 custom asset files in /Users/nicorama/ti-voip/build/android/bin/assets
Configurations:
(default)
v11
v14
....
Src: () /var/folders/6f/twxz46614h7_q/res/values/ids.xml
values/strings.xml
Src: () /var/folders/6f/twxz46614h7_q/res/values/strings.xml
Src: (af) /var/folders/6f/twxz46614h7_q/res/values-af/strings.xml
Src: (am) /var/folders/6f/twxz46614h7_q/res/values-am/strings.xml
...
But nothing about my /Users/nicorama/ti-voip/build/android/res/values/strings.xml folder which is first in the appt command.
I do have all references needed for images or other xml files thought :
(new resource id hidden from /Users/nicorama/ti-voip/build/android/res/layout/hidden.xml)
(new resource id launcher from /Users/nicorama/ti-voip/build/android/res/layout/launcher.xml)
Any idea where and why this strings.xml has disappeared ?
aapt will merge all strings.xml "values" into resources.arsc, which is why you do not see the file strings.xml in the apk. I double checked this with few of my apks, i do not see the strings.xml inside the apk.
If you would like to see the string values that were packaged with your 'apk', run the below command it will dump all string values.
aapt.exe d strings myapp.apk
Verify some of the string values you defined in your app, are listed in the above string dump. If not, try passing in absolute path to the "res" folder "/Users/nicorama/ti-voip/project/build/android/res" at the beginning:
aapt "package" "-f" "-m" "-J" "/Users/nicorama/ti-voip/build/android/gen"
"-M" "/Users/nicorama/ti-voip/project/build/android/AndroidManifest.xml"
"-S" "/Users/nicorama/ti-voip/project/build/android/res"
"-S" "/var/folders/6f/twxz46614h7_q/res"
"-S" "/var/folders/6f/twxz46614h7_q/res" ....
the new apk should have the xml values, test again with
aapt.exe d strings myapp.apk
to make sure your strings.xml values are in the above string dump.

Cordova Build - ignore files

When compiling a cordova application every single file in my /www folder gets copied to the assets/www folder(android) but I'd like to customize what files are copied. I use several pseudo languages like CoffeeScript, Jade or Stylus which are auto-compiled by my IDE and they shouldn't be shipped into the final application.
With the help of this article, I found that you can create/edit the platform/android/ant.properties, and add the following line to it:
aapt.ignore.assets=!*.map:!thumbs.db:!.git:.*:*~
With this line, any file or directory that matches one of these patterns will not be included in the .apk file:
*.map
thumbs.db
.git
.*
*~
I found that editing the platforms/android/build.xml won't work because it's overwritten each time the build is invoked; also, creating a build.xml file in the root of the project didn't work.
The rules for this property are the following, taken from $ANDROID_HOME/tools/ant/build.xml:
<!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"
Overall patterns syntax is:
[!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...
- The first character flag ! avoids printing a warning.
- Pattern can have the flag "<dir>" to match only directories
or "<file>" to match only files. Default is to match both.
- Match is not case-sensitive.
-->
<property name="aapt.ignore.assets" value="" />
Hidden (.*) folder & files will be ignored by default
All hidden files and folders will be ignored while build for example .git/ & .gitignore
To hide : Rename the folder by a . (dot) prepended to the folder/file name.
Use Terminal in Linux/Mac to rename the folder.
mv dev .dev
Use cmd in Windows
ren dev .dev
I do not know any way how to filter files for cordova, but I can describe you approach that we use in our projects.
We are using gruntjs with phonegap plugin. We configure it to use prebuilt folder (which contains only necessary files and folders) and it:
creates cordova/phonegap project
adds plugins,platforms
builds native applicaiton
launches native application on emulator
Main thing in this approach is that cordova/phonegap project (directory with .cordova, platforms and www folders) is just a build artefact.
Here is relevant part of our Gruntfile.js as an example:
phonegap : {
config : {
root : './out/dist',
config : {
template : './config.tpl.xml',
data: {
id: pkg.id,
version: pkg.version,
name: pkg.name,
author : pkg.author
}
},
path : 'out/phonegap',
plugins : [
// PHONEGAP OFFICIAL PLUGINS
'org.apache.cordova.globalization',
'org.apache.cordova.network-information',
'org.apache.cordova.splashscreen',
//THIRD-PARTY PLUGINS
'de.appplant.cordova.plugin.local-notification'
],
platforms : [
'android'
],
maxBuffer : 200, // You may need to raise this for iOS.
verbose : false,
releases : 'out/releases',
releaseName : function() {
return pkg.name + '-v' + pkg.version;
},
// Android-only integer version to increase with each release.
// See http://developer.android.com/tools/publishing/versioning.html
versionCode : function() {
return 1;
}
}
}
Note, out/dist is generated by previous build step and contains concatenated and minified version of code.
I've created a custom script to ignore files/folders which works not just for android:
Add this to your config.xml:
<ignore entries="lang,foo/bar,ignore/asdf.html" />
And you need two more files located in hooks/before_build and hooks/after_build folders.
I highly recommend cordova-plugin-exclude-files. You simply specify files to exclude as pattern attributes of exclude-files tags in your config.xml file.
For example, to exclude all .scss files:
<exclude-files pattern="**/*.scss" />
And to exclude all files with the string "ios-only" on the Android platform only:
<platform name="android">
<exclude-files pattern="ios-only" /> </platform>
This is what I'm doing. I've moved the src files for the app that was in www folder to another folder created in root - web-app. Next, the following script was added in package.json :
This surely needs to be refined, but am working with it as makeshift arrangement.
Good Luck...

How to set strings.xml values at build time?

I'm building my Android application with Ant, and would like to set one of the values in my strings.xml at build time. For example, I could use
<string name="app_name">MyApp-DEBUG</string>
with a debug build, or I could use
<string name="app_name">MyApp<string>
for a release build. Is this possible?
There are two tasks in Ant that can help:
First is the <replace>. You give it a file name that contains parameters that can be replaced, and you give the <replace> task the values for those parameters. It replaces them in the file. I don't like this task because it's used to replace stuff that is under version control, and if you're not careful, you can end up changing the file without meaning to.
settings.xml
<settings>
<properties>
<property name="server" value="#SERVER#"/>
</properties>'
</settings>
Replace Task
<replace file="settings.xml">
<replacetoken token="#SERVER#" value="google.com"/>
</replace>
I've seen plenty of version control repositories where revision #3 of the replaced file was an accidental checkin of the the file with the replaced parameters (and not realizing it until the next release when the parameters didn't get changed). Then version #4 is a duplicate of version #2 which had the replacement parameters. Followed by a bad version #5, followed by a version #6 which restores the file, and on and on.
My preferred method is to copy the file over to another directory, and use <filterset>/<filter> tokens to change the file while being copied:
<copy todir="${target.dir}"
file="settings.xml">
<filterset>
<filter token="SERVER" value="google"/>
</filterset>
</copy>
Both can use a property file instead of specifying individual tokens. The <copy>/<filterset> pair can take a fileset of files and replace a bunch of tokens at once. (Be careful not to pass it a binary file!).
try this code, it works for me
<target name="app-name-debug">
<replaceregexp file="res/values/strings.xml" match='name="app_name"(.*)'
replace='name="app_name">MyApp-DEBUG<\/string>'/>
</target>
<target name="app-name-release">
<replaceregexp file="res/values/strings.xml" match='name="app_name"(.*)'
replace='name="app_name">MyApp<\/string>'/>
</target>

aapt basename attribute

I upgraded my SDK to 2.3 and my build scripts start to fail. I am getting this error:
build.xml:363: aaptexec doesn't support the "basename" attribute
For the following line in my build script:
<aapt executable="${aapt}" command="package" manifest="AndroidManifest.xml" resources="${resource.absolute.dir}" assets="${asset.absolute.dir}" androidjar="${android.jar}" outfolder="${out.absolute.dir}" basename="${ant.project.name}" />
I do not know what changed in aapt but apparently basename is not there any more. Can you tell me what should I use instead?
I had similar problems when upgrading from 2.1 to 2.3 with symbolic names changing in the built in 'rules' xmls. 2.1 seemed to use ant_rules_3.xml, now SDK 2.3 uses main_rules.xml as its starting point. If, like me, you had customised your build.xml based on the targets in the rules file, you will probably find it easier to start again by creating a sample project as described in : link text
then adapting you build.xml to override some or all of the targets that are listed in main_rules.xml. (This is in the sdk\tools\ant folder). I found this process to be reasonably quick, and for simple projects the generated files worked 'out of the box' with Ant. (I did have to change the build.xml a lot for a project which contained a second source folder containing .aidl files, as the main_rules.xml couldn't cope with it)
There are two targets which refer to aapt in the main_rules.xml, it's probably this one that you need to mimic:
<target name="-package-resources">
<echo>Packaging resources</echo>
<aapt executable="${aapt}"
command="package"
versioncode="${version.code}"
debug="${build.packaging.debug}"
manifest="AndroidManifest.xml"
assets="${asset.absolute.dir}"
androidjar="${android.jar}"
apkfolder="${out.absolute.dir}"
resourcefilename="${resource.package.file.name}"
resourcefilter="${aapt.resource.filter}">
<res path="${resource.absolute.dir}" />
<!-- <nocompress /> forces no compression on any files in assets or res/raw -->
<!-- <nocompress extension="xml" /> forces no compression on specific file extensions in assets and res/raw -->
</aapt>
</target>
I think the resourcefilename is the name of the generated apk. My build scripts generate the apks and put them in the bin directory OK, without my naming the resourcefilename explicitly.
My build.properties looks like:
#-----------------------------------------------
# The ONLY reference to the project's main base
#
projectname=MapProject1.2
#
#-----------------------------------------------
workspace.dir=/dev/projects/EclipseHelios/AndroidWorkspace
ant.project.name=${projectname}
outbasebase.dir=/dev/projects/AntBuilds
base.dir=${workspace.dir}/${projectname}
common_src=/dev/projects/CommonSource121210/GridSeviceAndUseGridService
source.dir=${base.dir}/src
outbase.dir=${outbasebase.dir}/${projectname}
out.dir=${outbase.dir}/bin
key.store=c:/users/me/my-release-key.keystore
key.alias=release_alias
key.store.password=*************
key.alias.password=*************
layout.dir=${base.dir}/res/layout
(Apart from the passwords of course!)
I did comment out the projectname in the buid.xml, so that it would pick it up from the build.properties.

Categories

Resources