Android developers get started

Get Started

Integrating the Google Mobile Ads SDK into an app is the first step toward displaying ads and earning revenue. Once you’ve integrated the SDK, you can choose an ad format (such as native or rewarded video) and follow the steps to implement it.

Before you begin

To prepare your app, complete the steps in the following sections.

App prerequisites

    Use Android Studio 3.2 or higher

Make sure that your app’s build file uses the following values:

  • A minSdkVersion of 16 or higher
  • A compileSdkVersion of 28 or higher

Set up your app in your AdMob account

Register your app as an AdMob app by completing the following steps:

Register your app with AdMob. This step creates an AdMob app with a unique AdMob App ID that is needed later in this guide.

Configure your app

In your project-level build.gradle file, include Google’s Maven repository and Maven central repository in both your buildscript and allprojects sections:

Add the dependencies for the Google Mobile Ads SDK to your module’s app-level Gradle file, normally app/build.gradle :

Add your AdMob app ID (identified in the AdMob UI) to your app’s AndroidManifest.xml file. To do so, add a tag with android:name=»com.google.android.gms.ads.APPLICATION_ID» . You can find your app ID in the AdMob UI. For android:value , insert your own AdMob app ID, surrounded by quotation marks.

In a real app, use your actual AdMob app ID, not the one listed above. If you’re just looking to experiment with the SDK in a Hello World app, you can use the sample app ID shown above.

Note also that failure to add the tag as shown above results in a crash with the message:

(Optional) Declare AD_ID permission for previous versions to work with Android S.

If your app uses the Google Mobile Ads SDK version 20.4.0 or higher, you can skip this step since the SDK automatically declares the com.google.android.gms.permission.AD_ID permission and is able to access the Advertising ID whenever it’s available.

For apps that use the Google Mobile Ads SDK version 20.3.0 or lower and are targeting Android S, you must add the com.google.android.gms.permission.AD_ID permission in the AndroidManifest.xml file in order to target Android S:

To learn more about the com.google.android.gms.permission.AD_ID permission declaration, including how to disable it, please refer to this Play Console article.

Initialize the Google Mobile Ads SDK

Before loading ads, have your app initialize the Google Mobile Ads SDK by calling MobileAds.initialize() which initializes the SDK and calls back a completion listener once initialization is complete (or after a 30-second timeout). This needs to be done only once, ideally at app launch.

Ads may be preloaded by the Google Mobile Ads SDK or mediation partner SDKs upon calling MobileAds.initialize() . If you need to obtain consent from users in the European Economic Area (EEA), set any request-specific flags (such as tagForChildDirectedTreatment or tag_for_under_age_of_consent ), or otherwise take action before loading ads, ensure you do so before initializing the Google Mobile Ads SDK.

Here’s an example of how to call the initialize() method in an Activity:

Example MainActivity (excerpt)

Kotlin

If you’re using mediation, wait until the completion handler is called before loading ads, as this will ensure that all mediation adapters are initialized.

Select an ad format

The Google Mobile Ads SDK is now imported and you’re ready to implement an ad. AdMob offers a number of different ad formats, so you can choose the one that best fits your app’s user experience.

Rectangular ads that appear at the top or bottom of the device screen. Banner ads stay on screen while users are interacting with the app, and can refresh automatically after a certain period of time. If you’re new to mobile advertising, they’re a great place to start.

Interstitial

Full-screen ads that cover the interface of an app until closed by the user. They’re best used at natural pauses in the flow of an app’s execution, such as between levels of a game or just after a task is completed.

Native

Customizable ads that match the look and feel of your app. You decide how and where they’re placed, so the layout is more consistent with your app’s design.

Rewarded

Ads that reward users for watching short videos and interacting with playable ads and surveys. Used for monetizing free-to-play apps.

Additional resources

The Google Mobile Ads repository on GitHub demonstrates how to use the different ad formats that this API offers.

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Источник

How to Get Started with Android Development

An intro to Android Development

Android apps can be a great, fun way to get into the world of programming. Officially programmers can use Java, Kotlin, or C++ to develop for Android. Though there may be API restrictions, using certain tools developers can use a large number of languages, including JavaScript, C, or assembly. The possibilities are endless.

Читайте также:  Что означает надпись андроид

From simple games and utility apps to full-blown music players, there are many opportunities to create something meaningful with Android. The Android developer community is widespread, and the documentation and resources online are easy to find, so that you can tackle any issue you’re facing.

There is definitely a learning curve to get used to the Android framework. But once you understand the core components that make up the app, the rest will come naturally.

The learning curve involved in Android has a relatively smaller slope compared to learning other technologies such as NodeJS. It is also relatively easier to understand and make contributions towards AOSP hosted by Google. The project can be found here.

Getting started

Check out the guides in this folder to learn about the 4 core components that make up an Android app and how you can get started with a sample app. Then delve into the more advanced topics such as fragments and the Gradle build system. Finally, check out the material design specifications guide as well to learn how to make your apps beautiful and user friendly.

Setting Up and Getting Started with Android Studio

Go to this link and install the latest JDK. Now download the Android Studio and SDK tools bundle from here. Install the Android Studio and SDK following the set up. Keep note of the SDK location. If you face any errors go to settings later to resolve them.

Lastly, learn to integrate 3rd party libraries and Firebase services to add functionality to your app. It would be helpful if you go through the official documentation for each component.

Official Documentation

Java vs. Kotlin: which language to learn?

Ever since Google announced Kotlin as the official language for Android development at Google IO in 2017, programmers who want to become Android developers have a dilemma. The big question in front of them is whether they should learn Kotlin or Java.

Beginners in Android Development Should Start With Java

The first and foremost thing is that Android development is not everything. As a programmer, you may be starting your career with Android development. But if you start with a well-established language like Java, you become a part of the bigger Java community and market, which directly means more job opportunities.

The second and more important thing is that there is a huge community of Java programmers, which means you can find answers when you are stuck. This is very important because, as a beginner, you will face a lot of technical problems and you might not know where to head when you are stuck.

When you search Google with a Java problem, you are bound to get answers. But the same cannot be said for Kotlin, which is still a new programming language.

Java Programmers Should Learn Kotlin

Now, coming back to the second set of programmers who want to learn Android development: our fellow Java developers. For them, I think it’s best to learn Kotlin because it really improves productivity.

A class which takes 50 lines of code in Java can really be written in just one line in Kotlin. It can help you avoid all boiler-plate code — for example, you don’t need to specify getters and setters, equals(), hashCode() or toString() methods. Kotlin can generate all that by itself.

If you don’t know, Kotlin was development by JetBrains, the company behind one of the most popular Java IDEs, IntelliJ IDEA. They were a Java shop developing IDEs like IntelliJ IDEA, PyCharm, and ReSharper, all in Java. And then they built Kotlin to improve their productivity. But at the same time, they couldn’t rewrite all their code in Kotlin, so that’s why they made Kotlin fully interoperable with Java.

Because Kotlin generates Java bytecode, you can use your favorite Java frameworks and libraries in Kotlin and your Java friends can also use any Kotlin framework you develop.

Java resources:

Kotlin resources:

More info on Android development:

If this article was helpful, tweet it.

Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started

freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)

Our mission: to help people learn to code for free. We accomplish this by creating thousands of videos, articles, and interactive coding lessons — all freely available to the public. We also have thousands of freeCodeCamp study groups around the world.

Donations to freeCodeCamp go toward our education initiatives and help pay for servers, services, and staff.

Источник

Get started with the Navigation component

This topic shows you how to set up and work with the Navigation component. For a high level overview of the Navigation component, see the Navigation overview.

Set up your environment

To include Navigation support in your project, add the following dependencies to your app’s build.gradle file:

Groovy

Kotlin

For information on adding other Architecture Components to your project, see Adding components to your project.

Create a navigation graph

Navigation occurs between your app’s destinations—that is, anywhere in your app to which users can navigate. These destinations are connected via actions.

A navigation graph is a resource file that contains all of your destinations and actions. The graph represents all of your app’s navigation paths.

Figure 1 shows a visual representation of a navigation graph for a sample app containing six destinations connected by five actions. Each destination is represented by a preview thumbnail, and connecting actions are represented by arrows that show how users can navigate from one destination to another.

Figure 1. A navigation graph that shows previews of six different destinations that are connected via five actions.

  1. Destinations are the different content areas in your app.
  2. Actions are logical connections between your destinations that represent paths that users can take.

To add a navigation graph to your project, do the following:

  1. In the Project window, right-click on the res directory and select New > Android Resource File. The New Resource File dialog appears.
  2. Type a name in the File name field, such as «nav_graph».
  3. Select Navigation from the Resource type drop-down list, and then click OK.
Читайте также:  Подходят ли эпл вотч для андроида

When you add your first navigation graph, Android Studio creates a navigation resource directory within the res directory. This directory contains your navigation graph resource file ( nav_graph.xml , for example).

After adding a graph, Android Studio opens the graph in the Navigation Editor. In the Navigation Editor, you can visually edit navigation graphs or directly edit the underlying XML.

Figure 2. The Navigation Editor

  1. Destinations panel: Lists your navigation host and all destinations currently in the Graph Editor.
  2. Graph Editor: Contains a visual representation of your navigation graph. You can switch between Design view and the underlying XML representation in the Text view.
  3. Attributes: Shows attributes for the currently-selected item in the navigation graph.

Click the Text tab to see the corresponding XML, which should look similar to the following snippet:

Источник

Developer guide

Android’s enterprise features provide organizations with a secure, flexible, and unified Android mobility platform—combining devices, applications, and management. Android apps are compatible with Android’s enterprise features by default. However, there are additional features you can use to make your app work best on managed Android devices:

  • Work profile compatibility—Modify your Android app so it functions best on a managed device.
  • Managed configurations—Modify your app to allow IT admins the option to specify custom settings for your apps.
  • Dedicated devices—Optimize your app so that it can be deployed on an Android device as a kiosk.
  • Single Sign-On (SSO)—Simplify the sign-on process for users signing in to different apps on their managed Android device.

Prerequisites

  1. You’ve created an Android app.
  2. You’re ready to modify your app so that it works best for organizations.
  3. Minimum version: Android 5.0 Lollipop recommended version: Android 6.0 Marshmallow and later.

Note: Android’s enterprise features are built into most Android 5.0 devices; however, Android 6.0 and later offers additional features, especially with regard to dedicated devices.

Work profiles

You can manage a user’s business data and applications through a work profile. A work profile is a managed corporate profile associated with the primary user account on an Android device. A work profile securely isolates work apps and data from personal apps and data. This work profile is in a separate container from the personal profile, which your user controls. These separate profiles allow organizations to manage the business data they care about, but leave everything else on a user’s device under the user’s control. For a deep dive into best practices, see the Work profiles guide. For an overview of those best practices, see below.

Key features of a work profile

  • Separate and secure profile
  • Managed Google Play for application distribution
  • Separate badged work applications
  • Profile-only management capabilities controlled by an admin

Work profile benefits on Android 5.0+

  • Full device encryption
  • One Android application package (APK) for both profiles when there’s a personal profile and a work profile present on the device
  • Device policy controller (DPC) is limited to the work profile
  • Device administration via the DevicePolicyManager class

Considerations for work profiles

  • The Android system prevents intents from crossing profiles and IT admins can enable or disable system apps.
  • A file path (Uniform Resource Identifier [URI]) that’s valid on one profile may not be valid on the other.

Prevent intents from failing between profiles

It’s difficult to know which intents can cross between profiles, and which ones are blocked. The only way to know for sure is by testing. Before your app starts an activity, you should verify that the request is resolved by calling Intent.resolveActivity() .

  • If it returns null , the request doesn’t resolve.
  • If it returns something, it shows that the intent resolves, and it’s safe to send the intent.

Note: For detailed testing instructions, see Prevent Failed Intents.

Share files across profiles

Some developers use URIs to mark file paths in Android. However, because there are separate file systems when a work profile is present, we recommend:

Use:
Content URIs
  • The content URIs contain the authority, path, and ID for a specific file. You can generate this using FileProvider subclass. Learn more
  • Share and grant permissions to access the content URI using an Intent. Permissions can only be passed across the profile boundary using Intents. If you grant another app access rights to your file using Context.grantUriPermission() , it only is granted for that app in the same profile.
Don’t use:
File URI
  • Contains the absolute path of the file on the device’s storage.
  • A file path URI that’s valid on one profile isn’t valid on the other.
  • If you attach a file URI to an intent, a handler is unable to access the file in another profile.

Next steps: Once your app supports managed profiles, test it in a work profile. See Test your app.

Implement managed configurations

Managed configurations are a set of instructions that IT admins can use to manage their users’ mobile devices in a specific way. These instructions are universal and work across any EMM, allowing admins to remotely configure applications on their users’ phones.

If you’re developing apps for business or government, you may need to satisfy your industry’s specific set of requirements. Using managed configurations, the IT admin can remotely specify settings and enforce policies for their users’ Android apps; for example:

  • Configure if an app can sync data via cellular/3G, or only Wi-Fi
  • Allow or block URLs on a web browser
  • Configure an app’s email settings
  • Enable or disable printing
  • Manage bookmarks

Best practices for implementing managed configurations

The Set up Managed Configurations guide is the key source for information on how to build and deploy managed configurations. After you’ve reviewed this documentation, see recommendations below for additional guidance.

When first launching the app

As soon as you launch an application, you can see if managed configurations are already set for this app in onStart() or onResume() . Additionally, you can find out if your application is managed or unmanaged. For example, if getApplicationRestrictions() returns:

  • A set of application-specific restrictions—You can configure the managed configurations silently (without requiring user input).
  • An empty bundle—Your application acts like it’s unmanaged (for example, how the app behaves in a personal profile).
  • A bundle with a single key value pair with KEY_RESTRICTIONS_PENDING set to true—your application is being managed, but the DPC isn’t configured correctly. You should block this user from your app, and direct them to their IT admin.
Читайте также:  Magic cast для андроид зеленая точка

Listen for changes to managed configurations

IT admins can change managed configurations and what policies they want to enforce on their users at any time. Because of this, we recommend you ensure that your app can accept new restrictions for your managed configuration as follows:

  • Fetch restrictions on launch—Your app should call getApplicationRestrictions() in onStart() and onResume() , and compare against old restrictions to see if changes are required.
  • Listen while running—Dynamically register ACTION_APPLICATION_RESTRICTIONS_CHANGED in your running activities or services, after you’ve checked for new restrictions. This intent is sent only to listeners that are dynamically registered, and not to listeners declared in the app manifest.
  • Unregister while not running—In onPause() , you should unregister for the broadcast of ACTION_APPLICATION_RESTRICTIONS_CHANGED .

Dedicated devices

Dedicated devices are kiosk devices used for a single purpose, such as digital signage displays, ticket printing kiosks, or checkout registers.

When an Android device is configured as a dedicated device, the user sees an application locked to the screen with no Home or Recent Apps buttons to escape the app. Dedicated devices can also be configured to show a set of applications, such as a library kiosk with an app for the library catalog and a web browser.

Set up single sign-on with Chrome custom tabs

Enterprise users often have multiple apps on their device, and they prefer to sign in once to access all of their work applications. Typically, users sign in through a WebView; however, there are a couple reasons why this isn’t ideal:

  1. Users often need to sign in multiple times with the same credentials. The WebView solution often isn’t a true Single Sign-On (SSO) experience.
  2. There can be security risks, including malicious applications inspecting cookies or injecting JavaScript® to access a user’s credentials. Even trusted developers are at risk if they rely on potentially malicious third-party SDKs.

A solution to both problems is to authenticate users using browser Custom Tabs, instead of WebView. This ensures that authentication:

  • Occurs in a secure context (the system browser) where the host app cannot inspect contents.
  • Has a shared cookie state, ensuring the user has to sign in only once.

Requirements

Custom Tabs are supported back to API level 15 (Android 4.0.3). To use Custom Tabs you need a supported browser, such as Chrome. Chrome 45 and later implement this feature as Chrome Custom Tabs.

How do I implement SSO with custom tabs?

Google has open sourced an OAuth client library that uses Custom Tabs, contributing it to the OpenID Connect working group of the OpenID Foundation. To set up Custom Tabs for SSO with the AppAuth library, see the documentation and sample code on GitHub.

Test your app

After you’ve developed your app, you’ll want to test it—both in a work profile and on a fully managed device. See the instructions below.

Use Test DPC to test your Android app

We provide the Test DPC app to help Android developers test their apps in an enterprise environment. Using Test DPC, you can set EMM policies or managed configuration values on a device—as if an organization managed the device using an EMM. To install Test DPC on a device, choose one of the following methods:

  • Install Test DPC from GooglePlay.
  • Build from the source on GitHub.

For more information on how to configure Test DPC, see the instructions below and the Test DPC User Guide.

Provision a work profile

To test your app in a work profile, you need to first provision a work profile on device using the Test DPC app, as follows:

  1. Install Test DPC on the device.
  2. In the Android launcher, tap the Set up Test DPC app icon.
  3. Follow the onscreen instructions.
  4. Install your app on the device and test to see how it runs in the work profile.

Android creates a work profile and installs a copy of Test DPC in the work profile. You use this work-badged instance of Test DPC to set policies and managed configurations in the work profile. To learn more about setting up a work profile for development, read the developer’s guide Work profiles.

Provision a fully managed device

Organizations use fully managed devices because they can enforce a full range of management policies on the device. To provision a fully managed device, follow these steps:

  1. Install Test DPC on the device.
  2. Confirm that there are no other users or a work profile on the device.
  3. Confirm that there are no accounts on the device.
  4. Run the following Android Debug Bridge (adb) command in your terminal:
  5. Once you’ve completed provisioning the device owner, you can test your app on that device. You should specifically test how managed configurations and intents work on that device.

You can also use other provisioning methods—see the Test DPC User Guide. To learn how IT admins typically enroll and provision Android-powered devices, read Provision devices.

End-to-end testing

After you’ve finished testing your app in the environments above, you’ll likely want to test your app in an end-to-end production environment. This process includes the steps a customer needs to take to deploy your app in their organization, including:

  • App distribution through Play
  • Server-side managed configuration
  • Server-side profile policy control

You need to access an EMM console to complete the end-to-end testing. The easiest way to get one is to request a testing console from your EMM. Once you have access, complete these tasks:

  1. Create a test version of your application with a new ApplicationId.
  2. Claim a managed Google domain and bind it to your EMM. If you already have a testing domain that’s bound to an EMM, you may need to unbind it to test it with your preferred EMM. Please consult your EMM for the specific unbinding steps.
  3. Publish your application to the private channel for their managed Google domain.
  4. Use the EMM console and EMM application to:
    1. Set up work devices.
    2. Distribute your application.
    3. Set managed configuration.
    4. Set device policies.

This process will differ based on your EMM. Please consult your EMM’s documentation for further details. Congrats! You’ve completed these steps and verified that your app works well for enterprise users.

Content and code samples on this page are subject to the licenses described in the Content License. Java is a registered trademark of Oracle and/or its affiliates.

Источник

Оцените статью