Friday, 7 August 2015

How to Get Started in Android Studio



How to Get Started in Android Studio

Prerequisites

    Running Android Studio 1.0 or higher
    Developing for Android level 9 or higher

In order to complete the Get Started guide, you'll need to have Android Studio installed on your development machine. If you don't already have it, see the Android Studio site for instructions on how to download everything you need to get up and running.

Creating a new project

In this step, we'll create a brand new project in Android Studio to use for our example. If you don't already have Studio running, go ahead and open it now.

Start the new project wizard
 
If you see the above welcome screen, select New Project. Otherwise, select File > New Project from the menu. This will bring up the new project wizard:

Name your project
 
Enter "BannerExample" as the app name, and whatever company domain you use for your apps. Android Studio will automatically determine a good project location, but feel free to change it if you'd like.

Set the required SDK version
 
On the next screen, select Phone and Tablet for the form factor and a minimum platform SDK version of 9. That's the minimum version supported by the Google Mobile Ads SDK.

Add your main activity
 
We're keeping it simple for this example, so on this screen select Blank Activity.

Name your activity
 
On this screen you have the option of choosing names for the app's activity and its related resources. We'll use the default names for this example, so just click the Finish button.

Compile your new project

After clicking Finish, you'll have a working project with a single activity. Try compiling and running it (select Run 'app' from the Run menu). You should see a "Hello world!" message on an otherwise empty gray screen. Don't worry, we'll add some more content in the next steps.

If you're new to developing Android apps, take a look at the tutorials for USB Debugging and Using Virtual Devices. You'll need to do one of those two things in order to run your new app and see what it looks like.

Download the Google Repository

The Android SDK Manager is a package manager for Android. You can use it to download those parts of the SDK you'd like to incorporate into your apps. This can include libraries of code, but also things like build tools and system images for emulators.

In this case, we're using the SDK Manager to download the Google Repository, which has a number of prebuilt artifacts that Android Studio can easily include in its projects. The particular artifact we're interested in, play-services-ads, contains resources and compiled Java code for the Google Play services Ads SDK. It sounds complicated, but as you'll see, the SDK Manager and gradle combine to make downloading and including the right code a straightforward process.

The Google Repository contains gradle artifacts for the Google Mobile Ads SDK, which your app can use to request and display ads. Make sure that you have the latest version by opening up your SDK Manager. You can do this by selecting Tools > Android > SDK Manager.

The SDK Manager
 
In the Android SDK Manager window, select Google Repository under the Extras folder, then press Install Packages and accept the licenses to download. If the Install Packages button is disabled, don't worry. That just means you already have the latest version, so there's nothing else you need to do in the SDK Manager.

Configuring gradle

Gradle is a free and open source build system that incorporates a number of handy features for Android developers (such as dependency management, as you're about to see). If you haven't used it before, visit gradle.org for more information on how gradle works.

In this step, you're going to modify a build.gradle file. Your new project has two of these. One is a project-level file in the root BannerExample folder, which has settings that affect the project as a whole. The other is an app-level one that has settings for your project's single app. It's saved in the BannerExample/app folder. Other projects that contain multiple apps will have a separate app-level build.gradle file for each one.

Now that the Google Repository is installed, you need to update your app to reference the Google Play Services SDK inside it. You can do this by adding a line to the dependencies in your app-level build.gradle file. Look for it in the BannerExample/app/ folder and open it.

build.gradle

...
dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:22.2.0'
        compile 'com.google.android.gms:play-services-ads:7.5.0'
    }
...

Update the dependencies section to include the latest Google Play services SDK.

You may see a warning message across the top of the Android Studio window indicating that gradle needs to perform a gradle sync. If that's the case, click Sync Now to do so. Gradle will refresh your project's libraries to include the dependency you just added.

Try rebuilding your project (Run 'app' in the Run menu) to make sure everything compiles correctly. You won't see any changes, but including Google Play services is the first step toward getting ads into your app.

Modify the manifest file

Every Android app uses a file called a manifest to inform the Android system about itself. The information typically found in these files includes things like the permissions required by the app, the activities it contains, and so on. For more details on manifests and how they work, check out the App Manifest Intro.

Now that you have a working app that includes Google Play services, it's time to modify the app manifest file to include the permissions, version number, and activity definition that the Mobile Ads SDK requires. Open BannerExample/app/src/main/AndroidManifest.xml for editing.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.example.bannerexample" >

    <!-- Include required permissions for Google Mobile Ads to run-->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!--This meta-data tag is required to use Google Play Services.-->
        <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
        <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>
        <!--Include the AdActivity configChanges and theme. -->
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>

There are three changes you need to make:


  1.  Add two <uses-permission> tags for INTERNET and ACCESS_NETWORK_STATE. The tag for INTERNET is required and used to access the Internet to make ad requests. ACCESS_NETWORK_STATE is optional, and used to check if an internet connection is available prior to making an ad request
  2. Add a <meta-data> tag that references the Google Play services version. This lets Android know which version of the service your app expects to use.
  3. Add an <activity> element with configChanges and theme attributes. This activity is used by the SDK when banners are clicked or interstitials are presented, and like any other activity must be declared in the manifest before being presented.

Go ahead and rebuild the project to make sure everything has been done correctly. You should still see the same "Hello world!" message for now. By configuring the App Manifest correctly, though, you've given your app the ability to use Mobile Ads.

Give your app an Ad Unit ID

An Ad Unit ID is a unique identifier given to the places in your app where ads are displayed. If you had an app with two activities, for example, each displaying a banner, you'd have two ad units, each with its own ID. AdMob ad unit IDs have the form ca-app-pub-XXXXXXXXXXXXXXXX/NNNNNNNNNN

In order for your new app to display an ad, it needs to include an Ad Unit ID. Open your app's string resource file, which is found at BannerExample/app/src/main/res/values/strings.xml.

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">My Application</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
</resources>

Add a new <string> tag as shown. Note that the Ad Unit ID provided above is just for testing. It will allow you to retrieve a sample banner ad and make sure your implementation is correct. You should always use test ads when developing and testing your app--testing with live production ads is a violation of AdMob policy and could cause your account to be suspended. See the addTestDevice method documentation for information on how to get test ads with your own Ad Unit IDs.

While it's not a requirement, storing your Ad Unit ID values in a resource file is a good practice. As your app grows and your ad publishing needs mature, you will occasionally find that you want to change the ID values. If you make sure they're always in a resource file, you'll never have to search through your code looking for them.

Place an AdView in your main activity layout

Layout files contain XML definitions for the visual design of things like activities, fragments, and list items. In this step, we'll be modifying the layout file for the main activity so that it includes an AdView at the bottom. You can add things to an activity progammatically via Java code, but layout files offer better separation of presentation and behavior.

There are only two steps remaining before your app is ready to show an ad. First, you'll need to modify your main activity's layout to include an AdView. Open BannerExample/app/src/main/res/layout/activity_main.xml in the editor.

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

</RelativeLayout>

Add these to the XML:

1    An additional namespace used for ads:

    http://schemas.android.com/apk/res-auto

 2   A new element for your AdView. You'll be asked to provide layout_width and layout_height. You can set both to wrap_content. In the AdView tag, set the adSize to BANNER and the adUnitId to @string/banner_ad_unit_id.

If you look at the last parameter in the AdView tag, you'll see that it's called adUnitId. This is the Ad Unit ID that the AdView will use when requesting ads. In this case, we've given it a reference to the string resource you added in the last step, so the AdView will use that value.

Load the ad in the MainActivity class

The last change needed is for you to add to your app's main activity class some Java code that will load an ad into the AdView.

Open your MainActivity.java file. It will be in the BannerExample/app/src/main/java/ folder, though the exact subdirectory path will vary based on the domain you used when creating your project above. Once it's open in the editor, look for the onCreate method in the MainActivity class:

MainActivity.java (excerpt)

package ...

import ...
import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends ActionBarActivity {

    ...

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }

    ...

}

Make these two changes:

  1. Import the AdRequest and AdView classes.
  2. Add the code that will find your AdView in the layout, create an AdRequest, and then load an ad into the AdView with it.

Do not use the AdRequest line shown above if you are testing. Refer to our Targeting page to learn more about using test devices and test device IDs.

Once that's completed, you're finished. You now have a fully functional AdView in your app's main activity.

Enjoy a freshly loaded ad

Your app is now ready to display an ad using the Google Mobile Ads SDK. Run it again, and you should see a test banner displayed at the bottom of the device screen:
Congratulations! You've successfully integrated banner ads into an app.

Thursday, 6 August 2015

5+ Ways to Install Android Apps on Your Phone or Tablet



5+ Ways to Install Android Apps on Your Phone or Tablet



Google provides a primary app store for Android, known as Google Play. However, Android isn’t like iOS. Google Play isn’t the only game in town, and there are other ways to install apps on Android.
Some Android devices — particularly dirt-cheap ones coming direct from manufacturers in China — aren’t certified by Google and don’t include Google Play. This makes the device much less useful, but you can still install apps on it.
Google Play App
The primary way you’ll install apps on Android is by firing up the Play Store app on your phone or tablet. You’ll find the Play Store in your app drawer and likely on your default home screen. You can also open it by tapping the shopping bag-like icon at the top-right corner of the app drawer.
 android-open-play-store
Once in the store, browse or search for an app and tap the Install button to install it.
 install-android-app
Google Play on the Web
Hunting for apps using a touchscreen keyboard isn’t the most convenient way to install apps. To install apps from your computer, visit the Google Play website at play.google.com. You can search and browse for apps on the website.
As long as you’re logged in with the same Google account you use for your Android phone or tablet, you can click the install button on the website to remotely install the app on your phone or tablet. If you have multiple Android devices, you can select the one you want the  app installed on. Once you’ve told Google Play to install the app, your phone or tablet will start downloading the app and install it for you.
 install-android-app-from-web-brower-on-pc
Sideloading Apps
Android supports sideloading, which allows you to install apps from outside of Google Play. However, this is disabled by default for security reasons. To enable sideloading, open the Settings app on your Android, tap the Security category, and enable the Unknown sources check box.
Note that this can be a security risk, as it allows installation of apps from outside the Play Store, which could potentially contain malware. If you enable this setting, it’s your job to install applications responsibly — stay away from pirated games and other apps that may contain Android malware.
 android-unknown-sources
After enabling this setting, you can download an Android app in .APK format and install it on your device. For example, you could download the .APK file in your Android’s browser and open it from the Downloads app. You could also download the APK file to your computer, copy it over to your Android’s file system with a USB cable, use a file manager app to browse to it, and tap the APK file to start installing it.
Sideloading allows you to install a variety of apps that aren’t available in the Play Store, such as the XBMC Media Center for Android, various emulators that have been removed from Google Play, and third-party app store apps like the Amazon Appstore for Android and Humble Bundle app.
 sideload-android-app
This option may not be available on some devices if the device’s manufacturer or carrier has disabled it. AT&T once did this, but it’s become much less common. Most devices should have the Unknown sources checkbox.

Third-Party App Stores
Android allows for third-party app stores. The most well-known and popular one is Amazon’s Appstore for Android (currently only available in the US), which gives away a free paid app every day. Amazon’s Appstore for Android is also used natively on Amazon’s Kindle Fire devices.
 amazon-appstore-for-android
The ever-popular Humble Bundle, which sells bundles of indie games for Windows, Mac, and Linux, has also sold bundles of Android games. If you purchased any of the Humble Bundles containing Android games, you could install the Humble Bundle app and use it to manage the installation and updating of your Humble Bundle games.
To use either the Amazon Appstore, Humble Bundle, or other third-party app stores, you’ll need to sideload the app store’s app.
 humble-bundle-for-android
Some devices may come with their own, integrated third-party app stores. For example, Samsung devices come with the Samsung Apps app, which may contain free versions of some paid apps, but is otherwise fairly uninteresting. Carriers have distributed their own app stores with their Android devices in the past, but this is becoming less common.
Just as you should be extra careful when sideloading apps, you should be extra careful when using third-party app stores. For example, you can probably trust Amazon and the Humble Bundle, but you should beware of many other app stores. For example, an untrustworthy app store distributing pirated apps may be a source of malware. We recommend staying away from those.

Sideloading From Your PC
You can also sideload apps onto your Android device in other ways. For example, if you have an APK file on your computer, you can use the excellent AirDroid app to upload it to your Android device and install it without even connecting your Android device to your computer.
 airdroid-install-apk
If you’re a developer, you can use the adb (Android debug bridge) command to “push” an app to a connected device, installing it from your computer. The appropriate command is as follows, where C:\package.apk is the path to the APK file on your computer:

adb install C:\package.apk
________________________________________
You can also install Android apps on your Windows PC, which opens up a whole new world of touchscreen games and apps on touch-enabled Windows 8 devices.