FreeMarker modify existing file - java

I want to modify an existing file with FreeMarker.
For Ex,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="$(package_name)">
$(activities)
<activity android:name=".map.activity.MapsDemoActivity" />
<activity android:name=".map.activity.MapListActivity" />
<activity android:name=".map.activity.EventsDemoActivity" />
<activity android:name=".map.activity.InfoWindowDemoActivity" />
<activity android:name=".map.activity.CustomMarkerClusteringDemoActivity" />
</manifest>
In above code I want to update the $(package_name) and $(activities).
I'm using org.freemarker:freemarker:2.3.23, Need to do this file modification using Java program.
I know how to create new file using free maker but unable to modify existing file using java program.
Please suggest any solution.

Use merge command in the recipe.xml
<merge from="root/src/AndroidManifest.xml.ftl"
to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />
In root/src/AndroidManifest.xml.ftl
you just specify the elements that you want to insert

Related

Android deep link for OAuth callback URI not working

I'm trying to get an implicit deep link working in a very basic Android app. The goal is to demonstrate a basic OAuth flow with Strava's OAuth server to get access to their API.
Strava automatically whitelists 'localhost' plus any authorization callback domain you enter when you register your 'app'. I'm relying on localhost.
I have an activity plus some fragments. In Android Studio I've added a deep link to a fragment that isn't the initial one. My initial fragment asks for a login name and password then creates an IntentUri to hit the Strava OAuth server. This goes just fine and it passes the callback URI in the OAuth request along with the grant requests. The problem is that the callback URI does not go to the deep link.
I've tried a few combinations for callback URIs:
http://localhost/fibonacci/itemFragment
localhost/fibonacci/itemFragment
myapp://localhost/fibonacci/itemFragment
None of these work (yes I always update the URI in the OAuth request and in xml that describes the deep link at the same time.
the callback URI triggers a request to open the browser.
the Strava site flags the callback uri as invalid.
the callback doesn't seem to occur.
I've also tried to test this by creating a shortcut but in each the browser tries to open the shortcut.
Below are my android manifest file, my shortcuts.xml file and my nav_graph.xml.
I should at least be able to get the shortcut to work even if Strava wouldn't work for whatever reason.
Any help is appreciated.
-------------------AndroidManifest.xml---------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fibonacci">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.Fibonacci">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.Fibonacci.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
</activity>
</application>
</manifest>
----------------------shortcuts.xml-------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="RichShortcut"
android:enabled="true"
android:shortcutShortLabel="#string/static_shortcut_label_short"
android:shortcutLongLabel="#string/static_shortcut_label_long"
android:shortcutDisabledMessage=
"#string/static_shortcut_disabled_message">
<!-- android:icon="#drawable/donut_with_sprinkles">-->
<intent
android:action="android.intent.action.VIEW"
android:data="myapp://localhost/fibonacci/itemFragment" />
</shortcut>
</shortcuts>
--------------------------nav_graph.xml-------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/nav_graph"
app:startDestination="#id/loginFragment">
<fragment
android:id="#+id/FirstFragment"
android:name="com.example.fibonacci.FirstFragment"
android:label="#string/first_fragment_label"
tools:layout="#layout/fragment_first">
<action
android:id="#+id/action_FirstFragment_to_SecondFragment"
app:destination="#id/SecondFragment" />
</fragment>
<fragment
android:id="#+id/SecondFragment"
android:name="com.example.fibonacci.SecondFragment"
android:label="#string/second_fragment_label"
tools:layout="#layout/fragment_second">
<action
android:id="#+id/action_SecondFragment_to_FirstFragment"
app:destination="#id/FirstFragment" />
</fragment>
<fragment
android:id="#+id/loginFragment"
android:name="com.example.fibonacci.ui.login.LoginFragment"
android:label="fragment_login"
tools:layout="#layout/fragment_login" />
<fragment
android:id="#+id/itemFragment"
android:name="com.example.fibonacci.ItemFragment"
android:label="fragment_item_list"
tools:layout="#layout/fragment_item_list">
<deepLink
android:id="#+id/deepLink"
app:uri="myapp://localhost/fibonacci/itemFragment" />
</fragment>
</navigation>
You haven't registered your <deepLink> in your navigation graph in your manifest, so it will never be triggered by the Android OS.
As per the implicit deep link documentation:
To enable implicit deep linking, you must also make additions to your app's manifest.xml file. Add a single <nav-graph> element to an activity that points to an existing navigation graph, as shown in the following example:
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/Theme.Fibonacci.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
<!-- This line is what adds the correct intent filter for your deepLink -->
<nav-graph android:value="#navigation/nav_graph" />
</activity>

How to change application icon for a libgdx android project

I'm trying to change the application icon for a libGDX Project, which I use to develop games. They give a sample ic_launcher.png file to use for setting the app icon, and I'm trying to change it to a different png file. I have specified everything for mdpi, hdpi, xhdpi, xxhdpi, and xxxhdpi, and also an anydpi, but in the AndroidManifest.xml, when I want to change the icon to #drawable/my_launcher, it says that it's an invalid symbol. I have even specified the same lengths and widths as the ic_launcher.png provided. Here is the AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.zunkuftedu.zunkuftlife" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:isGame="true"
android:appCategory="game"
android:label="#string/app_name"
android:theme="#style/GdxTheme" >
<activity
android:name="org.zunkuftedu.zunkuftlife.AndroidLauncher"
android:label="#string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is a libGDX project, so it's different from normal Android Studio projects. Any help will be appreciated.
You shouldn't need to change any code. The android\res folder has folders with icons for different resolutions. You can simply overwrite the ic_launcher.png file in each of those folders with your own custom image (make sure you match the sizes of the existing images in each folder). That's how I've done it and it works great.

how can I assign a widget's settings when I put it on the home screen

How can I open an Activity at the start of a widget to set the required settings?
The idea is to on putting a widget on the main screen to be able to define which i.e. the working database through an index.
To start some activity right after put widget on screen, it's enough to define in the manifest with this intent-filter:
<activity android:name=".ConfigurationActivity">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE"/>
</intent-filter>
</activity>
then add android:configure entry in AppWidgetProviderInfo metadata:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:configure=".ConfigurationActivity"
android:initialLayout="#layout/widget_layout"
android:minHeight="50dp"
android:minWidth="50dp"
android:updatePeriodMillis="50000" />

unable to instantiate activity, ClassNotFoundException

I am developing an android application and after moving around some libraries and adjusting my workspace so that it can sync with github I am now encountering a new error that I have not had in the past. Here is the print out in LogCat.
FATAL EXCEPTION: main
Process: com.exmple.loop, PID:1000
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.loop/com.example.loop.MainActivity}:
java.lang.ClassNotFoundException: Didn't find class "com.example.loop.MainActivity" on
path: DexpathList[[zip file "/data/app/com.example.loop-2.apk"]...
I can supply more if needed. I have checked stackoverflow for solutions and came across some that proved to be unhelpful. Here is the first question I found and here is the second. I tried the helpful solutions for both and nothing seems to work. Similar to the user in the first question, the exception is thrown before any line I write because it is failing to recognize my main activity as a class.
Here is my android manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.loop"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:name="com.example.loop.LoopApplication"
android:icon="#drawable/loop_icon"
android:label="#string/app_name"
android:theme="#style/Theme.Loop" >
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<activity
android:name="com.example.loop.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:theme="#style/AppTheme" android:logo="#drawable/loop_icon" android:name="LoginActivity">
</activity>
</application>
</manifest>
If you look in your logcat it writes:
ComponentInfo{com.exmple.loop/com.example.loop.MainActivity}:
com.exmple.loop
I think it should be com.example.loop
Try remove this line and see if it works:
android:name="com.example.loop.LoopApplication"
Change your activity tag to this and it will run:
<activity
android:name="MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
There are many causes to this. I'll try to name as much as I can.
1.You tryed to add the library in your Java Build Path and not in the Android libraries.
rigt-click on your project> properties > Android. At the bottom there is a place for Android libraries.
2.Right-click your src folder>Build Path > Use as Source Folder
3.Right-click you src folder> Build Path> Configure output folder > set projects default output folder
4.Right-clikc project > Java Build Path > Order and Export > check Android Private Libraries
(You CAN'T have any library in Android Private Libraries and in the outside at the same time)
5.If you have set the project to use Maven and removed it after. Delete the bin folder and the pom.xml
6.Check if you R file is being generated. Delete it and see if it rebuild. If not look in your manifest or xml files for any error.
7.Check that Build Atomatically is checked.
Hope this help, I have had this that problem many times, and each time it was something else.
Good luck!
Edit: there is a 8 cause but this is not your case. Its if you forget to add the class to the manifest.
So after trying every bit of advice given to me I decided to create a new Android Project and copy all the files from the old project the appropriate folder in the new project. While this took a little bit of time that could have probably been shorter if I had messed with the original project some more, ultimately it did fix the error I was encountering and the project now runs.

Custom component namespace invokation

I am having problem with invoking a custom component in my Android projects in Eclipse. It seem that I do not understand how the namespace declarations belong together. I have checked several other threads here at SO, which at first seem to be related, but I cannot solve my problem with these:
android-custom-control-namespace-issue
how-to-pass-custom-component-parameters-in-java-and-xml
android-custom-widget-styles-how-to-put-them-into-a-namespace
I have the following set-up (code is anonymized):
/values/extra_attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="extraComponent">
<attr name="count" format="integer" />
</declare-styleable>
</resources>
/layout/extra_main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/extra_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/extra_main"
/>
</merge>
/com.site.package.extra/extra.java
package com.site.package.extra;
... misc imports...
public class Extra extends FrameLayout
{
... misc code...
}
/com.site.package/main.java (start-up class)
package com.site.package;
... misc imports...
public class Main extends Activity
{
... misc code...
}
/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/main_style"
xmlns:extra="http://schemas.android.com/apk/res/com.site.package" >
<com.site.package.Extra
android:layout_width="wrap_content"
android:layout_height="wrap_content"
extra:count="3">
</com.site.package.Extra>
</RelativeLayout>
The problem I face is; whatever I do, I cannot manage to invoke my custom component. The errors occur in may layout and I have tried to change the following items:
name space declaration
xmlns:extra="http://schemas.android.com/apk/res/com.site.package"
xmlns:extra="http://schemas.android.com/apk/res/com.site.package.extra"
xmlns:extra="http://schemas.android.com/apk/res/extra"
component invokation
<com.site.package.extraComponent />
<com.site.package.Extra.extraComponent />
<extraComponent />
<Extra.extraComponent />
<android.view.ext.extraComponent />
attributes
extra:count="3"
com.site.package.extra:count="3"
In neither case I manage to get any help from intellisense so I am completely lost. I really do not understand how the namespaces are working here and how I should put the code to work.
EDIT :
I forgot to include my AndroidManifest.xml file.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.site.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="#drawable/ic_logo"
android:label="MyApp"
android:name="MyApp" >
<activity
android:label="MyApp"
android:name=".Main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</manifest>
Your custom component is situated in the package com.site.package.extra(from the code you posted) so you could use it in the xml layout with:
<com.site.package.extra.Extra // ... other attributes
or with:
<view class="com.site.package.extra.Extra"
// ... other attributes />
The namespace for the custom attributes:
xmlns:extra="http://schemas.android.com/apk/res/com.site.package"
and to use them:
extra:count="3"

Categories

Resources