How to let build.gradle support multi-language? - android

I have a question, I write script "resValue("string", "app_name", "Test")" in the build.gradle file and it occured an error that I have already anounce the same value in the "string.xml" file as "Test".
Here is the question, how to make gradle file support Chinese and English ?
Thank you !

You need to use an encoding like UTF-8 while building the gradle by setting -
-encoding UTF-8 -docencoding utf-8 -charset utf-8
Or something like this -
android {
...
compileOptions.encoding = 'ISO-8859-1'
}
Or through the settings -
File -> Settings -> Editor -> File encodings -> set UTF-8 for
IDE Encoding
Project Encoding
Default encoding propertie file
Press OK
Rebuild Project
Build -> Rebuild project

Related

Android studio doesn't recognize UTF-8 encoding

The problem is that when I use UTF-8 characters like ā, ē, š Android Studio translates them to some different symbols.
This is Log.d() that I'm passing:
Log.d("Test", "ššāā");
The result that I get is
D/Test: ЕЎЕЎДЃДЃ
And that is not only for Logcat. It passes same thing everywhere I try to use these characters.
Check your gradle file for any encodings like
compileOptions.encoding = 'windows-1251'
Can you try this:
File -> Other Settings -> Default Settings, then search for "File Encodings", change Project Encoding to UTF-8
Make sure all the below are set to UTF-8:
Global Encoding (Settings > Editor > File Encodings)
Project Encoding (Settings > Editor > File Encodings)
Default encoding for properties files (Settings > Editor > File Encodings)
The actual file encoding at the bottom right in Android Studio

How to change the name of the apk file?

I build apk file with help of https://build.phonegap.com/apps/ portal evrey time I build debug or release apk file the names are:
______debug.apk or ______release.apk
I want to change the name of the release apk to something more significant, for example MyProject-v2.apk.
Any idea how can I change the name of the the apk file when it build?
No, sadly you cannot control the file name in the config.xml, but the file name doesn't matter. In the config.xml, you specify the name of your app as it will appear to the users on their mobiles, and that's what matters.
If you like to give the file a specific name for your own file management (which is what I do too), you'll have to manually rename the file every time you create a new build.
The thing you should know is that APK names does not matter in its file name, What matters is what you said in the AndroidManifest.xml in the Application label:
android:label="#string/app_name"
This is the only app name and it can even be different from package name so the apk being called ______debug.apk or ______release.apk doesnt matter it is just to differenciate if it is debug or release. You can just rename it just like any file in your computer even Michael.apk. And still all android phones will ignore the file names and go get the label at android:label="#string/app_name" so just copy the file and change it to anything you want and it works.
You can rename it as a file.
The installed apk name is same as what you defined in manifest.
You can configure naming of you builds in gradle configuration
Add this to your android{} close in your module's build.gradle:
// determine the apk file name when produced
applicationVariants.all { variant ->
variant.outputs.each { output ->
output.outputFile = new File(
output.outputFile.parent,
output.outputFile.name.replace("app", "YourAppNameHere-V${variant.versionName}"))
}
}
the V is for the version number.
Inside modules build.gradle file, add following line to android { defaultConfig { X } }
setProperty('archivesBaseName', "YourAppName-$versionCode")
In my case I just renamed the file app_release.apk to appname.apk
and it works for me.
Same for app_debug.apk to appname_debug.apk
In project directory:
While Installing
You cannot control the file name in the config.xml. But if you have a self signed certificate and want to rename the apk, you can do it with zipalign:
zipalign.exe -v 4 platforms\android\app\build\outputs\apk\release\app-release-unsigned.apk platforms\android\app\build\outputs\apk\release\YouyAppName-v2.apk

Referencing a japanese/chinese string in gradle.properties from strings.xml results in an unknown behaviour

I'm referencing a gradle variable from a string.xml the following way:
gradle.properties
APP_NAME_JAPANESE="日本語"
build.gradle
resValue 'string', 'APP_NAME_JAPANESE', APP_NAME_JAPANESE
strings.xml
<string name="app_name">#string/APP_NAME_JAPANESE</string>
The generated file generated.xml have the following string
<string name="APP_NAME_JA" translatable="false">"ã¢ãã¿ã¼ãã«ã¹"</string>
As we can see charaters are unreadables.
All files are UTF-8 encoded.
If instead of putting the variable in the gradle.properties I put it directly on build.gradle resValue 'string', 'APP_NAME_JAPANESE', "日本語", everything is working properly.
Could this be a bug in the gradle system?
Java properties files by definition are not UTF-8 encoded, unless you explicitly parse them as UTF-8 files.
They are by default ISO-8859-1 encoded with unicode escapes for characters out of that range.
Fix your properties file and everything will work like a charm.
As you are using android I guess you are using Android Studio which is just a specialized IntelliJ IDEA. There you can change in the settings the encoding for properties files to ISO-8859-1 with transparent native-to-ascii escaping. This allows you to see the UTF-8 glyphs in the editor, but the file will be saved automatically with the unicode escapes as necessary. Alternatively you can use the native2ascii tool on the UTF-8 encoded properties file to do that transformation before you start your actual build. But I really recommend using the proper encoding that most tools that can deal with property files are expecting.
I've tried changing the encoding but, still the same issue is happening.
Finally I decided to set the strings in base64 format and then decode the strings in the build.gradle like this
resValue 'string', 'APP_NAME_JA', new String(APP_NAME_JA.decodeBase64(), "UTF-8")

Freemarker template for gradle build files

Hi I'm trying to make a build.gradle file from a freemarker template and I'm running in to small problem both Freemarker and de gradle DSL support ${someString}
So somewhere in the gradle script I have field/variable definition.
For instance I define a String like below:
def gitSha = 'git rev-parse --short HEAD'.execute([], project.rootDir).text.trim()
And would use that somewhere else in the gradle file like so:
filename = "new-archive-${gitSha}.jar"
But then when I generate the gradle file with Freemarker I get the error gitSha is null or not defined in the data-model. Is there a way to make a distinction between ${gitSha} which is for the gradle DSL and the other ${someStrings} which are part of the Freemarker template?
In principle the correct solution would be to use something else instead of ${} in some of the two languages (let's say, #{}). But at least in Freemarker that's not configurable. But it could be emulated (kind of...), if you write a custom TempalateLoader that just wraps another TemplateLoader, but filters the Reader so that #{-s are replaced with ${-s, and ${ are replaced with #{. And then, the same transformation has to be applied to the Writer where Freemarker writes its output into, so the #{ in the output are replaced with ${-s. (I hope BTW that sooner or later Freemarker will have a configurable interpolation syntax, because this issue with ${ comes up quite often, but that's the future...)

android gradle mergeDebugAssets

android project with files in assets, these file need encrypt before generator apk
every times i changed some file in assets,
i need copy out these file , encrypt ,then copy into assets
what i want is:
keep file in assets not encypt (can edit it conveniently) ,
but file in .apk encrypted
encrypt work do automatically by gradle.build
my basic idea is thad add some task before mergeDebugAssets (or mergeReleaseAssets)
before mergeDebugAssets, i replace all file in file://$MODULE_DIR$/build/assets
code like below
task processAssetFile {
// code : replace file in build/assets
}
mergeDebugAssets.dependsOn processAssetFile
the problem is
mergeDebugAssets is not available in gradle.build
error log below:
Could not find property 'mergeDebugAssets' on project ':gradle'.
so is there some idea can achieve my goals ?
android studio ver :0.52
You can always find the task by its name.
I would also suggest that you want to perform encryption for other build variants (makes sense for release even more than for debug).
This requires a little bit of coding since you need to create a task for each variant, too.
It is best to create those tasks dynamically so you don't forget a variant.
Say you're encrypting with some command-line tool, the code you need to add could look like this:
android.applicationVariants.all { variant ->
String suffix = variant.variantData.name.capitalize()
Task mergeAssetsTask = tasks.findByName("merge${suffix}Assets")
Task processAssetFileTask = tasks.create(name: "process${suffix}AssetFile", type: Exec)
processAssetFileTask.commandLine "path/to/your/encryption/tool",
"--input-file=${inputFilePath}",
"--output-file=${outputFilePath}"
mergeAssetsTask.dependsOn processAssetFileTask
}

Categories

Resources